How to define a 1s timer interrupt

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Memphis
Posts: 53
Joined: 15 Jan 2010 17:42
Location: France

How to define a 1s timer interrupt

#1 Post by Memphis » 13 Aug 2010 09:38

Hello,

I want to write in C a timer interrupt each 1sec.

I read some examples for my easypic6 board but i don't understand how to do that.
I use a PIC18F4685 with 20MHz oscillator

Someone can help me?

Thanks.

kada200
Posts: 56
Joined: 10 Jul 2010 08:04
Location: Phoenix, AZ

Re: How to define a 1s timer interrupt

#2 Post by kada200 » 13 Aug 2010 10:26

this article might help
http://romanblack.com/one_sec.htm


here's a neat tool for you :)

http://pictimer.picbingo.com/
#DEFINE ENDEAVOR_TO_PERSERVERE 1
While (ENDEAVOR_TO_PERSERVERE != 1) {
ENDEAVOR_TO_PERSERVERE = 1;
}

Phoenix, AZ (USA)

Memphis
Posts: 53
Joined: 15 Jan 2010 17:42
Location: France

Re: How to define a 1s timer interrupt

#3 Post by Memphis » 13 Aug 2010 16:50

Thanks for all.

I'm not very good in english an in c :D

The only thing i arrived to do is this:

Code: Select all

// LCD
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// FIN LCD

unsigned long count;
char txt[12];


void interrupt() {
  if (TMR0IF_bit) {
    count++;
    TMR0IF_bit = 0;        // Clear TMR0IF
  }
}


void main() {
 // OPTION_REG = 0x84;       // Assign prescaler to TMR0

  CMCON = 0x07;            // turn off comparators
  ADCON1 = 0x0F;           // AD-Ports as digital I/O

  count=0;

  Lcd_Init();                        // Initialisation LCD
  Lcd_Cmd(_LCD_TURN_ON);             // Allumage LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Suppression du curseur
  Lcd_Cmd(_LCD_CLEAR);               // Effacement LCD
  Lcd_Out(2,1,"compteur:");
  
  T0CON = 0b11000111;
  INTCON = 0xA0;           // Enable TMRO interrupt

  do {
     LongToStr(count, txt);
     Lcd_Out(3,1,rtrim(txt));
     Lcd_Out(1,1,"wait");
     delay_ms(200);
     Lcd_Out(1,1,"wait.");
     delay_ms(200);
     Lcd_Out(1,1,"wait..");
     delay_ms(200);
     Lcd_Out(1,1,"       ");
     delay_ms(200);

  } while(1);
  
  
}

To initialize the timer0, i try to convert this example for PIC16F to PIC18F:

Code: Select all

unsigned cnt;

void interrupt() {
  if (TMR0IF_bit) {
    cnt++;                 // Increment counter
    TMR0IF_bit = 0;        // Clear TMR0IF
    TMR0   = 96;
  }
}

void main() {
  OPTION_REG = 0x84;       // Assign prescaler to TMR0
  ANSEL  = 0;              // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;            // Disable comparators
  C2ON_bit = 0;
  TRISB = 0;               // PORTB is output
  PORTB = 0xFF;            // Initialize PORTB
  TMR0  = 96;              // Timer0 initial value
  INTCON = 0xA0;           // Enable TMRO interrupt
  cnt = 0;                 // Initialize cnt

  do {
    if (cnt >= 400) {
      PORTB = ~PORTB;      // Toggle PORTB LEDs
      cnt = 0;             // Reset cnt
    }
  } while(1);
}

The programm i write for my PIC18F4685 works but the counter is incremented to far...

Thanks for futur help.

braus
Posts: 167
Joined: 25 Jul 2007 22:55
Location: Mexico, city.

Re: How to define a 1s timer interrupt

#4 Post by braus » 14 Aug 2010 07:09

Hello Memphis, prior to leave interrupt service routine you have to set INTCON.GIE bit to enable future interrupt.

Code: Select all

void interrupt() {
  if (TMR0IF_bit) {
    cnt++;                 // Increment counter
    TMR0IF_bit = 0;        // Clear TMR0IF
    TMR0   = 96;
    INTCON.GIE=1;     //ready
  }
}

Best Regards
Omar Galicia
Mexico City

braus
Posts: 167
Joined: 25 Jul 2007 22:55
Location: Mexico, city.

Re: How to define a 1s timer interrupt

#5 Post by braus » 14 Aug 2010 07:18

Another thing, I'm not sure how valid is to use this syntax...

Code: Select all

if (TMR0IF_bit) {   //I mean, you are not explicitly defining that form 
                         //i.e. sbit TMR0IF_bit at INTCON.TMR0IF
...without had declared it previously.
Best Regards
Omar Galicia
Mexico City

Memphis
Posts: 53
Joined: 15 Jan 2010 17:42
Location: France

Re: How to define a 1s timer interrupt

#6 Post by Memphis » 14 Aug 2010 09:50

Hello,

Thanks to read me.

The INTCON.GIE is defined in my main as

Code: Select all

INTCON = 0xA0;
or
INTCON = 0b11100000;
It isn't good?

I try to defining TMR0IF_bit as you say like

Code: Select all

sbit TMR0IF_bit at INTCON.TMR0IF
but it isn't compiling.


So finally i modify this register:

Code: Select all

  T0CON = 0b11000111;
  RCON.IPEN=1;
  INTCON = 0b11100000;
And define my interrupt like this

Code: Select all

void interrupt() {
  if (TMR0IF_bit) {
    co++;
    if( co==77 )
    {
     count++;
     co=0;
    }
    TMR0IF_bit = 0;        // Clear TMR0IF
  }
}

It works fine for the moment, i've got one second timer interrupt...
I don't know if is good... lol

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

Re: How to define a 1s timer interrupt

#7 Post by MARIO » 14 Aug 2010 14:10

braus wrote:Hello Memphis, prior to leave interrupt service routine you have to set INTCON.GIE bit to enable future interrupt.
No, it isn't true. You do not need to do this. Once GIE is enabled and returning from interrupt, instruction RETFIE does it automatically.
From the PIC18F4685 datasheet, chapter 9, page 115 :
The “return from interrupt” instruction, RETFIE, exits
the interrupt routine and sets the GIE bit (GIEH or GIEL
if priority levels are used), which re-enables interrupts.
BR.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: How to define a 1s timer interrupt

#8 Post by Dany » 14 Aug 2010 15:17

braus wrote:Another thing, I'm not sure how valid is to use this syntax...

Code: Select all

if (TMR0IF_bit) {   //I mean, you are not explicitly defining that form 
                         //i.e. sbit TMR0IF_bit at INTCON.TMR0IF
...without had declared it previously.
Hi,
The TMR0IF_bit is already declared as

Code: Select all

sbit at INTCON.2;
where

Code: Select all

TMR0IF = 2;
So, TMR0IF_bit is in fact " INTCON.TMR0IF", and can be used as Memphis did... :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroC PRO for PIC General”