Timer0 accuracy setting using c code

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Posts: 2
Joined: Sat Sep 26, 2015 10:40 pm

Timer0 accuracy setting using c code

Postby fairbrain » 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
//Prescaler 1:1

option_reg = (option_reg & 0xF0) | 0x08;
st_bit(intcon, T0IE);
// clear Interrupt flag
// set value (413-256=157)usec, (256-157=99) usec

I would be thankful for your feedback.

User avatar
Valued Contributor
Valued Contributor
Posts: 8813
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2559 times
Been thanked: 3714 times

Re: Timer0 accuracy setting using c code

Postby medelec35 » Sun Sep 27, 2015 9:23 pm

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?

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Posts: 2
Joined: Sat Sep 26, 2015 10:40 pm

Re: Timer0 accuracy setting using c code

Postby fairbrain » Sun Sep 27, 2015 11:15 pm

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