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.

[solved]PIC timer interrupt simple question

Status
Not open for further replies.

wcgan

Member level 2
Joined
Nov 1, 2006
Messages
43
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,604
pic timer interrupt

Hi all,

I have tried the code below to generate an interrupt after timer1 overflow. but it does not working.

Can anyone help to correct my code? or give me a simple example of timer interrrupt?

thanks.

Code:
#include <timers.h>
#include <p18cxxx.h>

void timer1_isr(void);

void main()
{
	TRISD = 0x00;	// RD4-7 for LCD, RD3 for SR_DATdA, RD2 for output to CPLD, RD0 for nRESET output 
	LATD = 0xFF;
	PORTD = 0xFF; // RESET alwasy high at power up		
	PORTEbits.RDPU =0;

	OpenTimer1(TIMER_INT_ON & T1_8BIT_RW & T1_SOURCE_INT & T1_PS_1_8 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
	WriteTimer1(0x00FF);

	RCONbits.IPEN=1;
	RCONbits.SBOREN=0;

	INTCON=0b10000000;
	IPR1bits.TMR1IP=1;
	PIE1bits.TMR1IE=1;

	while(1)
	{
	}
}

void high_ISR (void)
{
_asm goto timer1_isr _endasm
}

#pragma interrupt timer1_isr

void timer1_isr(void)
{
PIR1bits.TMR1IF=0;
WriteTimer1(0x00FF);

PORTDbits.RD7=~PORTDbits.RD7;
}
 

mcc18 timer1 library

Just Check T1CON register value..What values it has written when u use Library function....When we use library function, we need define define MASK Value..just go through it library document...please let me know if u have not succeeded ...i am wrking in microchip..i can help
 

#pragma code high_vector

Make sure, global interrupt is enable.
 

mcc18 opentimer1

shashavali_m said:
Just Check T1CON register value..What values it has written when u use Library function....When we use library function, we need define define MASK Value..just go through it library document...please let me know if u have not succeeded ...i am wrking in microchip..i can help
Hi shashavali_m,

after OpenTimer1(...), T1CON value was 0x35
umery2k75 said:
Make sure, global interrupt is enable.

Hi umery2k75,
I already set the global interrupt to enable.
INTCON=0b10000000;
INTCON.7 = GIE/GIEH: Global Interrupt Enable bit.

thanks.

Added after 4 hours 20 minutes:

Hi all,

I found another example and it is working. But i still have some problem on the interrupt sub-routine.

my board using ext resonator : 4Mhz

below are the code:
Code:
#include <p18f4550.h>

void timer1_isr(void); //Interrupt service routine prototype
int i; //Global counter
//Always include this code, it’s necessary when using a bootloader
extern void _startup (void);

#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code high_vector = 0x000808
void high_ISR (void)
{
_asm goto timer1_isr _endasm
}
#pragma code

#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void low_ISR (void)
{
;
}
#pragma code

//End bootloader code

#pragma interrupt timer1_isr //High priority interrupt service routine
void timer1_isr(void)
{
	PIR1bits.TMR1IF = 0; //Reset Timer1 interrupt flag
	TMR1H = 0x00;
	TMR1L = 0x01;
	i = i++ % 64;  // this is line is from the original sample
	PORTAbits.RA0 = ~PORTAbits.RA0;
}
#pragma code


void main(void)
{
	TRISA = 0x00; //Port A output
	LATA=0xFF;
	ADCON1 = 0b00001111; //All ADC disabled
	RCONbits.IPEN = 1; //Enable priority levels on interrupts
	RCONbits.SBOREN = 0; //Disable BOR
	TMR1H = 0x00;
	TMR1L = 0x01;
	T1CON = 0b00110001; //Timer1 settings: 1:8 prescalar, clock = Fosc/4, Timer1 ON
	INTCONbits.GIE = 1; //Enable global interrupts
	INTCONbits.PEIE = 1; //Enable peripheral interrupts
	INTCON = 0b10000000; //
	IPR1bits.TMR1IP = 1; //Timer1 interrupt priority high
	PIE1bits.TMR1IE = 1; //Timer1 interrupt enable

	while(1)
	{
	}
}

The problem is, if i comment "i = i++ % 64;" in 'timer1_isr' sub-routine, main program will not able to trigger/jump in the interrupt "timer1_isr" function again. leave the "i = i++ % 64;" in 'timer1_isr' then everything is ok.

anyone can tell me what going on in this program?

Please find attached for my project configuration bits.
Thanks

/*===================================================
Hi all,

I found out what is the problem in my program already.
I set the wrong interrupt vector
#pragma code high_vector = 0x000808
#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
the correct interrupt vector should be
#pragma code high_vector = 0x000008
#pragma code _LOW_INTERRUPT_VECTOR = 0x000018


thanks.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top