.: EMICROS :.  Embedded Micro Software
Home CAN232 CANUSB I2C232 I2CUSB CanWiser Learn C
Add your text here
Contact

Email : info@emicros.com

Ordering Information

.: Ordering using our PayPal powered shopping cart (PayPal account not required).

CAN232 : $79
CANUSB : $89
I2C232 : $79

I2CUSB : $89

Click here to View Cart and check out.

 

 

 

 
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;
         }
   }
   }
}

 
 
Copyright 2010 Embedded Micro Software. All rights reserved