@jayanthd
hello Jayanthd thank you for all the help & pointing me in the right direction, i've read the the page on timers you sent me, i've been playing around with the calculations & getting different interrupt times, having a lot of fun. but i have 3 issues i don't understand that i need clarification on.
(1) Note: If you look at the Timer 0 block diagram in the datasheet the internal clock synchronization uses Fosc/4, so do not add 2 when calculating the period if you are not using the internal clock as a clock source! The main calculation will use your external clock and you need to The main calculation will use your external clock and you need to add 2 Fosc/4 cycles to that period.
from the page you sent me it says you don't add 2 when calculating if you are using external clock source, then they say The main calculation will use your external clock and you need to add 2 Fosc/4 cycles to that period.
Am i reading & understanding it wrong ?
(2)i didn't see any way to do the calculations with external crystal do i just use 8 Mhz in the calculations if i'm using an 8 Mhz external crystal ?
(3)
sbit Led at LATA1_bit;
sbit Led_Direction at TRISA1_bit;
unsigned char counter = 0, count = 2;
void InitTimer0 () {
T0CON = 0b11000111; //TMR0 on,at 8 bit,internal clock,low to high transition,1 : 256256 prescaler
TMR0L = 0xE9; //load value
GIE_bit = 1; // enable global interrupt
TMR0IE_bit = 1; // enable interrupt
}
void interrupt() {
if ((TMR0IF_bit)&&(TMR0IE_bit)){ //if both TMR0IF & TMR0IE is true
INTCON.TMR0IF = 0; //reset overflow flag
TMR0L = 0xE9;
if(++counter >=count){ // increment counter
Led = ~Led ; // toggle led
counter = 0; // reset counter
}
}
}
void main() {
TRISA = 0b00000000; // make all PORTA pins output
PORTA = 0; //turn off PORTA pins
ADCON1 = 0b00001111; //make all analog pins as digital
CMCON = 0b00000111; //
InitTimer0 ();
while (1){
}
}
this is my code to generate a 1 second interrupt here is my calculations
i think the internal clock is 48Mhz so
FOSC4 so 48/4 = 12
1/(12Mhz/256 prescaler/256) = 1/(.046875/256) = 1/.000183105 = 5461
1/(12Mhz/256) = 1/.046875 =21
so 1000 x 21 = 21 ms add 2 to 21 = 23
256 - 23 = 233 decimal = 0xE9
so i TMR0L = 0xE9
I don't have a scope or anything like that to check the speed of the blink of the led but from looking at it, it seems to be blinking faster than 1 sec. can you tell me where i went wrong ?
hello Jayanthd thank you for all the help & pointing me in the right direction, i've read the the page on timers you sent me, i've been playing around with the calculations & getting different interrupt times, having a lot of fun. but i have 3 issues i don't understand that i need clarification on.
(1) Note: If you look at the Timer 0 block diagram in the datasheet the internal clock synchronization uses Fosc/4, so do not add 2 when calculating the period if you are not using the internal clock as a clock source! The main calculation will use your external clock and you need to The main calculation will use your external clock and you need to add 2 Fosc/4 cycles to that period.
from the page you sent me it says you don't add 2 when calculating if you are using external clock source, then they say The main calculation will use your external clock and you need to add 2 Fosc/4 cycles to that period.
Am i reading & understanding it wrong ?
(2)i didn't see any way to do the calculations with external crystal do i just use 8 Mhz in the calculations if i'm using an 8 Mhz external crystal ?
(3)
sbit Led at LATA1_bit;
sbit Led_Direction at TRISA1_bit;
unsigned char counter = 0, count = 2;
void InitTimer0 () {
T0CON = 0b11000111; //TMR0 on,at 8 bit,internal clock,low to high transition,1 : 256256 prescaler
TMR0L = 0xE9; //load value
GIE_bit = 1; // enable global interrupt
TMR0IE_bit = 1; // enable interrupt
}
void interrupt() {
if ((TMR0IF_bit)&&(TMR0IE_bit)){ //if both TMR0IF & TMR0IE is true
INTCON.TMR0IF = 0; //reset overflow flag
TMR0L = 0xE9;
if(++counter >=count){ // increment counter
Led = ~Led ; // toggle led
counter = 0; // reset counter
}
}
}
void main() {
TRISA = 0b00000000; // make all PORTA pins output
PORTA = 0; //turn off PORTA pins
ADCON1 = 0b00001111; //make all analog pins as digital
CMCON = 0b00000111; //
InitTimer0 ();
while (1){
}
}
this is my code to generate a 1 second interrupt here is my calculations
i think the internal clock is 48Mhz so
FOSC4 so 48/4 = 12
1/(12Mhz/256 prescaler/256) = 1/(.046875/256) = 1/.000183105 = 5461
1/(12Mhz/256) = 1/.046875 =21
so 1000 x 21 = 21 ms add 2 to 21 = 23
256 - 23 = 233 decimal = 0xE9
so i TMR0L = 0xE9
I don't have a scope or anything like that to check the speed of the blink of the led but from looking at it, it seems to be blinking faster than 1 sec. can you tell me where i went wrong ?