14、PIC32系列-内核定时器

PIC32系列-内核定时器

1、使用Harmony配置内核定时器

2、实际代码分析

3、实验验证


1、使用Harmony配置内核定时器

1、在Available Components组件中将CORE TIMER添加到Project Graph中;

2、组件添加后,组件初始内容如下;

其中Enable Interrupt Mode是使能中断模式,勾选后会自动生成中断相关代码;

Stop Timer in Debug mode是在空闲模式下的操作,勾选后在空闲模式下停止模块;

Compare period(milliseconds)是比较周期,系统时间频率是4MHz;

Enable Interrupt Mode显示如下

 3、点击Generate Code生成代码;

4、代码生成后需要的操作;

1、系统初始化完成后添加系统延时初始化函数;

2、添加应用层操作函数;

5、编译运行将代码烧录到开发板中;

2、实际代码分析

在plib_coretimer.c文件

static uint32_t compareValue = CORE_TIMER_COMPARE_VALUE;
//内核定时器初始化
void CORETIMER_Initialize()
{
    // Clear Core Timer
    _CP0_SET_COUNT(0);
    _CP0_SET_COMPARE(compareValue);

    // Enable Timer by clearing Disable Count (DC) bit
    _CP0_SET_CAUSE(_CP0_GET_CAUSE() & (~_CP0_CAUSE_DC_MASK));
}

//内核定时器启动
void CORETIMER_Start( void )
{
    // Disable Timer by setting Disable Count (DC) bit
    _CP0_SET_CAUSE(_CP0_GET_CAUSE() | _CP0_CAUSE_DC_MASK);

    // Clear Compare Timer Interrupt Flag
    IFS0CLR=0x1;

    // Clear Core Timer
    _CP0_SET_COUNT(0);

    _CP0_SET_COMPARE(compareValue);

    // Enable Timer by clearing Disable Count (DC) bit
    _CP0_SET_CAUSE(_CP0_GET_CAUSE() & (~_CP0_CAUSE_DC_MASK));

}

//内核定时器停止
void CORETIMER_Stop( void )
{
    // Disable Timer by setting Disable Count (DC) bit
    _CP0_SET_CAUSE(_CP0_GET_CAUSE() | _CP0_CAUSE_DC_MASK);
}

//获取内核定时器频率
uint32_t CORETIMER_FrequencyGet ( void )
{
    return (CORE_TIMER_FREQUENCY);
}

//设置内核定时器频率
void CORETIMER_CompareSet ( uint32_t compare )
{
    compareValue = compare;
    _CP0_SET_COMPARE(compareValue);
}

//获取内核定时器计数值
uint32_t CORETIMER_CounterGet ( void )
{
    uint32_t count;
    count = _CP0_GET_COUNT();
    return count;
}

//
bool CORETIMER_CompareHasExpired( void )
{
    if (IFS0bits.CTIF != 0)
    {
        // Clear Compare Timer Interrupt Flag
        IFS0CLR=0x1;

        return true;
    }

    return false;
}

//内核定时器毫秒延时
void CORETIMER_DelayMs ( uint32_t delay_ms)
{
    uint32_t startCount, endCount;
    /* Calculate the end count for the given delay */
    endCount=(CORE_TIMER_FREQUENCY/1000)*delay_ms;
    startCount=_CP0_GET_COUNT();
    while((_CP0_GET_COUNT()-startCount)<endCount);
}

//内核定时器微秒延时
void CORETIMER_DelayUs ( uint32_t delay_us)
{
    uint32_t startCount, endCount;
    /* Calculate the end count for the given delay */
    endCount=(CORE_TIMER_FREQUENCY/1000000)*delay_us;
    startCount=_CP0_GET_COUNT();
    while((_CP0_GET_COUNT()-startCount)<endCount);
}

需要注意的点:毫秒延时函数以及微秒延时函数采用当前值减去起始值,再与需要计数的值进行比较,数值类型均为32位,因此长时间计数可能存在溢出的情况,该点需要注意。

3、实验验证

1、使用内核定时器毫秒延时函数,LED灯延时500ms翻转,编译完成后烧录到开发版中,LED灯闪烁。

时间:2021.08.18

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huangtop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值