• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

stm32精确延时问题

DengQilong 2017-12-06 浏览量:1023

有时候程序需要让CPU死等一会,一般用定时器或者systick定时器可以实现精确的微秒级延时,但是那样白白占用了定时器,太浪费资源了,且在有os的系统,systick多用于系统心跳。

最近网上看到国外高人使用了这样的延时函数:

rt_err_t rt_hw_delay_init(void) 
{
#if !defined(STM32F0xx)
        uint32_t c;
        
    /* Enable TRC */
    CoreDebug->DEMCR &= ~0x01000000;
    CoreDebug->DEMCR |=  0x01000000;
        
    /* Enable counter */
    DWT->CTRL &= ~0x00000001;
    DWT->CTRL |=  0x00000001;
        
    /* Reset counter */
    DWT->CYCCNT = 0;
        
        /* Check if DWT has started */
        c = DWT->CYCCNT;
        
        /* 2 dummys */
        __ASM volatile ("NOP");
        __ASM volatile ("NOP");
        
        /* if result is zero, DWT has not started */
        if (DWT->CYCCNT - c == 0)
        return RT_ERROR;
#else
        /* Return OK */
        return RT_EOK;
#endif
}
void rt_delay_us(__IO uint32_t micros) 
{
#if !defined(STM32F0xx)
        uint32_t start = DWT->CYCCNT;
        
        /* Go to number of cycles for system */
        micros *= (HAL_RCC_GetHCLKFreq() / 1000000);
        
        /* Delay till end */
        while ((DWT->CYCCNT - start) < micros);
#else
        /* Go to clock cycles */
        micros *= (SystemCoreClock / 1000000) / 5;
        
        /* Wait till done */
        while (micros--);
#endif
}
void rt_delay_ms(__IO uint32_t millis) 
{
    rt_delay_us(millis*1000);
}
有点看不明白,是什么原理,为什么STM32F0就不能用

0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
最佳答案
  • 从代码上看,应该是根据芯片的主频进行分频延时的。
    • 发布于 2017-12-06
    • 举报
    • 评论 1
    • 0
    • 0
电子老工程师 回复了 :有些功能不一样, F28004x支持多达256KB(128KW)的闪存,分为两个128KB(64KW)的存储区,从而可以并行编程和执行。还可以以4KB(2KW)和16KB(8KW)的块提供高达100KB(50KW)的片上SRAM,以进行有效的系统分区。还支持闪存ECC,SRAM ECC /奇偶校验和双区域安全性。 回复

其他答案 数量:0

相关问题

问题达人换一批

stm32精确延时问题