Luis F
Joined: 25 Apr 2006 Posts: 2 Location: Venezuela
|
Timer1 overflow and counter problem |
Posted: Tue Apr 25, 2006 7:19 am |
|
|
I have a simple circuit wuith a 12F675 like a 300Hz. pulse generator, a LCD and a PIC16F688 like counter.
I have this program code in PIC16F688 for counter 500 pulses and stop the pulse generator (MCLR of 12F675) and show the counter in a LCD.
The problem is:
The counter do not stop at 500 pulses, its answer is erratic, sometimes is 545, other is 525, ...but, if I erase all program lines with delay_ms and replace this for a 'for cycle' it work good.
I use another PIC like counter, 18F252 and is the same.
Some ideas?
why delay routine affects timer1 overflow interrupt?
Which is my error?
#include <16F688.h>
#device adc=8
#use delay(clock=8000000)
#fuses NOWDT,INTRC_IO, NOPROTECT, BROWNOUT, NOMCLR, NOCPD, NOPUT, IESO, FCMEN
#use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8)
#define Led pin_A0
#define Stop pin_A1
int16 Counter;
#int_TIMER1
TIMER1_isr()
{output_low(Stop); //Reset 12F675 (300 Hz generator)
delay_ms(100);
output_high(Stop); //Enable 12F675
delay_ms(100);
Set_Timer1(65036); //Set timer1 for overflow interrupts after 500 Pulses
}
void main()
{setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_EXTERNAL_SYNC);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator( OSC_8MHZ );
output_high(Stop); //Enable 12F675
delay_ms(1000);
Set_Timer1(65036); //Set timer1 for overflow interrupts after 500 Pulses
putchar(254); // LCD command
putchar(254);
putchar(128);
printf("(Active)"); //LCD Ok
enable_interrupts(INT_TIMER1);
enable_interrupts(global);
while(True)
{output_low(Led);
delay_ms(300);
output_high(Led);
delay_ms(300);
Counter= get_timer1();
putchar(254); // LCD command
putchar(192);
printf("%lu", Counter); //Show in LCD Counter Value
}
} |
|