STM32HAL库微秒延时函数的实现---DWT和SysTick
以下两种延时方式来源:Arduino_Core_STM32源码delayMicroseconds(uint32_t us)函数的实现。利用SysTick再实现微秒延时函数虽然SysTick已经被配置为1ms中断一次的模式,但每个1ms之间SysTick的当前值寄存器是一直在计数的(每计一个数的时间是1/SytemCoreClock)我们便可以利用该机制实现微秒延时函数。
void delayMicroseconds(uint32_t us){ __IO uint32_t currentTicks = SysTick->VAL;/* Number of ticks per millisecond */const uint32_t tickPerMs = SysTick->LOAD + 1;/* Number of ticks to count */const uint32_t nbTicks = ((us - ((us > 0) ? 1 : 0)) * tickPerMs) / 1000;/* Number of elapsed ticks */uint32_t elapsedTicks = 0;__IO uint32_t oldTicks = currentTicks;do { currentTicks = SysTick->VAL; elapsedTicks += (oldTicks < currentTicks) ? tickPerMs + oldTicks - currentTicks : oldTicks - currentTicks; oldTicks = currentTicks;} while (nbTicks > elapsedTicks);}
以上函数可以直接复制到工程中使用,不需要额外的任何配置。http://6.eewimg.cn/news/uploadfile/2022/0809/20220809090806513.pngNote
虽然函数参数us为uint32_t类型,但是延时数不能过大,原因自己分析。建议超过1ms的延时时间使用HAL_Delay()。
利用DWT(数据观测点)实现微秒延时函数对于DWT大家可以搜索具体了解,这里我也不是很了解,就直说实现方法好了。
dwt.h文件/********************************************************************************* @file dwt.h* @authorFrederic Pillon* @brief Header for dwt.c module******************************************************************************* @attention** Copyright (c) 2019, STMicroelectronics* All rights reserved.** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/#ifndef _DWT_H_#define _DWT_H_
#include "stm32f4xx.h"#include
#ifndef UNUSED#define UNUSED(x) (void)x#endif
#ifdef DWT_BASEuint32_t dwt_init(void);
#ifdef __cplusplusextern "C" {#endif
//uint32_t dwt_init(void);void dwt_access(bool ena);
static inline uint32_t dwt_max_sec(void){return (UINT32_MAX / SystemCoreClock);};
static inline uint32_t dwt_max_msec(void){return (UINT32_MAX / (SystemCoreClock / 1000));};
static inline uint32_t dwt_max_usec(void){return (UINT32_MAX / (SystemCoreClock / 1000000));};
static inline uint32_t dwt_getCycles(void){return (DWT->CYCCNT);};
#ifdef __cplusplus}#endif
#endif /* DWT_BASE */#endif /* _DWT_H_ */
dwt.c文件/********************************************************************************* @file dwt.c* @authorFrederic Pillon* @brief Provide Data Watchpoint and Trace services******************************************************************************* @attention** Copyright (c) 2019, STMicroelectronics* All rights reserved.** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************/
#include "dwt.h"
#ifdef DWT_BASE#ifdef __cplusplusextern "C" {#endif
uint32_t dwt_init(void){
/* Enable use of DWT */if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) { CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;}
/* Unlock */dwt_access(true);
/* Reset the clock cycle counter value */DWT->CYCCNT = 0;
/* Enableclock cycle counter */DWT->CTRL |=DWT_CTRL_CYCCNTENA_Msk;
/* 3 NO OPERATION instructions */__asm volatile(" nop nt" " nop nt" " nop nt");
/* Check if clock cycle counter has started */return (DWT->CYCCNT) ? 0 : 1;}
void dwt_access(bool ena){#if (__CORTEX_M == 0x07U)/* * Define DWT LSR mask which is (currentuly) not defined by the CMSIS. * Same as ITM LSR one. */#if !defined DWT_LSR_Present_Msk#define DWT_LSR_Present_Msk ITM_LSR_Present_Msk#endif#if !defined DWT_LSR_Access_Msk#define DWT_LSR_Access_Msk ITM_LSR_Access_Msk#endifuint32_t lsr = DWT->LSR;
if ((lsr & DWT_LSR_Present_Msk) != 0) { if (ena) { if ((lsr & DWT_LSR_Access_Msk) != 0) { //locked DWT->LAR = 0xC5ACCE55; } } else { if ((lsr & DWT_LSR_Access_Msk) == 0) { //unlocked DWT->LAR = 0; } }}#else /* __CORTEX_M */UNUSED(ena);#endif /* __CORTEX_M */}
#ifdef __cplusplus}#endif
#endif
delayMicroseconds()函数void delayMicroseconds(uint32_t us){#if defined(DWT_BASE) && !defined(DWT_DELAY_DISABLED)int32_t start= dwt_getCycles();int32_t cycles = us * (SystemCoreClock / 1000000);
while ((int32_t)dwt_getCycles() - start < cycles);#endif}
void delayMicroseconds(uint32_t us)
{
__IO uint32_t currentTicks = SysTick->VAL;
/* Number of ticks per millisecond */
const uint32_t tickPerMs = SysTick->LOAD + 1;
/* Number of ticks to count */
const uint32_t nbTicks = ((us - ((us > 0) ? 1 : 0)) * tickPerMs) / 1000;
/* Number of elapsed ticks */
uint32_t elapsedTicks = 0;
__IO uint32_t oldTicks = currentTicks;
do {
currentTicks = SysTick->VAL;
elapsedTicks += (oldTicks < currentTicks) ? tickPerMs + oldTicks - currentTicks :
oldTicks - currentTicks;
oldTicks = currentTicks;
} while (nbTicks > elapsedTicks);
}
引线长度就决定了其响应信号的波长
数字电压表(DVM)或仪器来测量效率
靠近功率开关管或整流器的电容分到的纹波电流远多于相距较远的电容分到的纹波电流
任何一条PCB引线都可能成为天线
滤波电容、功率开关管或整流器、电感或变压器
离线式变换器
交流能量就会从输入或输出滤波电容上流进流出
经变压器映射成这么多节点
专门的滤波器来阻止交流噪声进入周围的电路
电流型变换器电流检测电阻的公共接点和输出端电阻分压器的下端
页:
[1]