| This brings
us to a new topic, the if
statement.
The if statement allows the program to test for
certain conditions and adjust variables or program
execution accordingly. In the above example, the
sequence was executed once but was required to run
continuously. We need to add an if
statement to test for x equal to 0 and then set x to
0x10 if this is true. This can be seen in the modified
program on the right.
Problem: As a quick check to see
if you understand this, modify the program to change
the direction to start with RB7 and sequence from RB7
to RB4 and then repeat. The answer is shown in the
next section (don't peek until you've tried it).
|
#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;
if(
x == 0 )
{
x
= 0x10;
}
}
} |