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

 

 

 

 
On the right is a basic C program for the Hi-Tech Compiler to blink the LED connected to pin 13 (C2).

The first line starts with a pound sign (#) and is a command to the C compiler to include the file contained in the angle brackets <>. In this case the file htc.h is included in the compilation process and provides access to information needed by the program.

The second line starts with 2 forward slashes // and indicates to the compiler that everything after (and including) the slashes is a comment and will not generate and code. The double slashes are actually a recent convention from C++ code but has been adopted in most C compilers. Standard C comments are contained between /* and */ as shown in lines 9 through 13.

 

#include <htc.H>
//---------------------------------------------
// file: blinkled.c
// description: First PicWiser project
// author: Emicros
// compiler: Hi-Tech
//---------------------------------------
/*---------------------------------------
** 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: This is the main function.
**--------------------------------------*/
void main( void )
{
   TRISC = 0b11111011;
   while( 1 )
   {
      PORTC = 0b00000100;
      _delay( 100000 );
      _delay( 100000 );
      PORTC = 0b00000000;
      _delay( 100000 );
      _delay( 100000 );
   }
}

The TimerISR() function is defined on line 14. This function is not used in this simple program but serves as a placeholder for now. Without defining this interrupt function, the Hi-Tech C Compiler will detect that the program can start executing immediately after reset and will not contain the correct reset vector preventing the bootloader from operating correctly. Note that keyword is inserted before the function name to indicate that this is a special function. More on interrupts later.

The meat of the program (more like a slice of ham in this simple program) starts on line 26 with the main function void main( void ) . Functions are to C what subroutines are to Basic. Functions execute code and are called from functions and can be passed data and can also return data. Functions will be expanded on as we develop more programs.

The main function in C is significant in that normally it is the first function that is executed after the C startup code is executed. In the case of a microcontroller running C, there is a certain amount of code that is executed from reset to initialize the processor to get it up and running. This startup code in most cases is hidden from the developer and basically just does some preliminary initialization. For what we are learning this isn't important but it's more for your information.

 

 

 
 
Copyright 2010 Embedded Micro Software. All rights reserved