Simple code mysterious problem

Status
Not open for further replies.

skmdmasud

Member
Hi..
i wrote this very simple code, but when i run it simply does not do what it is suppose to do. The turning on leds part hangs. The check all led part works fine. there is only one button PinD0 when i press it repeatedly only the PIND3 lights up and hangs until the loop exists. I also tried "use case " but same result.

Code:
int main(void)
{
 
   DDRB  = 0b11111111; //LED
   PORTB = 0b00000000; //low
 
   DDRC =  0b11111111; //LED
   PORTC = 0b00000000; //low
 
   DDRD =  0b11111110; //For Various inputs e.g switches
   PORTD = 0b00000001; //pullup for input and low for led
 
   //------INITIAL BUTTON PRESS-------------------------------------------------------------------------------------------------------------
   int set_delay,loop;
   if(bit_is_clear(PIND,0)) //launch initial setting
   {
     //_delay_ms(300);
     set_delay = 1;
     while (loop < 15)
     {
       _delay_ms(300);
       loop++;
       if(bit_is_clear(PIND,0)) //detect input
       {
         _delay_ms(300);         set_delay++;         loop = 0;
         if(set_delay > 20)
         {   set_delay = 1;   }
//-----------------------------turning on led with set delay value --------------------------------------
           if (set_delay == 1)
             {
               PORTD |= 1<<PIND1;
             }
             if (set_delay == 2)
             {
               PORTD |= 1<<PIND2;
             }
           
             if (set_delay == 3)
             {
               PORTD |= 1<<PIND3;
             }
           
             if (set_delay == 4)
             {
               PORTD |= 1<<PIND4;
             }
             if (set_delay == 5)
             {
               PORTB |= 1<<PINB6;
             }
             if (set_delay == 6)
             {
               PORTB |= 1<<PINB7;
             }
             if (set_delay == 7)
             {
               PORTD |= 1<<PIND5;
             }
             if (set_delay == 8)
             {
               PORTD |= 1<<PIND6;
             }
             if (set_delay == 9)
             {
               PORTD |= 1<<PIND7;
             }
             if (set_delay == 10)
             {
               PORTB |= 1<<PINB0;
             }
             if (set_delay == 11)
             {
                PORTB |= 1<<PINB1;
             }
             if (set_delay == 12)
             {
               PORTB |= 1<<PINB2;
             }
             if (set_delay == 13)
             {
               PORTB |= 1<<PINB3;
             }
             if (set_delay == 14)
             {
               PORTB |= 1<<PINB4;
             }
             if (set_delay == 15)
             {
               PORTB |= 1<<PINB5;
             }
             if (set_delay == 16)
             {
                PORTC |= 1<<PINC0;
             }
             if (set_delay == 17)
             {
               PORTC |= 1<<PINC1;
             }
             if (set_delay == 18)
             {
               PORTC |= 1<<PINC2;
             }
             if (set_delay == 19)
             {
               PORTC |= 1<<PINC3;
             }
             if (set_delay == 20)
             {
               PORTC |= 1<<PINC4;
             }       
           
        }
     }
   
     eeprom_update_word((&NV_delay), set_delay); //writing eeprom memory
   } 
   //------------------------------------------check all leds----------------------------------
   PORTB = 0b11111111;
   PORTD = 0b11111111;
   PORTC = 0b11111111;
   _delay_ms(1000);
   PORTB = 0b00000000;
   PORTD = 0b00000001;
   PORTC = 0b00000000;
   _delay_ms(500);

  while(1)
{

}


}
 
Last edited:
Usually outputs are set to 0 not 1..... 1 is for inputs..

Not sure about the chip you are using
 
Just for your information... Some compilers enumerate pin numbers differently

PORTD |= 1<<PIND3; PIND3 may not enumerate to 3.... You need to check your compiler specs.. Try
PORTD |= 1<<3; Just to see if it works...
 
Could you try to define "set_delay" as volatile?

(e.g. if it's a "char set_delay;" , make it "volatile char set_delay;")

does it work any better?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…