Hello guys I want to create a program for pic 18f2550 timer0.In which a led will blink after every 10ms. I have used the Timer calculator of mikroC Which generates the following codes
I have used the following specification
MCU pic18f2550
OSC: 20mhz
Timer: timer0
Prescaler: 1:1
I slightly modified it to use in my program Which is as below
But still it does not generates any led blink after every 10ms. Please check my code and correct if it is wrong.
I have used the following specification
MCU pic18f2550
OSC: 20mhz
Timer: timer0
Prescaler: 1:1
Code:
//Timer0
//Prescaler 1:1; TMR0 Preload = 15536; Actual Interrupt Time : 10 ms
//Place/Copy this part in declaration section
void InitTimer0(){
T0CON = 0x88;
TMR0H = 0x3C;
TMR0L = 0xB0;
GIE_bit = 1;
TMR0IE_bit = 1;
}
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0H = 0x3C;
TMR0L = 0xB0;
//Enter your code here
}
}
Code:
#define LED PORTB.B0
int count = 0;
void InitTimer0()
{
T0CON = 0x88;
TMR0H = 0x3C;
TMR0L = 0xB0;
GIE_bit = 1;
TMR0IE_bit = 1;
}
void Interrupt()
{
if (TMR0IF_bit)
{
TMR0IF_bit = 0; // clearing the flag after the overflow
TMR0H = 0x3C;
TMR0L = 0xB0;
//Enter your code here
count++ ; // adding 1 in count after 10 milli second timer interrupt
}
}
void main()
{
InitTimer0(); // calling the init procedure that contain the necessary register modifications to get the timer work
TRISB = 0x00; // declare PORTB as output for led
LED = 0; // initially led is off
while (1)
{
if (count == 1) // count = 1 means 10ms*1 = 10ms = 10 ms
{
LED =~ LED ;
count = 0; // clearing the counter after 10 ms
}
}
}