mamech
Full Member level 3
- Joined
- Nov 9, 2010
- Messages
- 176
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,296
- Activity points
- 3,135
hello
I am trying to make timer application using timer0. I made RB0 pin to toggle every 1 second using tmr0 interrupt, and I made in main function another delay for 1 second to toggle portc .
I expected that they will be synchronized together, but I got strange behaviour in simulator.
I found that in beginning , nearly there is no difference, but as the time goes, I notice that there is a time gap between RB0 and portc, and this time gap increases with time.
I used oshon and proteus, and I found that this happen in both.
in another code, I tried to use tmr0 alone on oshon with time period 1ms. and I found that the interrupt of timer in the beginning comes at 1.030 ms (the 0.03 error is not my issue here). I expected that next interrupts will happen at 2.06 ms , 3.09 ms , 4.12 ms ,etc.... , but this was not what I found on simulator. I found actual values less than expected (second interrupt happened at 2.042, not at 2.06). I found that the initial delay of 1.03 ms decreases with time. and I do not know why....
what is the reason of this weird behaviour??
I am trying to make timer application using timer0. I made RB0 pin to toggle every 1 second using tmr0 interrupt, and I made in main function another delay for 1 second to toggle portc .
I expected that they will be synchronized together, but I got strange behaviour in simulator.
I found that in beginning , nearly there is no difference, but as the time goes, I notice that there is a time gap between RB0 and portc, and this time gap increases with time.
I used oshon and proteus, and I found that this happen in both.
in another code, I tried to use tmr0 alone on oshon with time period 1ms. and I found that the interrupt of timer in the beginning comes at 1.030 ms (the 0.03 error is not my issue here). I expected that next interrupts will happen at 2.06 ms , 3.09 ms , 4.12 ms ,etc.... , but this was not what I found on simulator. I found actual values less than expected (second interrupt happened at 2.042, not at 2.06). I found that the initial delay of 1.03 ms decreases with time. and I do not know why....
Code:
// 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);
}
}
what is the reason of this weird behaviour??