i want to blink LED in reverse order

Thread Starter

ect_09

Joined May 6, 2012
180
Hello,
i want to reverse the code as it complete first time.
for example
pin 1-pin 2-pin 3-pin 4 (its somplete)
now it should run as
pin 4-pin 3-pin 2-pin 1

i write this code but its not working in reverse order.please guide me in this way.
Code:
#include<htc.h>

__CONFIG(1,OSCSDIS & HSPLL);
__CONFIG(2,BORDIS & PWRTDIS &WDTDIS);
__CONFIG(3,CCP2RC1);
__CONFIG(4,LVPDIS & STVREN);
__CONFIG(5,UNPROTECT);
__CONFIG(6,WRTEN);
__CONFIG(7,TRU);

#define _XTAL_FREQ   40000000


void delay_sec(unsigned char seconds)    // This function provides delay in terms of seconds
{
    unsigned char i,j;

    for(i=0;i<seconds;i++)
        for(j=0;j<100;j++)
            __delay_ms(10);
}
void led_display(char a)
{
switch(a)

{
case 0: PORTB=0x01;PORTD=0x08;  break;
case 1: PORTB=0x02;PORTD=0x04;  break;
case 2: PORTB=0x04;PORTD=0x02;  break;
case 3: PORTB=0x08;PORTD=0x01;  break;
}
}
void main()
{
   
TRISB=0x00;
TRISD=0x00;
char a,b;

while(1)
{
led_display(a);

a++;
delay_sec(1);
if(a>=4)
{
a--;
}


}

}
 

takao21203

Joined Apr 28, 2012
3,702
your program has many issues.

a is never initialized.
first you increment then you decrement it again. but you need to clear it again indeed.
 

MrChips

Joined Oct 2, 2009
30,706
Instead of counting 1-2-3-4-4-3-2-1 or whatever order, count from 0 to (n-1) and assign a display mode for each of the n states. Thus you can display any sequence you desire.
 
Top