PIC 101d (intro to C)

xxalexx at ix.netcom.com xxalexx at ix.netcom.com
Sat Jun 27 14:07:17 GMT 1998


Intro to C using a PIC

note if you want to try CCS C examples you need to make 
some changes to code in the #preprocesor statements, depending
on the chip you are using.

#1
cut and paste the following code into notepad or other text editor  
save file as ex1.c in your picc\project directory. 

// ex1.c PIC C example
// slashes are used for comments.
// anything on a line after them will not be used in program.
// this is a comment

// the first lines in program will be #preprocesing statements 
// specfic to the PIC micro they start with a # key
// #include<filename > 
// this will include the code in the filename in your code
// you can use paths such as #include <c:\picc\examples\16F84.h>
//especially if you get compile error cannot find include file 

#include<16F84.h> //this will include the code in file 16F84.h 

// The next preprocessor statement will be a #fuses for programmer
// we will set the oscillator type as crystal=XT
// if not used in high risk area we do not need a watchdog NOWDT 
//note: very important!
// since this is a open mailing list no code protection=NOPROTECT
//this will allow reverse enginneering and you can program the chip 
//again 

#fuses XT, NOWDT, NOPROTECT

// The next preprocessor statement will be a #use for delay timing.
// This will generate a timing loop function in the assembly 
//object code based on the oscillator clock frequecncy we are using.

#use delay(clock = 4000000) //uses a delay for a 4MHz oscillator 

// The next preprocessor statement will be a #use for rs232
// This will set baud rate, define recieve and transmit pins, and 
//invert the logic signals.
// 1 start bit,  8 data bits, no parity bit  and 1 stop bit
// PIN_A0 is defined as port A bit 0 which is pin 17 on 16F84 
// PIN_A1 is defined as port A bit 1 which is pin 18 on 16F84
// these are the pins you have connected to your 9 pin cable
// pin 17 port A0 xmits to PC.   pin 18 port A1 rcvs from PC 
// you are free to use other pins

#use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_A1, invert)

//next the program will start in the function called main()
//the code of this function is included in start{
// and end brackets}
// all C programs must have a function called main()
 
main() //function main defines start of program
{ //bracket defines start of program code body
	
	// many times you will want program to run forever
	// untill ignition switch turned off
	// this can be done with a while loop statement  
	// while( value is more than 0  )
	// {
		// keep on doing something
         // }		
	// in the include file 16F84.h there is a preprocessor 
	// #define statement that defines the word TRUE as the number 1	
	
	while(TRUE) // this statement will start infinite loop
	{  //start while loop

		//the next code line will print a message on screen
		// the printf() function generates the assembly code
		// to send a ASCII coded  byte to rs232 transmit pin.
		//enclose anything you want to print in quotes
		//    \n is return \r is a line feed
		// note that this function requires a ; to end it

		printf("Hello Fuel Inj.\n\r");

	} // end while loop 			

}//end main program


	 
	



More information about the Diy_efi mailing list