RogerWhitfield
New Member
Hi, sorry if this seems an obvious question but I am struggeling to understand how to use TMR0 on the 16f628.
I am using Hi-Tech C.
void interrupt isr(void)
{
T0IF=0; // Clear Inturrupt Flag
PORTB++;
if(PORTB>16) PORTB=0x00;
}
void main(void){
OSCF=0; // Set Processor to 37KHz Mode
MSG1=0; // Clear MSG1
GIE=0x01; // Global Interrupt Enable
T0IE=1; // Timer0 Interrupt Enable
TMR0=0; // Set TMR0 to 0
PSA=0; // Prescalar assigned to TMR0
PS0=1; // Prescalar 111 = /256
PS1=1; // 110= /128
PS2=1; // 101= /64 etc
CMCON = 0x07; // turn off comparator
TRISB = 0b00000000; // 8bits of port B are outputs
TRISA = 0b00001111; // 4 high bits of port A are outputs and 4 low bits are inputs
OPTION_REG = 0b11010111; // Turn on Timer 0
while (1){ // loops forever
}
}
So I think I have set the internal oscillator to 37Khz and TMR0 is assigned a prescalar of 256, I believe this means that the timer increments once every 37 / 4 / 256 = 36ms and therefore the interrupt should fire when TMR0 reaches 255 which should be 36ms * 255 = every 9.21 seconds.
When I run the code on the chip the interrupt seems to fire about once every 7 seconds (close but not exact), but if I change the prescalar to 2 or any other value
PS0=0; // Prescalar 111 = /256
PS1=0; // 110= /128
PS2=0; // 101= /64 etc
I would expect the interrupt to fire once every 37 / 4 / 2 = 4.625 seconds * 255 = 1179 seconds, but if I change the code as above it still takes about 7 seconds.
I guess that I have misunderstood something, can anyone help, I have been searching the net all day and I am lost.
I am using Hi-Tech C.
void interrupt isr(void)
{
T0IF=0; // Clear Inturrupt Flag
PORTB++;
if(PORTB>16) PORTB=0x00;
}
void main(void){
OSCF=0; // Set Processor to 37KHz Mode
MSG1=0; // Clear MSG1
GIE=0x01; // Global Interrupt Enable
T0IE=1; // Timer0 Interrupt Enable
TMR0=0; // Set TMR0 to 0
PSA=0; // Prescalar assigned to TMR0
PS0=1; // Prescalar 111 = /256
PS1=1; // 110= /128
PS2=1; // 101= /64 etc
CMCON = 0x07; // turn off comparator
TRISB = 0b00000000; // 8bits of port B are outputs
TRISA = 0b00001111; // 4 high bits of port A are outputs and 4 low bits are inputs
OPTION_REG = 0b11010111; // Turn on Timer 0
while (1){ // loops forever
}
}
So I think I have set the internal oscillator to 37Khz and TMR0 is assigned a prescalar of 256, I believe this means that the timer increments once every 37 / 4 / 256 = 36ms and therefore the interrupt should fire when TMR0 reaches 255 which should be 36ms * 255 = every 9.21 seconds.
When I run the code on the chip the interrupt seems to fire about once every 7 seconds (close but not exact), but if I change the prescalar to 2 or any other value
PS0=0; // Prescalar 111 = /256
PS1=0; // 110= /128
PS2=0; // 101= /64 etc
I would expect the interrupt to fire once every 37 / 4 / 2 = 4.625 seconds * 255 = 1179 seconds, but if I change the code as above it still takes about 7 seconds.
I guess that I have misunderstood something, can anyone help, I have been searching the net all day and I am lost.