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.
How to define a 1s timer interrupt
Re: How to define a 1s timer interrupt
this article might help
http://romanblack.com/one_sec.htm
here's a neat tool for you
http://pictimer.picbingo.com/
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)
While (ENDEAVOR_TO_PERSERVERE != 1) {
ENDEAVOR_TO_PERSERVERE = 1;
}
Phoenix, AZ (USA)
Re: How to define a 1s timer interrupt
Thanks for all.
I'm not very good in english an in c
The only thing i arrived to do is this:
To initialize the timer0, i try to convert this example for PIC16F to PIC18F:
The programm i write for my PIC18F4685 works but the counter is incremented to far...
Thanks for futur help.
I'm not very good in english an in c
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.
Re: How to define a 1s timer interrupt
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
Omar Galicia
Mexico City
Re: How to define a 1s timer interrupt
Another thing, I'm not sure how valid is to use this syntax...
...without had declared it previously.
Code: Select all
if (TMR0IF_bit) { //I mean, you are not explicitly defining that form
//i.e. sbit TMR0IF_bit at INTCON.TMR0IF
Best Regards
Omar Galicia
Mexico City
Omar Galicia
Mexico City
Re: How to define a 1s timer interrupt
Hello,
Thanks to read me.
The INTCON.GIE is defined in my main as
It isn't good?
I try to defining TMR0IF_bit as you say like
but it isn't compiling.
So finally i modify this register:
And define my interrupt like this
It works fine for the moment, i've got one second timer interrupt...
I don't know if is good... lol
Thanks to read me.
The INTCON.GIE is defined in my main as
Code: Select all
INTCON = 0xA0;
or
INTCON = 0b11100000;
I try to defining TMR0IF_bit as you say like
Code: Select all
sbit TMR0IF_bit at INTCON.TMR0IF
So finally i modify this register:
Code: Select all
T0CON = 0b11000111;
RCON.IPEN=1;
INTCON = 0b11100000;
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
Re: How to define a 1s timer 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.braus wrote:Hello Memphis, prior to leave interrupt service routine you have to set INTCON.GIE bit to enable future interrupt.
From the PIC18F4685 datasheet, chapter 9, page 115 :
BR.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.
Re: How to define a 1s timer interrupt
Hi,braus wrote:Another thing, I'm not sure how valid is to use this syntax......without had declared it previously.Code: Select all
if (TMR0IF_bit) { //I mean, you are not explicitly defining that form //i.e. sbit TMR0IF_bit at INTCON.TMR0IF
The TMR0IF_bit is already declared as
Code: Select all
sbit at INTCON.2;
Code: Select all
TMR0IF = 2;
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)
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)