led on program / led blink program

Discussion in 'Embedded Systems and Microcontrollers' started by zak9000, Dec 5, 2012.

  1. zak9000

    Thread Starter New Member

    Jan 1, 2012
    20
    0
    hi

    i am new to embedded programming and am currently using a stk500 with a atmega32 mcu. i recently was able to program a simple led blink program which turns on and off all the leds connected to port b of the mcu as shown below:

    #include <avr/io.h>
    #include <util/delay.h>
    int main(void)
    {
    DDRB=0xFF;
    while(1)
    {

    PORTB=0xFF;
    _delay_ms(100);
    PORTB=0x00;
    _delay_ms(100);
    }
    }

    this code works fine but then i want to just turn on the led and so i tried

    #include <avr/io.h>

    int main(void)
    {
    DDRB=0xFF;
    PORTB=0xFF;
    while(1)
    {

    }


    }

    after programming my mcu i expected all leds to light up but none of them lighted up, maybe im missing something?
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    What happens when you change PORTB=0xFF; to PORTB=0x00; ?
     
  3. zak9000

    Thread Starter New Member

    Jan 1, 2012
    20
    0
    PORTB=0xFF; //set all pins on port b to high
    PORTB=0x00; // set all pins on port b to low
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    your while(1) may be getting optimized out... try setting PORTB high within the while loop...
     
  5. zak9000

    Thread Starter New Member

    Jan 1, 2012
    20
    0
    i did try adding the code in the while loop but the leds still do not turn on. however i have noticed that when i turn off my programming board and turn it back on with the mcu all the leds turn on a second before they are turned off.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    That means very little.

    If you have the blinking code working, try commenting out the code that turns the lights off.

    Also you normally set latches and read ports. You might want to check that.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    Yes that is obvious. but depending on the configuration of your LEDs, you can turn them on with a high or a low. Both cathode or anode can be tied to a pin. Cathode can be tied to one pin and anode tied to another.

    Have you used a scope, logic probe or voltmeter to see if they are actually changing state?
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    Also what have you done to debug the code? How do you know the code is even running?
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    That may be very significant. Just what does turn the LEDs on? Is it a high or a low on the I/O pins? I have no idea without a schematic.

    What happens if you change this to:

    PORTB=0x00;

    inside the always on program?
     
  10. zak9000

    Thread Starter New Member

    Jan 1, 2012
    20
    0
    Yes that is obvious. but depending on the configuration of your LEDs, you can turn them on with a high or a low. Both cathode or anode can be tied to a pin. Cathode can be tied to one pin and anode tied to another.

    yes you were right by simply changing portb=0x00 it sovled the problem thanks!!!
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005

    Had you posted your schematic in the message at the top of the thread this would have been solved in the first answer.
     
  12. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    umm.......

    *Who the heck are you!?*


    *Note: this was in reference to a post that was removed....
     
    Last edited: Dec 7, 2012
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    A moron spammer.
     
  14. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Wow, his post even got removed...well, he probably did it himself.... now I need to edit my post so i don't look too crazy:)
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    No the mods got to him I am sure. They are really good at that.
     
Loading...