I am currently working on a piece of code that uses two interrupt routines. A high priority and a low priority. I will be using the PIC18F4620 but currently only debugging in simulation mode in MPLAB (v7.60). I am using the MCC18 library to compile my code.
I am using a 20Mhz external oscillating crystal.
This is what i would like to happen:
Timer0 interrupts as a high priority
Timer1 interrupts as a low priority
This is what happens:
Timer0 interrupts as it should
Timer1 just sits there watching tea while drinking hot chocolate!
I would be very grateful if you can tell me where I've gone wrong.
All I care about is my interrupt flag..and my hair loss due to stress...
Here is my code, I realise its really unclear but I've tampered with it so many times its got messy.
#include <p18f4620.h>
#include <timers.h>
#include <stdio.h>
#include <delays.h>
void power (void);
void signal (void);
unsigned int ReadTimer0(void);
unsigned int ReadTimer1 (void);
void WriteTimer0( unsigned int timer );
void Delay100TCYx( unsigned char unit );
void Delay10TCYx( unsigned char unit );
void HandlerHigh (void);
void HandlerLow (void);
int number = 15;
unsigned char clock = 0;
unsigned char clock2 = 0;
int servo_flag = 0;
void main (void)
{
int Time = 0;
INTCON = 0x20; //disable global and enable TMR0 interrupt
INTCON2 = 0x84; //TMR0 high priority
IPR1 = 0x00; //TMR1 low priority
RCONbits.IPEN = 1; //enable priority levels
INTCONbits.GIEH = 1; //enable interrupts
INTCONbits.GIEL = 1; //enable low interrupts
OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_128);
T0CONbits.TMR0ON = 1;
TMR1H = 0;
TMR1L = 0;
T1CON = 0x13; /*b'00001001' */
PIE1bits.TMR1IE = 1;
RCONbits.IPEN = 1;
power();
while (1)
{
// clock++;
// signal(); //send out servo signal
}
}
void signal (void)
{
if (servo_flag == 1)
{
PORTAbits.RA1 = 1;
servo_flag = 0;
}
if (clock >= 100)
{
PORTAbits.RA1 = 0;
}
}
/*Set a Led to show PIC is running*/
void power (void)
{
ADCON1 = 0x0f;
TRISA = 0x00000110;
PORTA = 0;
PORTAbits.RA0 = 1;
}
//----------------------------------------------------------------------------
// High priority interrupt vector
#pragma code VectorHigh = 0x08
void VectorHigh (void)
{
_asm
goto HandlerHigh //jump to interrupt routine
_endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt HandlerHigh
void HandlerHigh (void)
{
if (INTCONbits.TMR0IF) //check for TMR0 overflow
{
clock++;
INTCONbits.TMR0IF = 0; //clear interrupt flag
}
if (clock > 2)
{
clock = 0;
servo_flag = 1;
}
}
//----------------------------------------------------------------------------
// Low priority interrupt vector
#pragma code VectorLow = 0x18
void VectorLow (void)
{
_asm
goto HandlerLow
_endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt HandlerLow
void HandlerLow (void)
{
if (PIR1bits.TMR1IF) //check for TMR1 overflow
{
clock2++;
PIR1bits.TMR1IF = 0; //clear interrupt flag
}
}
// END
Please don't just recommend me to use OpenTimer1() unless you can inform me what needs to go in there. I tried this method first and failed.
Thanks for reading,
James
I am using a 20Mhz external oscillating crystal.
This is what i would like to happen:
Timer0 interrupts as a high priority
Timer1 interrupts as a low priority
This is what happens:
Timer0 interrupts as it should
Timer1 just sits there watching tea while drinking hot chocolate!
I would be very grateful if you can tell me where I've gone wrong.
All I care about is my interrupt flag..and my hair loss due to stress...
Here is my code, I realise its really unclear but I've tampered with it so many times its got messy.
#include <p18f4620.h>
#include <timers.h>
#include <stdio.h>
#include <delays.h>
void power (void);
void signal (void);
unsigned int ReadTimer0(void);
unsigned int ReadTimer1 (void);
void WriteTimer0( unsigned int timer );
void Delay100TCYx( unsigned char unit );
void Delay10TCYx( unsigned char unit );
void HandlerHigh (void);
void HandlerLow (void);
int number = 15;
unsigned char clock = 0;
unsigned char clock2 = 0;
int servo_flag = 0;
void main (void)
{
int Time = 0;
INTCON = 0x20; //disable global and enable TMR0 interrupt
INTCON2 = 0x84; //TMR0 high priority
IPR1 = 0x00; //TMR1 low priority
RCONbits.IPEN = 1; //enable priority levels
INTCONbits.GIEH = 1; //enable interrupts
INTCONbits.GIEL = 1; //enable low interrupts
OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_128);
T0CONbits.TMR0ON = 1;
TMR1H = 0;
TMR1L = 0;
T1CON = 0x13; /*b'00001001' */
PIE1bits.TMR1IE = 1;
RCONbits.IPEN = 1;
power();
while (1)
{
// clock++;
// signal(); //send out servo signal
}
}
void signal (void)
{
if (servo_flag == 1)
{
PORTAbits.RA1 = 1;
servo_flag = 0;
}
if (clock >= 100)
{
PORTAbits.RA1 = 0;
}
}
/*Set a Led to show PIC is running*/
void power (void)
{
ADCON1 = 0x0f;
TRISA = 0x00000110;
PORTA = 0;
PORTAbits.RA0 = 1;
}
//----------------------------------------------------------------------------
// High priority interrupt vector
#pragma code VectorHigh = 0x08
void VectorHigh (void)
{
_asm
goto HandlerHigh //jump to interrupt routine
_endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt HandlerHigh
void HandlerHigh (void)
{
if (INTCONbits.TMR0IF) //check for TMR0 overflow
{
clock++;
INTCONbits.TMR0IF = 0; //clear interrupt flag
}
if (clock > 2)
{
clock = 0;
servo_flag = 1;
}
}
//----------------------------------------------------------------------------
// Low priority interrupt vector
#pragma code VectorLow = 0x18
void VectorLow (void)
{
_asm
goto HandlerLow
_endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt HandlerLow
void HandlerLow (void)
{
if (PIR1bits.TMR1IF) //check for TMR1 overflow
{
clock2++;
PIR1bits.TMR1IF = 0; //clear interrupt flag
}
}
// END
Please don't just recommend me to use OpenTimer1() unless you can inform me what needs to go in there. I tried this method first and failed.
Thanks for reading,
James