历史上的今天
今天是:2025年03月25日(星期二)
2020年03月25日 | 【PIC32MZ】Timer定时器
2020-03-25 来源:eefocus
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一个客户端),再调用开始停止
timer.h
/***********************************************
* This is a timer for active motor
* Timer 4 32bit
************************************************/
#ifndef _MOTOR_TIMER_H
#define _MOTOR_TIMER_H
#include #include #include #include #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 timer.c #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 microSeconds void 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 : %un",frequency); uint32_t us = frequency/1000000; Uart_printf("Timer 2 half period is : %un",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); }
史海拾趣
|
AV4061/4062型频谱分析仪是一款高性能、经济型的射频信号分析仪。仪器采用了全频段数字扫描锁相本地振荡器和数字检波技术,以及6.4\"高亮度TFT LCD显示器,具有准确的频率、幅度测量精度和灵活的多功能扩展能力。可广泛应用于数字移动通信、CATV测 ...… 查看全部问答> |
|
我最近在网上找到这么个简单的基于51单片机的多任务操作系统,但是我不知道其中task-swith和task-load这两个函数是怎么切换任务的,用堆栈管理实现任务的切换,请高手帮我解释下,谢谢 我主要想知道51单片机怎样去识别任务切换 程序 /* ...… 查看全部问答> |
|
系统:s3c2440a +wince5 想在系统运行时,关闭UPLL,不产生48Mhz,在需要时再打开UPLL 查看s3c2440a的datasheet,上写控制CLKSLOW寄存器的UCLK_ON即可 CLKSLOW[7] --> UCLK_ON --> = 0 UCLK_ON (UPLL turn on)   ...… 查看全部问答> |
|
我想改变列表框的列头的文本,代码如下: LVCOLUMN lvColumn; lvColumn.mask = LVCF_TEXT | LVCF_WIDTH; pMethodManageDlg->m_MethodList.GetColumn(1, ...… 查看全部问答> |
|
我现在手上有一个针对某个设备的 WinCE5.0 SDK包。 如何知道其在原版WinCE上做了哪些裁剪? 相关目录如下: [Atl] [Help] [Include] & ...… 查看全部问答> |
|
如果想在wince中自己开发一个中文输入法,要是用EVC的话,应该如果建立拼音和汉字字库的检索? 有没有相关的API函数? 要是想开发一个手写中文输入软件呢?都需要怎么去开发?… 查看全部问答> |




