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.
Timer0 accuracy setting using c code
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Timer0 accuracy setting using c code
Hi fairbrain,
I'm moving your post to a more appropriate section.
I can't see where you have got your output to toggle within ISR?
You will need an output to produce a square wave.
How have you been measuring a square wave?
Martin
I'm moving your post to a more appropriate section.
I can't see where you have got your output to toggle within ISR?
You will need an output to produce a square wave.
How have you been measuring a square wave?
Martin
Martin
Re: Timer0 accuracy setting using c code
Hello Medelec35 and thank you very much.
Indeed, i measure a square wave from PORT A3.
(Port A2 also gives me exactly the same period, about 540usec controlled by the Timer2.)
First enable tmr2 and then tmr0.
Normally with INT icon - no c code icon - i have set TIMER0 to call an assigned macro.
Now and after your reply i notice, too, that i have left out to call this macro in c code icon.
The question now is how can i call a macro in c code.
Thank you again
Indeed, i measure a square wave from PORT A3.
(Port A2 also gives me exactly the same period, about 540usec controlled by the Timer2.)
First enable tmr2 and then tmr0.
Normally with INT icon - no c code icon - i have set TIMER0 to call an assigned macro.
Now and after your reply i notice, too, that i have left out to call this macro in c code icon.
The question now is how can i call a macro in c code.
Thank you again