Myke Predko's 123 PIC Microcontroller Experiments for the Evil Genius

Discussion in 'Embedded Systems and Microcontrollers' started by grasshopper, Apr 16, 2009.

  1. grasshopper

    Thread Starter Member

    Apr 2, 2009
    11
    0
    Hi Guys,

    I am stuck on experiment 18 in this book. Posted below is the code. Basically the issue I am having is that now I want to go in reverse order once after LED 7 lights up. I don't know what to say, but I am stumped. I understood how k was able to increment and thus change the case order, but I am having trouble figuring out a formula for doing the reverse. I don't really want people giving me the answer straight up, but can someone please point me in the right direction?

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. /*  cPKLED.c - Roll Through PICkit 8 LEDs
    4.  
    5. This Program will roll through each of the 8 LEDs built
    6.   into the PICkit PCB.  
    7.  
    8. The LED values are:
    9.  
    10. LED   Anode  Cathode
    11. D0    RA4    RA5
    12. D1    RA5    RA4
    13. D2    RA4    RA2
    14. D3    RA2    RA4
    15. D4    RA5    RA2
    16. D5    RA2    RA5
    17. D6    RA2    RA1
    18. D7    RA1    RA2
    19.  
    20. myke predko
    21. 04.09.10
    22.  
    23. */
    24.  
    25. __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
    26.   & UNPROTECT & BORDIS & IESODIS & FCMDIS);
    27.  
    28.  
    29. int i, j, k;
    30.  
    31. main()
    32. {
    33.  
    34.     PORTA = 0;
    35.     CMCON0 = 7;                 //  Turn off Comparators
    36.     ANSEL = 0;                  //  Turn off ADC
    37.  
    38.     k = 0;                      //  Start at LED 0
    39.  
    40.     while(1 == 1)               //  Loop Forever
    41.     {
    42.         for (i = 0; i < 255; i++)  //  Simple Delay Loop
    43.             for (j = 0; j < 129; j++);
    44.  
    45.         switch (k) {            //  Select Which LED to Display
    46.             case 0:
    47.                 PORTA = 0b010000;
    48.                 TRISA = 0b001111;
    49.                 break;
    50.             case 1:
    51.                 PORTA = 0b100000;
    52.                 TRISA = 0b001111;
    53.                 break;
    54.             case 2:
    55.                 PORTA = 0b010000;
    56.                 TRISA = 0b101011;
    57.                 break;
    58.             case 3:
    59.                 PORTA = 0b000100;
    60.                 TRISA = 0b101011;
    61.                 break;
    62.             case 4:
    63.                 PORTA = 0b100000;
    64.                 TRISA = 0b011011;
    65.                 break;
    66.             case 5:
    67.                 PORTA = 0b000100;
    68.                 TRISA = 0b011011;
    69.                 break;
    70.             case 6:
    71.                 PORTA = 0b000100;
    72.                 TRISA = 0b111001;
    73.                 break;
    74.             case 7:
    75.                 PORTA = 0b000010;
    76.                 TRISA = 0b111001;
    77.                 break;
    78.         }  //  hctiws
    79.  
    80.         k = (k + 1) % 8;        //  Increment k within range of 0-7
    81.  
    82.     }  //  elihw
    83. }  //  End cPKLED
    84.  
    85.  
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Take a look at the red markups I have added to you code....

    hgmjr
     
  3. grasshopper

    Thread Starter Member

    Apr 2, 2009
    11
    0
    Thanks to you help, hgmjr, I was finally able to figure it out... it was really simple actually:

    I created a new variable called direction, and used it as a flag.

    I set the direction = 0 when case 7 was reached.

    and so when direction == 0, it counts backward k--.

    Likewise when case 0 is reached, i have direction = 1

    when direction == 1 then the counts are forward k++.

    _______________________________________________

    I didn't have to use the k = --k & 7 expression that you gave me, but once I figured out how that worked, it was a really cool way to count backwards and repeat, if I wasn't able to use flags. Thanks again.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I am pleased that I was able to shed even a little light on your programming challenge.

    hgmjr
     
Loading...