打印
[PIC32/SAM]

内核定时器

[复制链接]
9704|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
nawu|  楼主 | 2021-9-1 16:57 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
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、编译运行将代码烧录到开发板中;


使用特权

评论回复
沙发
nawu|  楼主 | 2021-9-1 16:58 | 只看该作者
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位,因此长时间计数可能存在溢出的情况,该点需要注意。


使用特权

评论回复
板凳
nawu|  楼主 | 2021-9-1 17:00 | 只看该作者
3、实验验证

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


使用特权

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

本版积分规则

72

主题

3307

帖子

3

粉丝