LED blink using timer0 in MikroC

Thread Starter

khatus

Joined Jul 2, 2018
115
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

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
  }
}
I slightly modified it to use in my program Which is as below

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
 }
 }
 }
But still it does not generates any led blink after every 10ms. Please check my code and correct if it is wrong.
 

JohnInTX

Joined Jun 26, 2012
4,787
A few things:

RB0-RB4 power up as ANALOG inputs unless PRADRN==0 in the CONFIG bits. MikroC hides the config in the project so we can't tell what it is. You can set them to digital using
ADCON1 = 0x0f;
but check that out. See the PORTB and CONFIG3H descriptions in the datasheet. It’s worth noting that if RB0 is indeed analog then
LED = ~LED;
will always set the output to ‘1’. That’s because reading an analog input will always return a ‘0’ but writing does set the output. So ~0 = 1.

You should define your LED as LATB.P0 Use PORT only for inputs never outputs.

Your count test should not be ==. It should be >0 so that if you have more than one interrupt between checking, you don't miss it. It's not an issue here probably but it's bad practice.

The counter reload is OK for your settings and 10ms. As noted above, that's pretty fast. Just for grins set the prescaler to 256 and let the counter free run without loading anything to make everything run a little slower. When the LED flashes on/off correctly, you can change back.

A better way to run your counter is to preset it to some value (number of interrupts * counter value = time) and count it to 0 leaving it at 0. That way, you can't miss anything. A good way to count to 0 and stay in the interrupt routine is:
if(count) count--; // if counter >0, decrement it. Stop at 0.

Then your code sets the timer like this:
count = 100; // 100*10ms = 1 sec

Test it like this:
if(count==0){
// toggle LED
count = 100;
}


How's that?
 
Last edited:
Top