Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC 18F timer interrupts

Status
Not open for further replies.

ernestmyname

Member level 1
Member level 1
Joined
Nov 19, 2009
Messages
33
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Clemson
Activity points
1,548
I have been unable to get an interrupt timer routine work on my PIC 18F2550. I basically copied the code from the C18 user's guide and it is still not working properly. Any help would be appreciated. PORTA is outputting to a 7 seg display.

#include <p18f2550.h>
#include <timers.h>
#include <delays.h>

void timer_isr (void);
unsigned char digit[] = {0x01,0x4F,0x12,0x06,0x4C,0x24,0x20,0x0F,0x00,0x04};
#pragma code low_vector=0x18

void low_interrupt (void)
{ _asm GOTO timer_isr _endasm
}

#pragma code
#pragma interruptlow timer_isr

void timer_isr (void)
{ INTCONbits.TMR0IF = 0;
PORTA = digit[3];
Delay10KTCYx(10);
}

void main(void)
{ TRISA = 0x00;
PORTA = digit[0];
OpenTimer0 (TIMER_INT_ON &
T0_SOURCE_INT &
T0_8BIT);
INTCONbits.GIE = 1; //enable global interrupts
while (1);
}

The value on the display never changes from zero, so I know it's not entering the ISR routine.
 

Hi,
Change your code to:
Code:
#include <p18f2550.h>
#include <timers.h>
#include <delays.h>

#pragma config FOSC = XT_XT //Crystal oscillator
#pragma config WDT = OFF //Watchdog Timer off
#pragma config MCLRE = OFF //Master clear off
#pragma config LVP = OFF //LVP off

void timer_isr (void);
unsigned char digit[] = {0x01,0x4F,0x12,0x06,0x4C,0x24,0x20,0x0F,0x00,0x04};
#pragma code high_vector=0x08

void high_interrupt (void)
{ _asm GOTO timer_isr _endasm
}

#pragma code
#pragma interrupt timer_isr

void timer_isr (void)
{
	INTCONbits.TMR0IF = 0;
	PORTA = digit[3];
	Delay10KTCYx(10);
}

void main(void)
{
	TRISA = 0x00;
	PORTA = digit[0];
	OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_8BIT);
	INTCONbits.GIE = 1; //enable global interrupts
	while (1);
}
The code you provided uses the low ISR but your program has not been set up to use the low ISR but the default high ISR.
You also did not set configuration bits.
Hope it helps.
Tahmid.
 
many thanks. That got me going in the right direction. This is the code that is working properly:

#pragma config FOSC = INTOSCIO_EC /*Internal oscillator, port function on RA6, EC used by USB*/
#pragma config WDT = OFF /* Disable watchdog timer */

The configuration bits you posted caused my controller to get stuck. It would just display one bar on the display, strangely. I used the code above for initialization and used the changes you made to the code, to make it a high level interrupt. This works perfectly.
 

Hi,
I set the oscillator for external crystal, but I think your circuit uses the internal RC oscillator. That's why it didn't work properly.
But now its fine.
Tahmid.
 
Hi,
In the RCON register, there is a bit called IPEN. Set that to 1, so you have interrupt priority levels.
Then in the INTCON register set bit GIEL (bit 6).
In the INTCON2 register, clear T0IP (bit 2). This sets timer0 interrupt priority low.
Then you can use low priority interrupts.
For (priority) interrupts look at these registers, in the datasheet:
INTCON
INTCON2
INTCON3
PIR1
PIR2
PIE1
PIE2
IPR1
IPR2
RCON
Carefully read pages 99 through 110 for a clear idea on interrupts.
Hope this helped.
Tahmid.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top