/*************************************************************************** Calibrate.c Martin Newell, 2005, San Jose, California Version 1, June 18, 2005 Calibrate PIC chips This is primarily for the PIC12F635 and 683 since they appear to need calibration, but can be used to recalibrate 12F629 and 675 which have calibration values that can be overwritten using a checkbox in Winpic's Options Pins 5,6,7 are toggled every 1 mS, for measuring on an oscilloscope Pins 2,3 are toggled every 0.25 second, for measuring with a stopwatch and an LED or analog voltmeter To calibrate a chip: Oscilloscope 1. Measure the period, P, of the rising edgeof pin 5,6 or 7 in microseconds. Should be about 2000 microseconds 2. Replace the return value from GetOsccal() with R + (P - 2000)/20, where R is the current return value. 5 bit 2s complement arithmetic. 3. Repeat from 1 until P = 2000 uS Stop watch and LED or analog voltmeter 1. Measure how many seconds, S, it takes for pin 2 or 3 to turn on 100 times. Should be about 50 seconds 2. Replace the return value from GetOsccal() with R + (S - 50)*2, where R is the current return value. 5 bit 2s complement arithmetic. 3. Repeat from 1 until S = 50 seconds The calibration value can be changed in the programmer code buffer and the program reloaded and tested without recompiling. ****************************************************************************/ //#define PIC12F629 #define PIC12F635 //#define PIC12F675 //#define PIC12F683 /****************************************************************************/ #ifdef PIC12F629 #pragma config = 0x3F84 #endif // PIC12F635 #ifdef PIC12F635 #pragma config = 0x33C4 #pragma bit IRCF0 @ OSCCON.4 // definitions missing from PIC12F635.h #pragma bit IRCF1 @ OSCCON.5 #pragma bit IRCF2 @ OSCCON.6 #endif #ifdef PIC12F675 #pragma config = 0x3F84 #define HAS_AD #endif // PIC12F635 #ifdef PIC12F683 #pragma config = 0x33C4 #pragma bit IRCF0 @ OSCCON.4 // definitions missing from PIC12F635.h #pragma bit IRCF1 @ OSCCON.5 #pragma bit IRCF2 @ OSCCON.6 #define HAS_AD #endif // Chip Configuration parameters // Oscillator Internal // Watchdog Off // Power Up Timer On // MCLR Internal // Brown Out Off // Code Protect Off // Data EE Read Protect Off char GetOsccal(); /************************************** MAIN **************************************/ void main() { char i,j; char t; #ifdef PIC12F629 OSCCAL = GetOsccal(); // Set oscillator calibration #endif // PIC12F635 #ifdef PIC12F635 IRCF0 = 0; IRCF1 = 1; IRCF2 = 1; // Select 4MHz OSCTUNE = GetOsccal(); #endif #ifdef PIC12F675 OSCCAL = GetOsccal(); // Set oscillator calibration #endif // PIC12F635 #ifdef PIC12F683 IRCF0 = 0; IRCF1 = 1; IRCF2 = 1; // Select 4MHz OSCTUNE = GetOsccal(); #endif TRISIO = 8; // Input on GP3. 0 = Output, 1 = Input OPTION = 8; // Assign prescaler to WDT, so TMR0 clock is 1:1 CM2 = 1; CM1 = 1; CM0 = 1; //These turn the input pins back to digital I/O #ifdef HAS_AD ANSEL = 0; //Turn off ADC #endif #pragma update_RP 0 GPIO = 0x00; #define COUNT1 250 #define COUNT2 197 // Following inner loop takes T2 = 15 + COUNT2*5 = 1000 i.e. 2 mS per cycle // Outer loop takes T1 = T2 * COUNT1 = 250 mS i.e. 2 on-off cycles per second #asm a001 MOVLW COUNT1 MOVWF i GOTO a002 b001 NOP NOP NOP NOP NOP NOP NOP a002 NOP NOP MOVLW COUNT2 MOVWF j b002 NOP NOP DECFSZ j,1 GOTO b002 MOVLW .7 XORWF GPIO,1 ; Toggle pins 5,6,7 every 1 mS DECFSZ i,1 GOTO b001 MOVLW .48 XORWF GPIO,1 ; Toggle pins 2,3 every 0.25 second GOTO a001 #endasm } // main /************************************** GetOsccal **************************************/ #ifdef PIC12F683 #pragma origin 0x7FF #else #pragma origin 0x3FF #endif // This is to enable us to create a call to the oscillator calibration value in 12F629 // On 12F629 this is a dummy - actual procedure is preset in chip and will normally override // On 12F635 this procedure runs and returns value shown - need to calibrate manually // OSCCAL is 5 bit, 2's complement, i.e. -16 to +15, or 0x10 to 0xF // A change of +1 causes about a +1% change in speed (i.e. reduction in cycle time) // No need to recompile to change value - can set the value in WinPic's buffer and rewrite the PIC // Can overwrite calibration on 12F629 by checking box in Options panel of WinPic char GetOsccal() {return 0x00;} /***************************************** End *****************************************/