[STM32] Cortex-M内核,基于执行指令计数寄存器的精确延时(nS,uS...

[复制链接]
1472|1
 楼主| nicholasldf 发表于 2016-7-6 15:50 | 显示全部楼层 |阅读模式
本帖最后由 nicholasldf 于 2016-7-7 09:01 编辑

Cortex-M 内核单片机的精确延时方法,ns、us、ms,,不基于任何定时器,,思路来源于uCOS-II官方BSP移植代码。
/*
*********************************************************************************************************
* void Cortex_DWT_Init (void)
* Description : just the same as CPU_TS_TmrInit function, Initialize Cortex-M3 DWT for delay services,
*                                 like delay_ns, delay_us, delay_ms
* Caller(s)   : Application.
*********************************************************************************************************
*/
#define  CORTEX_REG_DEM_CR                       (*(uint32_t *)0xE000EDFC)
#define  CORTEX_REG_DWT_CR                       (*(uint32_t *)0xE0001000)
#define  CORTEX_REG_DWT_CYCCNT                   (*(uint32_t *)0xE0001004)
#define  CORTEX_REG_DBGMCU_CR                    (*(uint32_t *)0xE0042004)
#define  CORTEX_BIT_DEM_CR_TRCENA                (1 << 24)
#define  CORTEX_BIT_DWT_CR_CYCCNTENA             (1 <<  0)
void Cortex_DWT_Init (void)
{
        /* Enable Cortex-M3's DWT CYCCNT reg. */
    CORTEX_REG_DEM_CR     |= (uint32_t)CORTEX_BIT_DEM_CR_TRCENA;
    CORTEX_REG_DWT_CYCCNT  = (uint32_t)0u;
    CORTEX_REG_DWT_CR     |= (uint32_t)CORTEX_BIT_DWT_CR_CYCCNTENA;
}

/*
*********************************************************************************************************
* void delay_us (uint32_t number)
* Description : delay service for specified time that often smaller than 1mS
* number      : time to delay in uS
*********************************************************************************************************
*/
void delay_us (uint32_t number)
{
    uint32_t  init_cnts, total, now;
        
        total = BSP_CPU_ClkFreq()/1000000*number;
    init_cnts = CORTEX_REG_DWT_CYCCNT;
        do{
                now = CORTEX_REG_DWT_CYCCNT;
        }while( (now - init_cnts) < total);
}
//delay_ns
void delay_ns (uint32_t number)
{
    uint32_t  init_cnts, total, now;
        
        total = BSP_CPU_ClkFreq()*number/1000000000;
    init_cnts = CORTEX_REG_DWT_CYCCNT;
        do{
                now = CORTEX_REG_DWT_CYCCNT;
        }while( (now - init_cnts) < total);
}
//delay_ms
void delay_ms (uint32_t number)
{
        while(number--) delay_us(1000);
}
jackhwang 发表于 2016-7-11 11:38 | 显示全部楼层
初学者没有看明白
您需要登录后才可以回帖 登录 | 注册

本版积分规则

61

主题

261

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部