104-SLM130(NB-IOT)C-SDK(OpenCPU)学习开发-定时器
<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/LearnEC616_SLM130" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
说明
一共有6路定时器,0,1,2,3,4,5,可以做6路定时器或者6路PWM
直接看程序吧
1,定时器0每隔1ms进入中断
#include <stdio.h> #include "app.h" #include "bsp.h" #include "slpman_ec616.h" #include "bsp_custom.h" #include "pad_ec616.h" #include "gpio_ec616.h" #include "timer_ec616.h" #define timer_index 0 //定时器0 #define timer_clock_id GPR_TIMER0FuncClk //定时器时钟 #define timer_clock_select GPR_TIMER0ClkSel_26M //选择时钟 #define timer_irqn PXIC_Timer0_IRQn //定时器中断 volatile uint32_t timer_cnt=0; //定时器中断 void timer_interrupt() { if (TIMER_GetInterruptFlags(timer_index) & TIMER_Match0InterruptFlag)//中断 { timer_cnt ++; TIMER_ClearInterruptFlags(timer_index, TIMER_Match0InterruptFlag);//清除中断 } } void timer0_init(void) { TIMER_DriverInit();//初始化 CLOCK_SetClockSrc(timer_clock_id, timer_clock_select);//设置时钟 CLOCK_SetClockDiv(timer_clock_id, 1);//设置时钟分频 //配置定时器 timer_config_t timerConfig; TIMER_GetDefaultConfig(&timerConfig); timerConfig.reloadOption = TIMER_ReloadOnMatch0;//设置第一个中断时间 timerConfig.match0 = 26000;//计数到26000(1ms),产生中断 TIMER_Init(timer_index, &timerConfig); TIMER_InterruptConfig(timer_index, TIMER_Match0Select, TIMER_InterruptLevel);//第一个时间产生中断 TIMER_InterruptConfig(timer_index, TIMER_Match1Select, TIMER_InterruptDisabled); TIMER_InterruptConfig(timer_index, TIMER_Match2Select, TIMER_InterruptDisabled); //设置中断 XIC_SetVector(timer_irqn, timer_interrupt);//设置中断回调函数 XIC_EnableIRQ(timer_irqn);//使能中断 TIMER_Start(timer_index);//打开定时器 } void main_entry(void) { BSP_CommonInit();//官方底层初始化函数,默认写上就可以 BSP_CustomInit();//串口1作为printf timer0_init(); while (1) { if (timer_cnt>=1000)//1S { timer_cnt=0; printf("11223344\r\n"); } } }
2,如果使用定时器1可以把下面的0全部改为1
程序说明
配置时钟,设置中断啥的就不说了.
这个定时器貌似可以设置3个中断时间,下面是只设置了一个,
我也测试了设置多个,有效果,但是好像有个小疑惑.....
一般都是设置一个中断时间,所以就不研究设置多个了.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-06-01 5-HC32F460(华大单片机)-串口(基本使用)