| The pullup
resistor places VCC on RB1 which is read as a 1 using
the input( PIN_B1 ) statement in the modified program
on the right. When the switch is pushed, RB1 is
connected to ground and the RB1 will be 0 so the if
statement if( RB1 == 0 ) will be true and the LEDs
will sequence.
Notice the modification to the TRISB statement.
This was changed so that RB1 is initialized as an
input.
Problem: What if you wanted the LEDs
to sequence and then stop if you pressed the switch?
Try this out. |
#include
<htc.h>
#include "delay.h"
//----------------------------------
// file: led4_sw.c
// description: This is the C code
// for the 4 LED & switch 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 =
0x02;
x =
0x80;
while( 1
)
{
if( RB1 == 0 )
{
PORTB
= x;
delay_ms(
50 );
x
>>= 1;
if(
x == 0x08 )
{
x
= 0x80;
}
}
}
} |