PIC Programming Notes

I've been fooling around a little bit with PIC programming. PIC microcontrollers are a favorite tool for a lot of hams to use in projects. The PIC ICs are produced by Microchip. There are a number of free tools available from Microchip, I'm currently using their MPLAB IDE (version 8.66) on my Windows 7 PC. I've run into a few issues along the way, so I thought I would share them.

123 PIC Microcontroller Experiments for the Evil Genius by Myke Predko, , McGraw-Hill, New York, 2005.

I started out by buying the Microchip PICkit1 Flash Starter Kit. I didn't really do much with it until I bought the book "123 PIC Microcontroller Experiments for the Evil Genius" by Myke Predko. The book was very helpful and the examples in the book all seemed to work with the version of MPLAB IDE that I had at the time. I moved on to playing with the Arduino a couple of years ago.

Unfortunately Microchip has now changed the defines in the headers files for the microcontrollers used in the book (16F684 and 12F675). This means that to run any of the example programs in the book you will need to change some of the defines. The defines are found in the C:\Program Files (x86)\HI-TECH Software\PICC\9.81\include directory on my PC (Windows 7 64 bit). Look for the file "pic16f684.h", it contains all the defines for the 16F684.

Here are the changes needed for the example (Flash.c) in the Introduction of the book:

Replace the line that begins with __config with:

/*
Correcting the config bits
config switches:
FCMEN_OFF  (old define: FCMDIS - Fail-Safe Clock disabled
IESO_OFF   (old define: IESODIS    :  Internal External Switchover mode is disabled
BOREN_OFF  (old define: BORDIS)  - Brown Out Reset Disabled
CP_OFF     (old define: UNPROTECT)  - Don't set the binary code read protect switch
MCLRE_OFF  (old define: MCLRDIS)  - master clear reset funcgtion disabled
PWRTE_ON   (old define: PWRTEN)  - power up timer enabled
WDTE_OFF   (old define: WDTDIS)  - watchdog time disabled
FOSC_INTOSCIO (old define: INTIO)  - internal oscillator/GP4 = IO//GP5 = IO

*/
__CONFIG(FCMEN_OFF & IESO_OFF & BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSCIO);

If you bought the book, the source code for all the examples is on the McGraw-Hill Professional web site here

One of the best suggestions in the book is that you install a ZIF (zero insertion force) socket in the breadboard area of the PICkit1 board and wire it in parallel with the evaluation socket. This really makes using the board as a programmer much easier. I highly recommend you do this.

In addition to the defines changing, Microchip has cancelled the PICkit1 Flash Starter Kit. This makes the "123 PIC Microcontroller Experiments for the Evil Genius" book less useful. So if you don't have the PICkit1, then think twice about buying this book.

HI.c

I have not written anything useful with my PICkit1. It's just a toy for me. But I did write this program that runs on the PICkit1 board using a 12F629 chip. Here is the source code:

#include <pic.h>
/*
HI.c for 12F629 by Bill Thompson
Flashes Morse code HI on LED D0 of PICkit1 board.  Outputs tone on GPIO0 (pin 7).

config switches:
BOREN_OFF  (old define: BORDIS)  - Brown Out Reset Disabled
CP_OFF     (old define: UNPROTECT)  - Don't set the binary code read protect switch
MCLRE_OFF  (old define: MCLRDIS)  - master clear reset funcgtion disabled
PWRTE_ON   (old define: PWRTEN)  - power up timer enabled
WDTE_OFF   (old define: WDTDIS)  - watchdog time disabled
FOSC_INTRCIO (old define: INTIO)  - internal oscillator/GP4 = IO//GP5 = IO

*/
__CONFIG( BOREN_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTRCIO);

 
int i,k;
const int dur = 200;
void waitloop(int j)
{
	
	for (i=0; i< j; i++)
	{
		for(k=0; k<25;k++); //see how much delay this adds.  k<25 results in 800 usec, 1250 Hz
		if(GPIO4)
		{
		GPIO0 = GPIO0 ^ 1;  //by itself results in square wave with period of 40 microseconds, 25 KHz Square Wave
		}
		//NOP();//10	//  add 10 NOP()'s now takes 60 msec, 16.6666 KHz	
	}
}

main()
{
	GPIO = 0;
	CMCON = 7;
	//ANSEL = 0;
	TRISIO0 = 0;  //was 	TRIS0 for old complier
	TRISIO1 = 1;
	TRISIO2 = 1;
	TRISIO4 = 0;
	TRISIO5 = 0;
	while (1== 1)
	{
		GPIO4 = 1; //D0 on
	
		waitloop(dur);
	
		GPIO4 = 0;  //D0 off

		waitloop(dur);
		GPIO4 = 1;  //D0 on
	
		waitloop(dur);
	
		GPIO4 = 0;  //D0 off

		waitloop(dur);        
		GPIO4 = 1;  //D0 on
	
		waitloop(dur);
	
		GPIO4 = 0;  //D0 off

		waitloop(dur);        
		GPIO4 = 1;  //D0 on
	
		waitloop(dur);
	
		GPIO4 = 0;  //D0 off, ends the H

		waitloop(3*dur);     //three element spaces   		

		GPIO4 = 1;  //D0 on, starts the I
	
		waitloop(dur);
	
		GPIO4 = 0;  //D0 off

		waitloop(dur);        
		GPIO4 = 1;  //D0 on
	
		waitloop(dur);
	
		GPIO4 = 0;  //D0 off ends the I

		waitloop(20*dur);      // 20 element spaces
	
	}
}
Creative Commons License
HI.c by Bill Thompson is licensed under a Creative Commons Attribution 3.0 Unported License.
 Return to my home page