程序框架是使用MCC进行配置。
定时器1初始化如下:
void TMR1_Initialize(void)
{
//Set the Timer to the options selected in the GUI
//T1GSS T1G_pin; TMR1GE disabled; T1GTM disabled; T1GPOL low; T1GGO_nDONE done; T1GSPM disabled;
T1GCON = 0x00;
TMR1H = 0xFF;
TMR1L = 0xE1; //时间1ms
// Clearing IF flag before enabling the interrupt.
PIR1bits.TMR1IF = 0;
// Load the TMR value to reload variable
timer1ReloadVal=(uint16_t)((TMR1H << 8) | TMR1L);
// Enabling TMR1 interrupt.
PIE1bits.TMR1IE = 1;
// Set Default Interrupt Handler
TMR1_SetInterruptHandler(TMR1_DefaultInterruptHandler);
// T1CKPS 1:1; T1OSCEN disabled; nT1SYNC synchronize; TMR1CS LFINTOSC; TMR1ON enabled;
T1CON = 0xC1;
}
定时器1中断程序如下:
void TMR1_ISR(void)
{
// Clear the TMR1 interrupt flag
PIR1bits.TMR1IF = 0;
TMR1_WriteTimer(timer1ReloadVal);
Bug_Check();
Read_Key_Value();
// ticker function call;
// ticker is 1 -> Callback function gets called everytime this ISR executes
TMR1_CallBack();
}
中断处理程序如下:
void __interrupt() INTERRUPT_InterruptManager (void)
{
// interrupt handler
if(INTCONbits.TMR0IE == 1 && INTCONbits.TMR0IF == 1)
{
TMR0_ISR();
}
else if(INTCONbits.PEIE == 1)
{
if(PIE1bits.TMR1IE == 1 && PIR1bits.TMR1IF == 1)
{
TMR1_ISR();
}
else
{
//Unhandled Interrupt
}
}
else
{
//Unhandled Interrupt
}
}
主程序如下:
void main(void)
{
SYSTEM_Initialize();
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
//TMR1_StartTimer();
//TMR1_SetInterruptHandler(TMR1_ISR);
uint16_t t;
t = TMR1_ReadTimer();
while(1)
{
WDT_Disable();
Power_Low_Check();
t = TMR1_ReadTimer();
Key_Handle();
Reset_Time_Judge();
Reset_Task();
Led_Blink();
WDT_Enable();
SLEEP();
}
}
按道理来说定时器初始化后TMR1ON都已经设为1了,全局中断和外设中断一开启应该就能开启计时,但是经过读取,两次的t都是FFE2,计数值感觉都没变,打断点调试了一下发现也没有进入中断,求大佬指点。
|