/*This program tells a series of LEDs in PORTD to flash (by pressing Push Button RA4) one by one in forward direction, from RD0 to RD7.*/
/*When it reaches RD7, backward direction executed from RD7 to RD0 again. When PB RA4 is pressed again, the whole system stop. */
#include <p18f4580.h>
#include <delays.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
int i, iKey, iPrevious;
unsigned char cShift=1; /*Declaring shift bit interval*/
unsigned char cPBState=1; /*State of the Push Button*/
void vFlashing(void); /*Declaring flashing mode1*/
/*Flashing Mode*/
void vFlashing(void)
{
if (cShift==15) /*Count cShift to 15x*/
{
cShift=1; /*Restarting cShift value*/
}
if (cShift<8) /*When cShift smaller than 8*/
{
LATD=0b00000001<<cShift; /*Forward flash*/
Delay10KTCYx(50); /*Delays*/
cShift++; /*Increment cShift*/
}
else /*When cShift bigger than 8*/
{
LATD=0b10000000>>(cShift-7); /*Backward flash*/
Delay10KTCYx(50); /*Delays*/
cShift++; /*Increment cShift*/
}
}
/*Main Function*/
void main(void)
{
/*Settings*/
TRISD=0b00000000; /*Declaring all PORTD as an Output Port*/
TRISA=0b00010000; /*Declaring RA4 as an Input Port*/
/*Initializing*/
iKey=0;
cPBState=1;
while(1) /*Loop Forever*/
{
iPrevious=iKey; /*Copy the last value*/
iKey=PORTAbits.RA4; /*Assigning new value*/
/*Push Button RA4 Function ON/OFF Button*/
if(iKey==0 && iPrevious==1) /*Getting information*/
{
if(cPBState==0) /*Changing the PBState*/
{
cPBState=1;
}
else
{
cPBState=0;
}
}
switch (cPBState) /*Switch function for cases*/
{
case 0:
vFlashing(); /*If case 0, do flashing1*/
break;
case 1: /*If case 1, do nothing*/
break;
default:
break;
}
}
}