完整的测试代码如下所示
#include "mcc_generated_files/system/system.h"
bool SW_flag = 0; //记录是否发生了下降沿中断
bool SW_Button_active_flag =0; //有效按键激活标记
void SW_Button_Interrupt_Handle(void)
{
if(SW_GetValue()==LOW)
{
SW_flag = 1;
}
else if( ( SW_GetValue()==HIGH ) && (SW_flag == 1) )
{
SW_Button_active_flag =1;
SW_flag = 0;
}
}
/*
Main application
*/
int main(void)
{
SYSTEM_Initialize();
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
// Use the following macros to:
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
SW_SetInterruptHandler(SW_Button_Interrupt_Handle);
while(1)
{
if(SW_Button_active_flag == 1)
{
SW_Button_active_flag = 0;
LED_Toggle();
UART1_Write('A');UART1_Write('-');
printf("Hello PIC18F16Q40 ! \n");
}
}
}
|