# unable to toggle a led on port pin using interrupt, went through code & can't find the problem

#### chaosenvoy

Joined Aug 6, 2017
49
@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 ?

#### jayanthd

Joined Jul 4, 2015
906
With PIC timers even 16 bit timer you cannot create a 1 sec timer interrupt delay.

However with a 4 MHz Crystal you can create a 500 ms delay using Timer1 and then use a counter variable in Timer ISR and get 1 sec delay when counter holds 2 (stating from 0).

#### chaosenvoy

Joined Aug 6, 2017
49
With PIC timers even 16 bit timer you cannot create a 1 sec timer interrupt delay.

However with a 4 MHz Crystal you can create a 500 ms delay using Timer1 and then use a counter variable in Timer ISR and get 1 sec delay when counter holds 2 (stating from 0).
@jayanthd
thank you & i understand what you are saying in terms of precision but i'm also asking did i do my calculations right,am i going about the code the right way & with regards to if i'm using an external crystal how do i do the calculations if you can give me an example with the pic i'm using (pic 18f2550) with an 8Mhz crystal,it doesn't matter the speed of the interrupt i just want an example .
i'm a bit confuse on how to do the calculation if i'm using an external crystal .
Also is it better to use an external crystal generally speaking ?
do you think i should buy one those DIY oscilloscope kit online i definitely can't afford a real scope,

Thank you again Jayanthd.

#### jayanthd

Joined Jul 4, 2015
906
@jayanthd
thank you & i understand what you are saying in terms of precision but i'm also asking did i do my calculations right,am i going about the code the right way & with regards to if i'm using an external crystal how do i do the calculations if you can give me an example with the pic i'm using (pic 18f2550) with an 8Mhz crystal,it doesn't matter the speed of the interrupt i just want an example .
i'm a bit confuse on how to do the calculation if i'm using an external crystal .
Also is it better to use an external crystal generally speaking ?
do you think i should buy one those DIY oscilloscope kit online i definitely can't afford a real scope,

Thank you again Jayanthd.

Ok. I will answer your questions after checking your code.

Can you afford this ?

https://www.sainsmart.com/products/dds140-pc-based-oscilloscope-logic-analyzer-signal-generator

#### jayanthd

Joined Jul 4, 2015
906
@jayanthd
Thank you for the suggestion on the oscilloscope i will buy it later, i have to & see how much of my dollars it will cost me for my friend to ship it in.
will keep you posted .
Don't buy it through free international shipping method. If shipment is lost then to get refund you have to get a letter from postal department.

#### chaosenvoy

Joined Aug 6, 2017
49
@jayanthd
hello sorry for being a bother but i really need to know if i did the calculations right according to the site you sent me here is the code again & the information, also is there a way to to calculate using external oscillator.
thank you in advance

C:
// pic 18f2550
// MikroC Pro compiler
// Pickit2 programmer
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 : 256 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
Moderators note : used code tags

Last edited by a moderator:

#### jayanthd

Joined Jul 4, 2015
906
I will check the calculation and reply tonight.

#### chaosenvoy

Joined Aug 6, 2017
49

#### jayanthd

Joined Jul 4, 2015
906

#### chaosenvoy

Joined Aug 6, 2017
49
Which PIC are you using and where it is mentioned that Internal Clock is 48 MHz ?
@jayanthd
i mentioned pic 18f2550 at the top of the code & i was searching & couldn't find what is the internal clock speed i think i saw it somewhere before, i'm not sure.
i'm using the calculations from the link you sent me.
secondly how do you do the calculations if you are using an external oscillator i need to learn & understand this before i go further .
just a hobbyist willing to learn thank you.

#### chaosenvoy

Joined Aug 6, 2017
49
@jayanthd
i mentioned pic 18f2550 at the top of the code & i was searching & couldn't find what is the internal clock speed i think i saw it somewhere before, i'm not sure.
i'm using the calculations from the link you sent me.
secondly how do you do the calculations if you are using an external oscillator i need to learn & understand this before i go further .
just a hobbyist willing to learn thank you.
i know you may be too busy & i hope i'm not asking any stupid question but i just want to understand clearly if i'm doing the calculations according to this site '' http://www.best-microcontroller-projects.com/article-pic-timer-calculation.html '' you gave me so i can move on with my learning . t
thank you .

#### chaosenvoy

Joined Aug 6, 2017
49
@JohnInTX
hello sorry for being a bother but i really need to know if i did the calculations right according to the site that @jayanthd sent me here is the code & the information, also is there a way to calculate the interrupt using external oscillator. which is 8 Mhz i'm using
do i just use 1 /(8 Mhz /prescaler /256) or 8 MHZ/ 4 fosc giving me
1(2Mhz/prescaler/256 )
this is the site jayanthd sent me ''http://www.best-microcontroller-projects.com/article-pic-timer-calculation.html''
thank you in advance

C:
// pic 18f2550
// MikroC Pro compiler
// Pickit2 programmer
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 : 256 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
Mederators note : used code tags

Last edited by a moderator:

#### JohnInTX

