.: 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.

 

 

 

 
So far the programs haven't dealt with any variable data. It's been very simple stuff but we need to introduce some variable data to make programs useful. Expanding on the simple 4 LED program, let's introduce a variable x and change it in the while loop and write it to the LEDs.

In line 9 we introduce a variable called x that is assigned the type of unsigned char. This makes it an 8 bit variable ranging from 0 to 255.

A good habit to get into is to make sure you initialize variables at the start of the program. Variable x is initialized to 0 just before the while loop in the program shown on the right.

In the while loop we write the value stored in x to PORTB, delay 50 millisecond, and then change the value in x by adding 0x10 to it. The 0x10 is hexidecimal notation and allows the program to increment through the upper 4 bits of PORTB without any pauses.

 

#include <htc.h>
#include "delay.h"
//----------------------------------
// file: led4_3.c
// description: This is the C code 
// for the second 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 = 0;
   while( 1 )
   {
      PORTB = x;
      delay_ms( 50 );
      x = x + 0x10;
   }
}

 
 
Copyright 2010 Embedded Micro Software. All rights reserved