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

Thread Starter

grasshopper

Joined Apr 2, 2009
11
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?

Rich (BB code):
#include <pic.h>
/*  cPKLED.c - Roll Through PICkit 8 LEDs

This Program will roll through each of the 8 LEDs built
  into the PICkit PCB.  

The LED values are:

LED   Anode  Cathode
D0    RA4    RA5
D1    RA5    RA4
D2    RA4    RA2
D3    RA2    RA4
D4    RA5    RA2
D5    RA2    RA5
D6    RA2    RA1
D7    RA1    RA2

myke predko
04.09.10

*/

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);


int i, j, k;

main()
{

    PORTA = 0;
    CMCON0 = 7;                 //  Turn off Comparators
    ANSEL = 0;                  //  Turn off ADC

    k = 0;                      //  Start at LED 0

    while(1 == 1)               //  Loop Forever
    {
        for (i = 0; i < 255; i++)  //  Simple Delay Loop
            for (j = 0; j < 129; j++);

        switch (k) {            //  Select Which LED to Display
            case 0:
                PORTA = 0b010000;
                TRISA = 0b001111;
                break;
            case 1:
                PORTA = 0b100000;
                TRISA = 0b001111;
                break;
            case 2:
                PORTA = 0b010000;
                TRISA = 0b101011;
                break;
            case 3:
                PORTA = 0b000100;
                TRISA = 0b101011;
                break;
            case 4:
                PORTA = 0b100000;
                TRISA = 0b011011;
                break;
            case 5:
                PORTA = 0b000100;
                TRISA = 0b011011;
                break;
            case 6:
                PORTA = 0b000100;
                TRISA = 0b111001;
                break;
            case 7:
                PORTA = 0b000010;
                TRISA = 0b111001;
                break;
        }  //  hctiws

        k = (k + 1) % 8;        //  Increment k within range of 0-7

    }  //  elihw
}  //  End cPKLED
 

hgmjr

Joined Jan 28, 2005
9,027
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?

Rich (BB code):
#include <pic.h>
/*  cPKLED.c - Roll Through PICkit 8 LEDs
 
This Program will roll through each of the 8 LEDs built
  into the PICkit PCB.  
 
The LED values are:
 
LED   Anode  Cathode
D0    RA4    RA5
D1    RA5    RA4
D2    RA4    RA2
D3    RA2    RA4
D4    RA5    RA2
D5    RA2    RA5
D6    RA2    RA1
D7    RA1    RA2
 
myke predko
04.09.10
 
*/
 
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);
 
 
int i, j, k;
 
unsigned char direction;
 
main()
{
 
    PORTA = 0;
    CMCON0 = 7;                 //  Turn off Comparators
    ANSEL = 0;                  //  Turn off ADC
 
    k = 0;                      //  Start at LED 0
 
    while(1 == 1)               //  Loop Forever
    {
        for (i = 0; i < 255; i++)  //  Simple Delay Loop
            for (j = 0; j < 129; j++);
 
        switch (k) {            //  Select Which LED to Display
            case 0:
                PORTA = 0b010000;
                TRISA = 0b001111;
                break;
            case 1:
                PORTA = 0b100000;
                TRISA = 0b001111;
                break;
            case 2:
                PORTA = 0b010000;
                TRISA = 0b101011;
                break;
            case 3:
                PORTA = 0b000100;
                TRISA = 0b101011;
                break;
            case 4:
                PORTA = 0b100000;
                TRISA = 0b011011;
                break;
            case 5:
                PORTA = 0b000100;
                TRISA = 0b011011;
                break;
            case 6:
                PORTA = 0b000100;
                TRISA = 0b111001;
                break;
            case 7:
                PORTA = 0b000010;
                TRISA = 0b111001;
                // Flip the direction from increment to decrement
                //    or decrement to increment with operator "^".
                break;
        }  //  hctiws
 
        if (direction == increment)
       { 
             k = (k + 1) % 8;        //  Increment k within range of 0-7
        }
        else
        {
            k = --k & 7;  //  Another way to do modulo 8 on k...
         }
 
    }  //  elihw
}  //  End cPKLED
Take a look at the red markups I have added to you code....

hgmjr
 

Thread Starter

grasshopper

Joined Apr 2, 2009
11
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.
 
Top