Author Topic: LED blink using timer0 in MikroC  (Read 1965 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
LED blink using timer0 in MikroC
« on: September 17, 2018, 12:48:59 am »
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: [Select]
//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: [Select]

#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.
« Last Edit: September 17, 2018, 12:51:24 am by khatus »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: LED blink using timer0 in MikroC
« Reply #1 on: September 17, 2018, 01:59:26 am »
As LED is used for single bit output, you are reading back its value to toggle it and you are using a PIC18, LED should be #defined as
Code: [Select]
#define LED LATB.B0to avoid possible issues with the read-modify-write effect.

Also the definition of count should be modified:
Code: [Select]
volatile unsigned int count=0;The 'volatile' qualifier tells the compiler that the ISR can modify the variable in the background, so it checks the actual variable in the if() in main() rather than optimising the check away or using a temporary copy of count within the loop.

The unsigned qualifier is because there is no need for negative counts, and without the wasted sign bit it can time up to just under 11 minutes.

Some other refinements that would be needed in a larger program would be to disable interrupts in main() while count is being checked or updated to prevent problems if main() is interrupted when its accessing count right in between the accesses to its high byte and its low byte, and to check count with if(count>1) so that if some other part of the program took longer than 10ms, the check wouldn't fail because count had already gone past 1.
« Last Edit: September 17, 2018, 02:01:15 am by Ian.M »
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
Re: LED blink using timer0 in MikroC
« Reply #2 on: September 17, 2018, 02:05:20 am »
Thanks it works !!! :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf