打印
[PIC32/SAM]

Timer定时器

[复制链接]
587|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wiba|  楼主 | 2019-7-25 13:42 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
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位模式,因为分频后的频率数值若是超过计数器长度,则无法正常使用,并且没有提示,你会看到定时器没有起作用。


使用特权

评论回复
沙发
wiba|  楼主 | 2019-7-25 13:42 | 只看该作者

以下使用timer4示例,事实上可以看到,不管配置的是哪个Timer,只要是相同的Instance ID,代码部分没有变化:


1、配置驱动


使用特权

评论回复
板凳
wiba|  楼主 | 2019-7-25 13:42 | 只看该作者
2、生成代码,使用定时器

使用时注意先调用initial(open一个客户端),再调用开始停止



timer.h

/***********************************************
* 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();




使用特权

评论回复
地板
wiba|  楼主 | 2019-7-25 13:43 | 只看该作者
timer.c


#include "timer.h"


void Timer_Tick_Callback( uintptr_t context, uint32_t alarmCount )
{
    //do sometime

   
}

使用特权

评论回复
5
wiba|  楼主 | 2019-7-25 13:43 | 只看该作者
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;
}

使用特权

评论回复
6
wiba|  楼主 | 2019-7-25 13:43 | 只看该作者

//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 : %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;

}

使用特权

评论回复
7
wiba|  楼主 | 2019-7-25 13:44 | 只看该作者
void Timer_Stop()
{
    DRV_TMR_Stop(timerData.drvtimerHandle);
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

77

主题

3305

帖子

3

粉丝