Timer0 accuracy setting using c code
Posted: Sat Sep 26, 2015 11:40 pm
Hello, I'm relatively inexperienced in programming and i need your help.
Project data :
1. 4MHz internal clock
2. PIC16f628A
I want to produce a square wave, with duty cycle 50%, using timer0. The square wave period is equal to 827usec and consequently the half period is approximately 413usec. Timer0 hasn't rollover option for maximum control and accuracy such as Timer2 but if i can write in C code, the required accuracy is possible, right?.
The problem is that i measure always 540usec half period , irrespective of changes. I can't understand it.
The general idea is that with prescaller of 1:1 (PSA=1) and start value for tmr0=99 i would control the interrupt with accuracy.
This the code for Timer0
// Interrupt
// Interrupt: Enable TMR0
cr_bit(option_reg,T0CS);
cr_bit(option_reg,T0SE);
//Prescaler 1:1
st_bit(option_reg,PSA);
option_reg = (option_reg & 0xF0) | 0x08;
st_bit(intcon,GIE);
st_bit(intcon, T0IE);
// clear Interrupt flag
clear_bit(intcon,T0IF);
// set value (413-256=157)usec, (256-157=99) usec
tmr0=99;
I would be thankful for your feedback.
Project data :
1. 4MHz internal clock
2. PIC16f628A
I want to produce a square wave, with duty cycle 50%, using timer0. The square wave period is equal to 827usec and consequently the half period is approximately 413usec. Timer0 hasn't rollover option for maximum control and accuracy such as Timer2 but if i can write in C code, the required accuracy is possible, right?.
The problem is that i measure always 540usec half period , irrespective of changes. I can't understand it.
The general idea is that with prescaller of 1:1 (PSA=1) and start value for tmr0=99 i would control the interrupt with accuracy.
This the code for Timer0
// Interrupt
// Interrupt: Enable TMR0
cr_bit(option_reg,T0CS);
cr_bit(option_reg,T0SE);
//Prescaler 1:1
st_bit(option_reg,PSA);
option_reg = (option_reg & 0xF0) | 0x08;
st_bit(intcon,GIE);
st_bit(intcon, T0IE);
// clear Interrupt flag
clear_bit(intcon,T0IF);
// set value (413-256=157)usec, (256-157=99) usec
tmr0=99;
I would be thankful for your feedback.