Joined Jun 26, 2012
4,379
18F2550, 8Mhz oscillator, TIMER 0. What is the period that you need?
Is the code working? If so, how are you measuring the timer period? Note that the MikroC simulator provides a 'Stopwatch' window that will display the time between breakpoints. Set a breakpoint at the timer interrupt service routine. Run to the breakpoint, clear the stop watch, run it again to the breakpoint and observe the elapsed time - which will be the timer period.

Last edited:

#### chaosenvoy

Joined Aug 6, 2017
49
18F2550, 8Mhz oscillator, TIMER 0. What is the period that you need?
Is the code working? If so, how are you measuring the timer period? Note that the MikroC simulator provides a 'Stopwatch' window that will display the time between breakpoints. Set a breakpoint at the timer interrupt service routine. Run to the breakpoint, clear the stop watch, run it again to the breakpoint and observe the elapsed time - which will be the timer period.
thank you @JohnInTX
i'm trying for a 1 second interrupt yes the code is working but i have no way of testing the interrupt speed but by looking at it ii can see the led is blinking faster than 1 second ,my main issue is by the calculations on the site that jayanthd gave me if you look at my code & the calculation i provide in the previous post if i did it right , if not where did i go wrong in my calculation & secondly how do i do the calculations if i'm using an external crystal
the code i'm using is the internal .at 8 bit i commented it in the code
the calculation i used is at the bottom of the code i provide in previous post
Thank you again just need to understand it clearly before i move on to the next step .

#### JohnInTX

Joined Jun 26, 2012
4,379
So LED on for 1 sec and off for 1 sec for a 2 sec period, yes?
I don't really use the calculator sites
In general, I take the oscillator frequency and divide by 4 to get the Fcyc frequency. That is the basic time quantum.
Take the reciprocal to get the Tcyc time. For 8MHz you get a Tcyc of 500ns.

Take 1second and divide by 500ns to see how many counts you need:
1/500E-9 = 2E6 = 2,000,000 counts. Then divide up 2E6 by the various prescalers/postscalers available to get the remainder into some 8 or 16 bit range (depending on the timer) then set the timer to run at that count.

You have shown a derived timer in your code. I'll show you another way you might like that is easier to implement. I have to look up the particulars of the PIC you are using. I'll see if I can provide more details tomorrow.

#### be80be

Joined Jul 5, 2008
2,051
Code:
void main() {
OSCCON = 0x7E: //make the osc start up at 8 mhz
TRISA = 0b00000000; // make all PORTA pins output
PORTA = 0; //turn off PORTA pins
ADCON1 = 0b00001111; //make all analog pins as digital
CMCON = 0b00000111; //
Are you using a crystal ? If not it's not 12mhz you can only have 8mhz if you set OSCCON = 0x7E;

Last edited:

#### chaosenvoy

Joined Aug 6, 2017
49
So LED on for 1 sec and off for 1 sec for a 2 sec period, yes?
I don't really use the calculator sites
In general, I take the oscillator frequency and divide by 4 to get the Fcyc frequency. That is the basic time quantum.
Take the reciprocal to get the Tcyc time. For 8MHz you get a Tcyc of 500ns.

Take 1second and divide by 500ns to see how many counts you need:
1/500E-9 = 2E6 = 2,000,000 counts. Then divide up 2E6 by the various prescalers/postscalers available to get the remainder into some 8 or 16 bit range (depending on the timer) then set the timer to run at that count.

You have shown a derived timer in your code. I'll show you another way you might like that is easier to implement. I have to look up the particulars of the PIC you are using. I'll see if I can provide more details tomorrow.
@JohnInTX
Thank you i understand what you are saying will do it & post code later as an example

#### chaosenvoy

Joined Aug 6, 2017
49
Code:
void main() {
OSCCON = 0x7E: //make the osc start up at 8 mhz
TRISA = 0b00000000; // make all PORTA pins output
PORTA = 0; //turn off PORTA pins
ADCON1 = 0b00001111; //make all analog pins as digital
CMCON = 0b00000111; //
Are you using a crystal ? If not it's not 12mhz you can only have 8mhz if you set OSCCON = 0x7E;
Code:
void main() {
OSCCON = 0x7E: //make the osc start up at 8 mhz
TRISA = 0b00000000; // make all PORTA pins output
PORTA = 0; //turn off PORTA pins
ADCON1 = 0b00001111; //make all analog pins as digital
CMCON = 0b00000111; //
Are you using a crystal ? If not it's not 12mhz you can only have 8mhz if you set OSCCON = 0x7E;
@be80be
thank you i'm using the internal oscillator
pic 18f2550
T0CON =0b11000111;//TMR0 on,at 8 bit,internal clock,low to high transition,1 : 256 prescaler
this part of my code sets it as an 8 bit & prescaler
my main issue is if i'm doing the calculations right in my code in my previous post & secondly if i'm using an external oscillatoe do i need to /4 fosc i posted my code & how i do the calculations in my previous post you can look at it & tell me if i did it wrong , i'm getting the interrupt but not the correct timing i wanted i don't have a scope or anything to check it but if i code for a 1 second interrupt i can see the led flashing faster than 1 second .
thank you in advance
i have the timer calculator but i really want to get a hands on to what i'm doing & understand it better .