i want to blink LED in reverse order

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Oct 30, 2014.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    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 (Text):
    1.  
    2. #include<htc.h>
    3.  
    4. __CONFIG(1,OSCSDIS & HSPLL);
    5. __CONFIG(2,BORDIS & PWRTDIS &WDTDIS);
    6. __CONFIG(3,CCP2RC1);
    7. __CONFIG(4,LVPDIS & STVREN);
    8. __CONFIG(5,UNPROTECT);
    9. __CONFIG(6,WRTEN);
    10. __CONFIG(7,TRU);
    11.  
    12. #define _XTAL_FREQ   40000000
    13.  
    14.  
    15. void delay_sec(unsigned char seconds)    // This function provides delay in terms of seconds
    16. {
    17.     unsigned char i,j;
    18.  
    19.     for(i=0;i<seconds;i++)
    20.         for(j=0;j<100;j++)
    21.             __delay_ms(10);
    22. }
    23. void led_display(char a)
    24. {
    25. switch(a)
    26.  
    27. {
    28. case 0: PORTB=0x01;PORTD=0x08;  break;
    29. case 1: PORTB=0x02;PORTD=0x04;  break;
    30. case 2: PORTB=0x04;PORTD=0x02;  break;
    31. case 3: PORTB=0x08;PORTD=0x01;  break;
    32. }
    33. }
    34. void main()
    35. {
    36.    
    37. TRISB=0x00;
    38. TRISD=0x00;
    39. char a,b;
    40.  
    41. while(1)
    42. {
    43. led_display(a);
    44.  
    45. a++;
    46. delay_sec(1);
    47. if(a>=4)
    48. {
    49. a--;
    50. }
    51.  
    52.  
    53. }
    54.  
    55. }
    56.  
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,362
    Your program flow logic is poor.

    Start by drawing a flow chart.
     
    ect_09 likes this.
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Code (Text):
    1. while(1)
    2. {
    3. led_display(a);
    4.  
    5. a++;
    6. delay_sec(1);
    7. if(a>=4)
    8. {
    9. a--;
    10. }
    11. }
    Instead of counting up to 3, count down from 3....
     
    ect_09 likes this.
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    run it in the software simulator
     
    ect_09 likes this.
  5. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i tried this but not working..
     
  6. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
    ect_09 likes this.
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ...and your code?
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,362
    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.
     
Loading...