Timer code for PIC18f to toggle led for 1 second

Thread Starter

Qual

Joined Aug 5, 2016
48
Hi All,

I am facing issue in toggling led for 1 second using timer0,
led is connected to RB7 of PIC18f.
Though i can insert delay_ms(1000); and toggle led i wish to do it using timer and interrupt.



Here is my code :

#code
Code:
void main()
  {
  ADCON1 = 0x0F;  // Disable Analog functions
  ANSELA=0;
  ANSELB=0;
  TRISB = 0x00;
  PORTA = 0x00;

  T0PS2_bit = 1;     // 256 prescalar
  T0PS1_bit = 1;
  T0PS0_bit = 1;
  PSA_bit    = 0;       // source from FCPU 5MHz
  T0CS_bit   = 1;       // Counter mode and
  T08BIT_bit = 1;       // 8 bit mode

  TMR0IE_bit=1;   //Enable TIMER0 Interrupt
  PEIE_bit=1;     //Enable Peripheral Interrupt

  GIE_bit=1;      //Enable INTs globally
  TMR0ON_bit=1;      //Now start the timer!

  TMR0ON_bit =  1; // Timer0 is on

TRISB = 0b01111111;  // 7 bit is output and rest are input

  while(1);             // Infinite Loop
  }

  void InitTimer0()
{
  T0CON    = 0x86;
  TMR0H    = 0x67;
  TMR0L    = 0x69;
  GIE_bit    = 1;
  TMR0IE_bit    = 1;
}

  void Interrupt(){
  if (TMR0IF_bit){
    TMR0IF_bit = 0;
    TMR0H    = 0x67;
    TMR0L    = 0x69;
    //Enter your code here
    counter++;  //Increment Over Flow Counter

      if(counter == 76 )
      {
         //Toggle RB7 (LED)

         if(RB7==0)
            PORTB.F7 = 1;
         else
            PORTB.F7=1;
         counter=0;  //Reset Counter
      }
      //Clear Flag
      TMR0IF_bit=0;
  }
}
#code

Any thing wrong in the code....?
Please suggest
 

Picbuster

Joined Dec 2, 2013
1,047
Hi All,

I am facing issue in toggling led for 1 second using timer0,
led is connected to RB7 of PIC18f.
Though i can insert delay_ms(1000); and toggle led i wish to do it using timer and interrupt.



Here is my code :

#code
Code:
void main()
  {
  ADCON1 = 0x0F;  // Disable Analog functions
  ANSELA=0;
  ANSELB=0;
  TRISB = 0x00;
  PORTA = 0x00;

  T0PS2_bit = 1;     // 256 prescalar
  T0PS1_bit = 1;
  T0PS0_bit = 1;
  PSA_bit    = 0;       // source from FCPU 5MHz
  T0CS_bit   = 1;       // Counter mode and
  T08BIT_bit = 1;       // 8 bit mode

  TMR0IE_bit=1;   //Enable TIMER0 Interrupt
  PEIE_bit=1;     //Enable Peripheral Interrupt

  GIE_bit=1;      //Enable INTs globally
  TMR0ON_bit=1;      //Now start the timer!

  TMR0ON_bit =  1; // Timer0 is on

TRISB = 0b01111111;  // 7 bit is output and rest are input

  while(1);             // Infinite Loop
  }

  void InitTimer0()
{
  T0CON    = 0x86;
  TMR0H    = 0x67;
  TMR0L    = 0x69;
  GIE_bit    = 1;
  TMR0IE_bit    = 1;
}

  void Interrupt(){
  if (TMR0IF_bit){
    TMR0IF_bit = 0;
    TMR0H    = 0x67;
    TMR0L    = 0x69;
    //Enter your code here
    counter++;  //Increment Over Flow Counter

      if(counter == 76 )
      {
         //Toggle RB7 (LED)

         if(RB7==0)
            PORTB.F7 = 1;
         else
            PORTB.F7=1;
         counter=0;  //Reset Counter
      }
      //Clear Flag
      TMR0IF_bit=0;
  }
}
#code

Any thing wrong in the code....?
Please suggest
Hi,
here my code to obtain seconds.(using a xtal on RC0 and RC1 32768KHz).

if (TMR1IF)
{
TMR1IF=0;
TMR1H=128; // in seconden
DateTime.Sec++;

// Toggel led
led = ! led;
}

///in main
TMR1IE=On;
TMR1IF=0;
 

AlbertHall

Joined Jun 4, 2014
12,345
What problem do you have with this code?
Which processor are you using?
What clock frequency?
Please include the config settings.
 

Thread Starter

Qual

Joined Aug 5, 2016
48
What problem do you have with this code?
Which processor are you using?
What clock frequency?
Please include the config settings.
Problem is led does not toggle. Doubt on timer settings and code written , whether it is working properly or not.
PIC18F45K50 ,
external crystal 20MHz,
using mikroC compiler.
Code in post 1 includes all settings configurations.
 

AlbertHall

Joined Jun 4, 2014
12,345
I suspect this code was written for a different processor.
The '45k40 timer 0 prescaler has four bits to set the value. To select 256 the value to set is '1000'.
The T0CS is three bits and should be set to '010' for Fosc/4
There are going to be many changes like this to modify the code for this processor but as I don't have the mikroc compiler I don't know the names used for the registers. You need to look at the datasheet and determine the settings you need and how to write that in mikroc code.

[EDIT] Sorry, I was looking at the wrong processor. Another reply will follow.
 

Thread Starter

Qual

Joined Aug 5, 2016
48
T0CS should be '0' to increment on Fosc/4
Hi ,
I have tried with T0CS = 0; but no change, i did small change as shown below,


Code:
  void Interrupt(){
  if (TMR0IF_bit){
    TMR0IF_bit = 0;
    TMR0H    = 0x67;
    TMR0L    = 0x69;
    //Enter your code here
    counter++;  //Increment Over Flow Counter

      if(counter == 76 )
      {
         //Toggle RB7 (LED)

        // if(RB7==0)
            PORTB.F7 = ~PORTB.F7;
            }
      else
         PORTB.F7=0;
         counter=0;  //Reset Counter
      }
      //Clear Flag
      TMR0IF_bit=0;
  }
But i see no toggle of led in hardware. (hardware is in good function)
 

AlbertHall

Joined Jun 4, 2014
12,345
Your new interrupt code won't work. You are clearing the counter on every pass so it will never get to 76.
  1. if(counter == 76 )
  2. {
  3. //Toggle RB7 (LED)

  4. // if(RB7==0)
  5. PORTB.F7 = ~PORTB.F7;
  6. } <= Delete this line
  7. else <= Delete this line
  8. PORTB.F7=0; <= Delete this line
  9. counter=0; //Reset Counter
  10. } <= Add this line
  11. }
 

Thread Starter

Qual

Joined Aug 5, 2016
48
Your new interrupt code won't work. You are clearing the counter on every pass so it will never get to 76.
  1. if(counter == 76 )
  2. {
  3. //Toggle RB7 (LED)

  4. // if(RB7==0)
  5. PORTB.F7 = ~PORTB.F7;
  6. } <= Delete this line
  7. else <= Delete this line
  8. PORTB.F7=0; <= Delete this line
  9. counter=0; //Reset Counter
  10. } <= Add this line
  11. }

Its working......:D:D thank you for support.

Thanks all for your support and guidance.
 
Top