我使用MCC配置了定时器0,时钟源选择Fosc/4,预分频为1024,后分频为5,按照计算定时时间为20.48ms。生成代码后未作修改,测试时却检测不到定时器0的中断,调试时发现已经进入了timer0的初始化,GIE总中断也打开了,T0IE中断也打开了,但在中断管理函数中却没有被执行,请问是不是还有哪里需要设置。另外我还开启了外部中断和串口通讯的中断,这些功能可以正常运行,就是定时器不起作用。
下面是MCC生成的timer0.c文件:
下面是中断处理函数,也是自动生成的,我在其中添加了翻转LED灯的语句,以便观察,同时在此设置断点,调试时不会执行到此处:
- #include "interrupt_manager.h"
- #include "mcc.h"
- #include "pin_manager.h"
- void __interrupt() INTERRUPT_InterruptManager (void)
- {
- // interrupt handler
- IO_RA2_Toggle();
- if(PIE0bits.TMR0IE == 1 && PIR0bits.TMR0IF == 1)
- {
- TMR0_ISR();
- }
- else if(PIE0bits.INTE == 1 && PIR0bits.INTF == 1)
- {
- INT_ISR();
- }
- else
- {
- //Unhandled Interrupt
- }
- }
下面是主程序,按照初始化,开总中断,开定时器0中断,进入while循环的步骤执行,调试时能够顺利进入循环:
#include "mcc_generated_files/mcc.h"
uint8_t keys,keyok,mode,rxData;
uint16_t ms;
void delay_ms(uint16_t times);
/*
Main application
*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts启用全局中断(INTCONbits.GIE = 1)。
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts启用外围设备中断(INTCONbits.PEIE = 1)
INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
IO_RA2_SetLow();
for(mode=0;mode<6;mode++){
delay_ms(500);
IO_RA2_Toggle();
}
mode = 33;
IO_RA2_SetHigh();
//INTERRUPT_PeripheralInterruptDisable();
TMR0_StartTimer(); //T0CON0bits.T0EN = 1;
while (1)
{
// Add your application code
if(keyok>0){
EUSART1_Write(keys);
keys = 0;
keyok = 0; //取消按键事件标志
}
if(ms>5){
ms = 0;
mode++;
if(mode>120)
mode = 33;
EUSART1_Write(mode);
}
if(0==IO_RC2_GetValue()){
IO_RA2_SetLow();
delay_ms(200);
IO_RA2_SetHigh();
}
// Logic to echo received data
if(EUSART1_is_rx_ready())
{
IO_RA2_SetLow();
rxData = EUSART1_Read();
if(EUSART1_is_tx_ready())
{
EUSART1_Write(rxData);
}
// delay_ms(500);
IO_RA2_SetHigh();
}
}
}
void delay_ms(uint16_t times)
{
uint16_t t,r;
for(t=times;t>0;t--)
for(r=110;r>0;r--);
}
这是整个项目打包:
|