I m new to both the timer and interrupts things.
I tried to write a frequency meter
(using some code from http://www.micro-examples.com/public/mi ... meter.html )
on a PIC18 series (PIC18F2420)
I modified some parts but it s not working,
in debug mode it shows that the interrupt function is never called.
Code: Select all
// Lcd pinout settings
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D4 at RC4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D7_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC4_bit;
char freqt[20];
unsigned long cntr ; // number of RB0 transition
unsigned int ovrflw ; // number of timer0 overflows
void init() {
Lcd_Init();
}
/*
* interrupt routine called 4000000/256 times by seconds :
* the timer TMR0 is increased each 4 clock cycles (quartz frequency is 16 Mhz),
* and overflows when reseting from 255 to 0,
* calling the interrupt procedure with bit T0IF set
*
* also called on each RBO transition, with bit INTF set
*/
void interrupt(void)
{
if(INTCON.RBIF)
{
/*
* RB0 interrupt
*/
cntr++ ; // inc. transition counter
INTCON.RBIF = 0 ; // clear interrupt flag to enable next call
}
else if(INTCON.TMR0IF)
{
/*
* TIMER 0 overflow
*/
ovrflw++ ; // inc. overflow counter
INTCON.T0IF = 0 ; // clear interrupt flag to enable next call on overflow
IntToStr(ovrflw,freqt);
Lcd_Out(1, 2, freqt);
}
}
void main() {
init();
TRISB0_bit=1;
T0CON = 0b11001000 ; // no prescaler
/*
* main loop
*/
for(;;)
{
cntr = 0 ; // clear counters
ovrflw = 0 ;
INTCON = 0b10110000 ; // GIE, T0IF and INTF enabled
INTCON2.TMR0IP = 1; // Timer interrupt high priority
INTCON2.RBIP = 0; // PortB interrupt low priority
while(ovrflw < 19352) ; // wait 1 second : 19352 = 20 000 000 / 4 / 256, rounded up
INTCON = 0 ; // stop all interrupts
//LongToStr(ovrflw,freqt);
//Lcd_Out(1, 2, freqt);
}
}