Hi, I'm trying to program a PIC16C71 to generate a given frequency at the output. I'm using the 71's timer overflow interrupt to generate the pulse. I've been doing some tests using the C2C plus 4.004e compiler's wizard. But everytime I put the PIC on I get a different frequency, about 1 time on 3 I get the correct one, but most of the time the frequency is too high. The 4Mhz crystal of the put is constant so it does not cause the problem. Here's how the PIC should work : I'm using the internal clock of the PIC with a prescaler 2. On timer overflow interrupt a function is called (tmrHandler) switch portB 1 to high or to low if it's already high. I did some calculation and figured I should get a 976.56 Hertz signal at the output : (1/(1/(4 000 000 / 4) * prescaler * 256))/2 (256 is the overflow count) (the result is divided by 2 because the output is toggled On and Off each time the function is called) (1/(1/1000000 * 2 * 256))/2 = 976.56 Hertz or 1000000/2/256/2 = 976.56 Hertz When I turn the PIC on sometimes I get 978 Hertz but sometimes I get 7.5KHz, 4xx Hz, 8.xKHz etc... I really do not understand why I get this result, I'm using a new 9volt battery to drive the circuit (with a 78x05 regulator) I tried to add an 1000uF capacitor at the regulator without any results. Could my PIC be damaged ? Something is wrong with my code ? Thanks in advance ! Here's the C2C testing code : ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- ------- //This file was generated by the C2G Code Generator 1.1 //If necessary insert your global variables declaration here char toggle = 1; //TMR0 Overflow handler void tmrHandler( void ) { if (toggle) { output_low_port_b (1); toggle = 0; } else { output_high_port_b (1); toggle = 1; } } //This is the interrupt handler void interrupt( void ) { //Insert your code which should run //before any interrupt processing here //Interrupt processing //TMR0 Overflow processing if( INTCON & 4 ) { clear_bit( INTCON, 2 ); tmrHandler(); } //Insert your code which should run //after any interrupt processing here } //This is the main function which is called //after processor power-up or reset void main( void ) { //Hardware Initialization set_bit( STATUS, RP0 ); //Select the Register bank 1 set_tris_a( 1 ); //Configure the Port A set_tris_b( 0 ); //Configure the Port B OPTION_REG = 128; //Configure the OPTION register clear_bit( STATUS, RP0 ); //Select the Register bank 0 INTCON = 160; //Configure the INTCON register //Don't forget to insert your code here!!! //(As there is no "return" from the main code //an endless loop may be a good idea) } ---------------------------------------------------------------------------- --------------------------------- Here's the compiled code : ---------------------------------------------------------------------------- --------------------------------- ; This file was generated by C2C-plus compiler version 4.00.4e include "p16C71.inc" ;Variables ***************************************** __int_save_cont_W equ 0x0c __int_save_cont_STATUS equ 0x0d __int_save_cont_FSR equ 0x0e __int_save_cont_PCLATH equ 0x0f _toggle equ 0x10 ORG 0 goto start__code ORG 4 _interrupt _interrupt__code movwf __int_save_cont_W swapf __int_save_cont_W, F swapf STATUS, W movwf __int_save_cont_STATUS swapf FSR, W movwf __int_save_cont_FSR swapf PCLATH, W movwf __int_save_cont_PCLATH btfss INTCON, 2 goto label_0002 bcf INTCON, D'2' call _tmrHandler label_0002 swapf __int_save_cont_PCLATH, W movwf PCLATH swapf __int_save_cont_FSR, W movwf FSR swapf __int_save_cont_STATUS, W movwf STATUS swapf __int_save_cont_W, W retfie _interrupt__end start__code movlw D'1' movwf _toggle _main__code bsf STATUS, RP0 movlw D'1' movwf TRISA movlw D'0' movwf TRISB movlw D'128' movwf OPTION_REG bcf STATUS, RP0 movlw D'160' movwf INTCON _main__end _tmrHandler__code _tmrHandler movf _toggle, W btfsc STATUS, Z goto label_0000 bcf PORTB, 1 clrf _toggle goto label_0001 label_0000 bsf PORTB, 1 movlw D'1' movwf _toggle label_0001 return _tmrHandler__end END ---------------------------------------------------------------------------- --------------------------------- -- http://www.piclist.com hint: PICList Posts must start with ONE topic: "[PIC]:" PIC only "[EE]:" engineering "[OT]:" off topic "[AD]:" ad's