例程-周期中断
周期中断编号
typedef enum // 枚举通道号
{
CCU60_CH0,
CCU60_CH1,
CCU61_CH0,
CCU61_CH1,
}pit_index_enum;
初始化中断
#define pit_ms_init(pit_index, time) pit_init((pit_index), (time*1000)) // (单位为 毫秒)
// 函数简介 pit_ms初始化
// 参数说明 pit_index 选择CCU6模块
// 参数说明 time 周期时间(单位:毫秒)
// 返回参数 void
// 使用示例 pit_ms_init(CCU60_CH0, 5); // 设置周期中断5ms
中断服务
- 语法:
IFX_INTERRUPT(cc60_pit_ch0_isr, 0, CCU6_0_CH0_ISR_PRIORITY)
定义了中断服务例程(ISR),其中cc60_pit_ch0_isr
是中断服务例程的名称,0
是中断优先级(数值越小优先级越高),CCU6_0_CH0_ISR_PRIORITY
是特定的中断优先级宏定义。
- 功能:当中断发生时,执行以下操作:
interrupt_global_enable(0)
:开启中断嵌套,允许在中断处理过程中响应其他更高优先级的中断。
pit_clear_flag(CCU60_CH0)
:清除周期中断标志位,以避免重复触发中断。
pit_state = 1
:设置一个标志位pit_state
,用于通知主循环中断已发生。
案例
#include "isr_config.h"
#include "zf_common_headfile.h"
#pragma section all "cpu0_dsram"
#define PIT_NUM (CCU60_CH0 ) // 使用的周期中断编号
#define LED1 (P20_9)
#define LED2 (P20_8)
uint8 pit_state = 0;
int core0_main(void)
{
clock_init(); // 获取时钟频率<务必保留>
debug_init(); // 初始化默认调试串口
// 此处编写用户代码 例如外设初始化代码等
gpio_init(LED1, GPO, GPIO_LOW, GPO_PUSH_PULL); // 初始化 LED1 输出 默认低电平 推挽输出模式
gpio_init(LED2, GPO, GPIO_HIGH, GPO_PUSH_PULL); // 初始化 LED2 输出 默认高电平 推挽输出模式
pit_ms_init(PIT_NUM, 500); // 初始化 CCU6_0_CH0 为周期中断 1000ms 周期
// 此处编写用户代码 例如外设初始化代码等
cpu_wait_event_ready(); // 等待所有核心初始化完毕
while (TRUE)
{
// 此处编写需要循环执行的代码
if(pit_state)
{
gpio_toggle_level(LED1);
gpio_toggle_level(LED2);
pit_state = 0; // 清空周期中断触发标志位
}
// 此处编写需要循环执行的代码
}
}
IFX_INTERRUPT(cc60_pit_ch0_isr, 0, CCU6_0_CH0_ISR_PRIORITY)
{
interrupt_global_enable(0); // 开启中断嵌套
pit_clear_flag(CCU60_CH0);
pit_state = 1;
}
#pragma section all restore