// Interrupt Handler
void interrupt()
{
if (INTCON.TMR0IF ==1) // timer 0 interrupt flag
{
PORTB.B0 = ~PORTB.B0; // Toggle PORTB bit0 LED
TMR0 = 131; // reset the timer with preset value
INTCON.TMR0IF = 0; // clear the flag
}
}
void main()
{
// setup portb to show the interrupts by blinking LEDs
TRISB = 0x00;
TRISC = 0x00;
PORTB = 0;
PORTC = 0 ;
OPTION_REG.T0CS = 0; // bit 5 TMR0 is used as timer
OPTION_REG.T0SE = 0; // bit 4 TMR0 changes with rising edge
OPTION_REG.PSA = 0; // bit 3 Prescaler is assigned to tmr0
OPTION_REG.PS2 = 0; // bits 2-0 PS2:PS0: Prescaler Rate Select bits the
OPTION_REG.PS1 = 1; // rate of prescaler here is 1:256
OPTION_REG.PS0 = 0;
TMR0 = 131; // preset for timer0 register
// Configuring Interrupt Control Registers INTCON
INTCON = 0; // clear the interrpt control register
INTCON.TMR0IE = 1; // bit5 TMR0 Overflow Interrupt Enable bit...1 = Enables the TMR0 interrupt
INTCON.GIE = 1; // bit7 global interrupt enable
INTCON.TMR0IF = 0; // bit2 clear timer 0 interrupt flag
while(1) //endless loop
{
PORTC = ~ PORTC;
delay_ms(10);
}
}
HS oscillator (note: everything i simulated, I did not connect real crystal osc to the micro in reality yet.)Hi,
What clock source for PIC?
What clock frequency for PIC?
sorry, it is supposed to be the second try, but I forgot to comment the line containing delay of portc. but this makes no difference, the main problem is that interrupt of timer does not come in fixed time intervals.Is given code for your first or second try?
I can't find why both RB0 clock and Portc clock should synchronize.
Portc clock should be about 50Hz. It is not very precise, because it uses busy waits.
They will not synchronize because the 'delay_ms(10)' is a software generated delay, a loop to waste time. The loop will be broken each time the ISR code is executed so each TMR0 interrupt will slightly lengthen the delay. To make it exact you would have to stop TMR0 as you entered the software delay and restart it afterwards.
The actual TMR0 delay is impossible to tell without knowing your clock frequency.
Brian.
// Interrupt Handler
void interrupt()
{
if (INTCON.TMR0IF == 1) // timer 0 interrupt flag
{
INTCON.TMR0IF = 0; // clear the flag
TMR0 = 131; // reset the timer with preset value
PORTB.B0 = ~PORTB.B0; // Toggle PORTB bit0 LED
}
}
As you are using mikroC Compiler use mikroe Timer Calculator tool to generate Timer interrupt code.
//Timer0
//Prescaler 1:4; TMR0 Preload = 6; Actual Interrupt Time : 1 ms
//Place/Copy this part in declaration section
void InitTimer0(){
OPTION_REG = 0x81;
TMR0 = 6;
INTCON = 0xA0;
}
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0 = 6;
PORTB.B0 = ~PORTB.B0;
}
}
void main()
{
// setup portb to show the interrupts by blinking LEDs
TRISB = 0x00;
TRISC = 0x00;
PORTB = 0;
PORTC = 0 ;
InitTimer0();
while(1) //endless loop
{
}
}
You actually have the prescaler set to 1:8, not 1:256 as commented in the program. By my calculation, the interrupts should be every 1.096mS so your 10mS software delay probably gets interrupted 9 times every time it executes. I would expect the 10mS delay to vary slightly but the interrupts should accurate.
If TMR0 really is speeding up by itself it can only be an electrical problem or the value being loaded into it is getting higher.
Try this, no promises but worth a try:
Code:// Interrupt Handler void interrupt() { if (INTCON.TMR0IF == 1) // timer 0 interrupt flag { INTCON.TMR0IF = 0; // clear the flag TMR0 = 131; // reset the timer with preset value PORTB.B0 = ~PORTB.B0; // Toggle PORTB bit0 LED } }
All I have done is rearranged the lines so the flag is cleared and the timer reloaded sooner.
If the 50Hz has to be accurate, generate it by ISR, not by using software delays. You might also see RMW problems on both ports.
Brian.
TMR0 = 131 - TMR0;
Don´t use the first timestamp as absolute value. This has a liitle deviation to the others because of the initialisation routine at startup.still the gap is getting bigger with the time,
I´m not familiar with PICs. But isn´t there a timer/counter mode that creates PWM/pulses/interrupts withoutthe needfor the software to set the tmr0 value or limits every time a new?The small increase in the timer ISR timing is expected.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?