sifrid
Newbie level 4
hello every one, i've been trying to solve a problem i have with the overflow flag TMR1IF, it doesn't seems to get set when timer 1 reaches 255 or 65535 depending on the operation mode, i paste my code below:
#include <htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(1,MCLRDIS & WDTDIS & INTIO & STVREN_OFF);
void init (void)
{
OSCCONbits.IDLEN=1; // device enters idle mode on sleep inst
OSCCONbits.IRCF2=1; // 4MHZ
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF0=0;
OSCCONbits.OSTS=1; // oscilator Start-up
OSCCONbits.IOFS=1; // frequency stable bits
OSCCONbits.SCS1=1; // internal oscillator
OSCCONbits.SCS0=0;
RCONbits.IPEN=0; //if 0 disables priority leves if 1 enables them
//INTCON=0b11000000; //enables GIE and PEIE
INTCON=0b1000000; // enables GIE
PIE1bits.TMR1IE=1; // Timer1 interrup flag enable
PIR1bits.TMR1IF=0; // clear overflow flag
T1CONbits.RD16=1; //it is suppose to count up to 65535
T1CONbits.T1RUN=0; //device clock is derive from a source different to timer1 osc
T1CONbits.T1CKPS1=1; // 1:4
T1CONbits.T1CKPS0=0;
T1CONbits.T1OSCEN=0; // Timer1 oscillator is shut off
T1CONbits.TMR1CS=0; // Internal clock selected
T1CONbits.TMR1ON=0; // stops Timer1
ADCON1=0b00001111; //All pins as digital
}
void main (void)
{
init();
T1CONbits.TMR1ON=1; //starts timer 1
while(1)
{
if(PIR1bits.TMR1IF==1)
{
PORTDbits.RD2^=1;
PIR1bits.TMR1IF=0;
}
}
}
i use MPLAB IDE v 8.76 and HI-Tech v9.80 PICC 18 in lite mode, so when i use MPLAB SIM tool and run the code step by step checking the watch window, TMR1 register counts up to 255 but as you may see above, i wrote T1CONbits.RD16=1 so it is suppose to count till 65536 and if i write T1CONbits.RD16=0 it counts up to 65535 not 255, anyway that's not my main concern, as i started checking the watch window i realized TMR1IF never gets set on overflow so i never get to toggle the led placed on RD2, but idk what could be the problem i've already enabled TMR1IE and GIE......
Another trouble i've found is that when i enable PEIE (INTCON=0b11000000 i always get the message CORE-E0001: Stack over flow error occurred from instruction at 0x007fe6 in the output window, so by now i don't know if i'm writing something wrong in the code, or if there is a HI-tech bug i'm not aware of, so i'll appreciate any help you can give me. thank you in advance.
#include <htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(1,MCLRDIS & WDTDIS & INTIO & STVREN_OFF);
void init (void)
{
OSCCONbits.IDLEN=1; // device enters idle mode on sleep inst
OSCCONbits.IRCF2=1; // 4MHZ
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF0=0;
OSCCONbits.OSTS=1; // oscilator Start-up
OSCCONbits.IOFS=1; // frequency stable bits
OSCCONbits.SCS1=1; // internal oscillator
OSCCONbits.SCS0=0;
RCONbits.IPEN=0; //if 0 disables priority leves if 1 enables them
//INTCON=0b11000000; //enables GIE and PEIE
INTCON=0b1000000; // enables GIE
PIE1bits.TMR1IE=1; // Timer1 interrup flag enable
PIR1bits.TMR1IF=0; // clear overflow flag
T1CONbits.RD16=1; //it is suppose to count up to 65535
T1CONbits.T1RUN=0; //device clock is derive from a source different to timer1 osc
T1CONbits.T1CKPS1=1; // 1:4
T1CONbits.T1CKPS0=0;
T1CONbits.T1OSCEN=0; // Timer1 oscillator is shut off
T1CONbits.TMR1CS=0; // Internal clock selected
T1CONbits.TMR1ON=0; // stops Timer1
ADCON1=0b00001111; //All pins as digital
}
void main (void)
{
init();
T1CONbits.TMR1ON=1; //starts timer 1
while(1)
{
if(PIR1bits.TMR1IF==1)
{
PORTDbits.RD2^=1;
PIR1bits.TMR1IF=0;
}
}
}
i use MPLAB IDE v 8.76 and HI-Tech v9.80 PICC 18 in lite mode, so when i use MPLAB SIM tool and run the code step by step checking the watch window, TMR1 register counts up to 255 but as you may see above, i wrote T1CONbits.RD16=1 so it is suppose to count till 65536 and if i write T1CONbits.RD16=0 it counts up to 65535 not 255, anyway that's not my main concern, as i started checking the watch window i realized TMR1IF never gets set on overflow so i never get to toggle the led placed on RD2, but idk what could be the problem i've already enabled TMR1IE and GIE......
Another trouble i've found is that when i enable PEIE (INTCON=0b11000000 i always get the message CORE-E0001: Stack over flow error occurred from instruction at 0x007fe6 in the output window, so by now i don't know if i'm writing something wrong in the code, or if there is a HI-tech bug i'm not aware of, so i'll appreciate any help you can give me. thank you in advance.