【PIC32MZ】Timer定时器
来源:互联网 发布:英制螺纹程序怎么编程 编辑:程序博客网 时间:2024/05/13 08:52
PIC32MZ有多达9组Timer,但是并非任何时候都能同时使用,主要是在使用32位计数器时的问题。
Timer的2、4、6、8定时器支持16位和32位,但是在使用32位,占用的ID并非自身,而是下一个。以Timer4为例,在system_interrupt中可以看到,若是使用16位模式,显示为Timer4,而使用32位时,其使用的是Timer5。如果同时使用Timer4的32位和Timer5,生成代码时,则会出现冲突提示。
还有就是设置系统频率时需要注意,若是要求较短间隔(us),最好使用32位模式,因为分频后的频率数值若是超过计数器长度,则无法正常使用,并且没有提示,你会看到定时器没有起作用。
以下使用timer4示例,事实上可以看到,不管配置的是哪个Timer,只要是相同的Instance ID,代码部分没有变化:
1、配置驱动
2、生成代码,使用定时器
使用时注意先调用initial(open一个客户端),再调用开始停止
/*********************************************** * This is a timer for active motor * Timer 4 32bit ************************************************/#ifndef _MOTOR_TIMER_H#define _MOTOR_TIMER_H#include <stdint.h>#include <stdbool.h>#include <stddef.h>#include <stdlib.h>#include "system_config.h"#include "system_definitions.h"typedef struct{ /*** uart ******/ DRV_HANDLE drvTimerHandle; uint8_t timerGap; uint32_t timerTotalCount; uint32_t timerCurrentCount;}TIMERS_DATA;TIMERS_DATA timerData;bool Timer_Init(void);void Timer_Start(uint32_t milliSeconds , uint32_t TotalCount);void Timer_Stop();#endif
#include "timer.h"void Timer_Tick_Callback( uintptr_t context, uint32_t alarmCount ){ //do sometime }bool Timer_Init(void){ timerData.timerTotalCount = 0; timerData.timerCurrentCount = 0; timerData.timerGap = 0; //initial timer4, id0 timerData.drvtimerHandle = DRV_TMR_Open(DRV_TMR_INDEX_0,DRV_IO_INTENT_READ ); /* Check the USART1 driver handler */ if (timerData.drvtimerHandle == DRV_HANDLE_INVALID ) { return false; } return true;}//start timer with microSecondsvoid Timer_Start(uint32_t microSeconds, uint32_t TotalCount){ //maybe some is invalib ,such TMR_PRESCALE_VALUE_4, DRV_TMR_CLKSOURCE_INTERNAL is Peripheral clock,now is 64,000,000 Hz DRV_TMR_ClockSet(timerData.drvtimerHandle,DRV_TMR_CLKSOURCE_INTERNAL,TMR_PRESCALE_VALUE_64); //get timer input clock frequency,100ns uint32_t frequency = DRV_TMR_CounterFrequencyGet(timerData.drvtimerHandle); Uart_printf("Timer 2 Frequency is : %u\n",frequency); uint32_t us = frequency/1000000; Uart_printf("Timer 2 half period is : %u\n",us*microSeconds); //Set timer gap and register callback function DRV_TMR_AlarmRegister(timerData.drvtimerHandle, us*microSeconds, true, 0, (DRV_TMR_CALLBACK)Timer_Tick_Callback); //enable the alarm DRV_TMR_AlarmEnable(timerData.drvtimerHandle, true); //start timer 2 DRV_TMR_Start(timerData.drvtimerHandle); OnePulseEnd = true; timerData.timerTotalCount = TotalCount; // one pulse is 0_1- timerData.timerCurrentCount = 0; timerData.timerGap = microSeconds;}void Timer_Stop(){ DRV_TMR_Stop(timerData.drvtimerHandle);}
- 【PIC32MZ】Timer定时器
- Timer 定时器
- TIMER 定时器
- timer 定时器
- timer 定时器
- timer定时器
- 定时器 Timer
- 定时器Timer
- timer定时器
- Timer 定时器
- Timer定时器
- 定时器TIMER
- 定时器Timer
- timer定时器
- Timer 定时器
- Timer定时器
- 定时器 timer
- 定时器 Timer
- 【PIC32MZ】开发环境的搭建
- 【PIC32MZ】点亮LED
- 【PIC32MZ】按键中断
- 【PIC32MZ】Usart串口通讯
- 二叉排序树的插入、遍历和删除
- 【PIC32MZ】Timer定时器
- UEditor不能获取文章内容的解决办法
- springMVC访问不了后台问题
- 【PIC32MZ】PWM呼吸灯
- CSDN粘贴代码显示行号,并实现代码高亮
- 【PIC32MZ】I2C通信
- 【PIC32MZ】OLED12864 I2C显式示例
- 使用python编写的落网电台下载工具
- 现操-week8