Thread: Help with LED program update :) (BEGINNER)

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    26

    Post Help with LED program update :) (BEGINNER)

    I'm a beginner, I have an Atmel STK500 AVR development board.

    EDIT: I didn't explain what the code does, sorry :S When I press a button on the board, the corresponding LED lights up until I release the button!

    I've manipulated code I found on the internet into this:

    Code:
    #include <avr/io.h>     // avr header file for IO ports
    int main(void){
        DDRA = 0x00;    // set PORTA for input
        DDRB = 0xFF;    // set PORTB for output
        PORTB = 0x00;  // turn ON all LEDs initially
        
        while(1){
            PORTB = PINA;
        }
        return 1;
    }
    That code works fine. I wanted to try to write the same thing another way. Here's what I have so far:

    Code:
    #include <avr/io.h>     // avr header file for IO ports
    
    
    int main(void){
        DDRA = 0x00;    // set PORTA for input
        DDRB = 0xFF;    // set PORTB for output
        PORTB = 0x00;  // turn ON all LEDs initially
        
        if(PORTA == 0){
            PORTB = 0;
        }        
            else PORTB = 1;
        return 1;
    }
    This code builds and compiles but when I run the program on my development board the LEDs just stay lit and the buttons do nothing. I want to achieve the same results as I do with the first code, just written differently. I also noticed that my second code doesn't have a loop, does it need one? Any help? Thank you!
    Last edited by aosterminal; 09-23-2012 at 05:41 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think you know the answers, pretty well:

    IO isn't done in one instant, so you need a loop.

    You need to use VERY similar logic to get what you need. The board has it's required addresses, and the amount of code is very small and fast (and should stay small and fast).

    Maybe use a for loop, instead of a while loop? That's very doable. Admittedly mostly cosmetic, of course.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Quote Originally Posted by Adak View Post

    You need to use VERY similar logic to get what you need. The board has it's required addresses, and the amount of code is very small and fast (and should stay small and fast).
    What do you mean about similar logic?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I mean inside the loop, portB needs to be assigned to pinA. You can't just assign it to any old address, it's got to be THAT address.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Quote Originally Posted by Adak View Post
    I mean inside the loop, portB needs to be assigned to pinA. You can't just assign it to any old address, it's got to be THAT address.
    Like this?:

    Code:
    #include <avr/io.h>     // avr header file for IO ports
    
    
    int main(void){
    	DDRA = 0x00;    // set PORTA for input
    	DDRB = 0xFF;    // set PORTB for output
    	PORTB = 0xFF;  // turn OFF all LEDs initially
    	
    	while(1){
    	if(PORTA == 0){
    		PINA = 1;
    	}		
    		else PINA = 0;
    	}		
    	return 1;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No. I believe that PINA is a hardware address, not a logical value of 1 or true. You have to assign the PORTB pointer, to that address.

    and you don't need the else, at all.

    I haven't worked with your board before, however.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    This is the sort of programming that I'd like to get into soon. Looks like a lot of fun.


    AVR 8- and 32-bit Microcontrollers- Atmel Corporation
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Regarding the second bit of code in your first post; did you try putting *that* code into a constant loop?

    @cfanatic: In my opinion, if you have no MCU experience, then PIC (Microchip) microcontrollers are the best to start with. They are (again, in my opinion) the most "friendly" to work with.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Matticus View Post
    Regarding the second bit of code in your first post; did you try putting *that* code into a constant loop?

    @cfanatic: In my opinion, if you have no MCU experience, then PIC (Microchip) microcontrollers are the best to start with. They are (again, in my opinion) the most "friendly" to work with.
    From working as a Teaching Assistant I have learned the below PIC information.

    When working with PIC Chips it is very important to remember to hookup power and ground.
    I have had students who missed either power or the ground connections and the PIC chip almost worked right.
    So, if the PIC project almost runs right; confirm that you have a good power and ground DO NOT assume it is good.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help with Beginner Program
    By anchorblue804 in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2012, 10:18 AM
  2. C Program beginner
    By striderx240 in forum C Programming
    Replies: 15
    Last Post: 01-26-2012, 10:17 PM
  3. Help beginner with program please
    By bubbles56 in forum C Programming
    Replies: 1
    Last Post: 03-23-2011, 10:37 PM
  4. Update Program Microsoft C V5.1 to Visual C++ 6.0
    By shatley in forum C Programming
    Replies: 5
    Last Post: 10-27-2003, 01:56 PM
  5. new program update!
    By jobolikescake in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2002, 10:40 AM