| Let's change
the program to turn on just 1 LED at a time and
sequence through all 4 LEDs. In this case we need to
modify the program to do a bit shift on the data
contained in x. The statement x
<<= 1; is the
shorthand method for the left-hand shift operation. In
this case, the value in x is left shifted by 1. Notice
that instead of initializing x to 0 we have
initialized it to 0x10 so that the first time x is
written to PORTB, only pin RB4 is turned on. After the
delay, the value is left shifted and written to PORTB
again turning on only RB5.
The only problem with this program is that it turns
performs the sequence only once because when x is 0x80
and it is left shifted, the result is 0 so the LEDs
will turn off after one time through the sequence.
|
#include
<htc.h>
#include "delay.h"
//----------------------------------
// file: led4_4.c
// description: This is the C code
// for the 4 LED project
// author: Emicros
//----------------------------------
unsigned char x;
/*----------------------------------
** function: TimerISR
** description: This is just a place
** holder for now and isn't used
** in the program.
**--------------------------------*/
void interrupt TimerISR( void )
{
if( (TMR1IE)
&& (TMR1IF) )
{
TMR1IF
= 0;
}
}
/*----------------------------------
** function: main()
** description: Main function.
**---------------------------------*/
void main( void )
{
TRISB =
0x00;
x = 0x10;
while( 1
)
{
PORTB
= x;
delay_ms(
50 );
x
<<= 1;
}
} |