Professional Documents
Culture Documents
CHASING LEDS
============
In this project 8 LEDs are connected to PORTC of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal.
The program turns ON the LEDs in an an clockwise manner with 1 s delay between
each output. The net result is that LEDs seem to be chasing each other.
**************************************************************************/
void main()
{
unsigned char J = 1;
ANSELC = 0; // Configure PORTC as digital
TRISC = 0; // Configure PORTC as output
for(;;) // Endless loop
{
PORTC = J; // Send J to PORTC
Delay_ms(1000); // Delay 1 s
J = J << 1; // Shi _ le _ J
if(J == 0) J = 1; // If last LED, move to first one
}
}
If you are using the EasyPIC V7 development board, then make sure that the following
jumper is configured:
DIP switch SW3: PORTC ON
In this project 8 LEDs are connected to PORTC of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal.
The program uses a pseudorandom number generator to generate a number between 0 and
32767. This number is then divided by 128 to limit it between 1 and 255. The resultant number
is sent to PORTC of the microcontroller. This process is repeated every second.
*****************************************************************************/
void main()
{
unsigned int p;
ANSELC = 0; // Configure PORTC as digital
TRISC = 0; // Configure PORTC as output
srand(10); // Ini alize random number seed
for(;;) // Endless loop
{
p = rand(); // Generate a random number
p = p / 128; // Number between 1 and 255
PORTC = p; // Send to PORTC
Delay_ms(1000); // 1 s delay
}
}