HAL库里面有ms级的延时__weak void HAL_Delay(__IO uint32_t Delay) { uint32_t tickstart = 0; tickstart = HAL_GetTick(); while((HAL_GetTick() - tickstart) < Delay) { } }, 但是如何实现us级的延时呢? 昨天尝试着修改了 __weak HAL_StatusTypeDef HAL_Delay_us(uint32_t TickPriority) { /*Configure the SysTick to have interrupt in 1ms time basis*/ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); /*Configure the SysTick IRQ priority */ HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0); /* Return function status */ return HAL_OK; } 没有实现要求,而且还出现了其他问题,程序不运行,所以想请教一下大家,如何在HAL库中实现us延时? 目前在做DHT11的程序 |
STM32L011D4芯片用SWD无法下载程序
求助:STM32L0系列标准库哪里下?
STM32L051低温时LPUART串口波特率异常
STM32L051单片机ADC如何彻底关闭?
STM32L051C8进入低功耗模式电流偏大
STM32L052C8T6通过I2C模拟读16位数
STM32L0的VLCD外接5V会有问题吗
求助!!使用STM32L073 IO口模拟IIC接口读写AT24CM01 程序不停...
STM32L072RB写Bank2 EEPROM不响应中断
STM32L031无法进入boot
我使用的STM32F030R8 的程序 别的改一下 DS18B20试过没问题 可以达到效果,这个程序我后来移植了好几个项目,成熟度还是可以的,STM32的定时器多可以这么使用。
微秒级延时用函数
*/
void Delay_us(uint16_t nus)
{
__HAL_TIM_SetCounter(&htim17, 0);//htim17 可以更改为任何一个 定时器
__HAL_TIM_ENABLE(&htim17);
while(__HAL_TIM_GetCounter(&htim17) < (48 * nus));//48是系统时钟频率 更换不同的 系列单片机 只修改 48这个数据
/* Disable the Peripheral */
__HAL_TIM_DISABLE(&htim17);
}
《tim.c》
/**
******************************************************************************
* File Name : TIM.c
* Description : This file provides code for the configuration
* of the TIM instances.
******************************************************************************
*
* COPYRIGHT(c) 2016 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim14;
TIM_HandleTypeDef htim16;
TIM_HandleTypeDef htim17;
/* TIM14 init function */
void MX_TIM14_Init(void)
{
htim14.Instance = TIM14;
htim14.Init.Prescaler = 0;
htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
htim14.Init.Period = 65535;
htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
HAL_TIM_Base_Init(&htim14);
}
/* TIM16 init function */
void MX_TIM16_Init(void)
{
htim16.Instance = TIM16;
htim16.Init.Prescaler = TIM16_PSC;
htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
htim16.Init.Period = 65535;
htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
htim16.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim16);
}
/* TIM17 init function */
void MX_TIM17_Init(void)
{
htim17.Instance = TIM17;
htim17.Init.Prescaler = 0;
htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
htim17.Init.Period = 65535;
htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
htim17.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim17);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM14)
{
/* USER CODE BEGIN TIM14_MspInit 0 */
/* USER CODE END TIM14_MspInit 0 */
/* Peripheral clock enable */
__TIM14_CLK_ENABLE();
/* USER CODE BEGIN TIM14_MspInit 1 */
/* USER CODE END TIM14_MspInit 1 */
}
else if(htim_base->Instance==TIM16)
{
/* USER CODE BEGIN TIM16_MspInit 0 */
/* USER CODE END TIM16_MspInit 0 */
/* Peripheral clock enable */
__TIM16_CLK_ENABLE();
/* USER CODE BEGIN TIM16_MspInit 1 */
/* USER CODE END TIM16_MspInit 1 */
}
else if(htim_base->Instance==TIM17)
{
/* USER CODE BEGIN TIM17_MspInit 0 */
/* USER CODE END TIM17_MspInit 0 */
/* Peripheral clock enable */
__TIM17_CLK_ENABLE();
/* USER CODE BEGIN TIM17_MspInit 1 */
/* USER CODE END TIM17_MspInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM14)
{
/* USER CODE BEGIN TIM14_MspDeInit 0 */
/* USER CODE END TIM14_MspDeInit 0 */
/* Peripheral clock disable */
__TIM14_CLK_DISABLE();
/* USER CODE BEGIN TIM14_MspDeInit 1 */
/* USER CODE END TIM14_MspDeInit 1 */
}
else if(htim_base->Instance==TIM16)
{
/* USER CODE BEGIN TIM16_MspDeInit 0 */
/* USER CODE END TIM16_MspDeInit 0 */
/* Peripheral clock disable */
__TIM16_CLK_DISABLE();
/* USER CODE BEGIN TIM16_MspDeInit 1 */
/* USER CODE END TIM16_MspDeInit 1 */
}
else if(htim_base->Instance==TIM17)
{
/* USER CODE BEGIN TIM17_MspDeInit 0 */
/* USER CODE END TIM17_MspDeInit 0 */
/* Peripheral clock disable */
__TIM17_CLK_DISABLE();
/* USER CODE BEGIN TIM17_MspDeInit 1 */
/* USER CODE END TIM17_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void delay_us(uint32_t value)
{
uint32_t i;
i = value * 8;
while(i--);
}
这是我用到L0上的延时,
delay_us(30); //主机拉高20~40us
这是我之前计算出来的,基本上是1us左右,不过没有严格测试,但是驱动DHT11足够了,详细的话可以看我另一个帖子,https://www.stmcu.org.cn/module/forum/thread-606093-1-1.html
这个函数是设置重载寄存器值得,为1ms,如果想设置1us要改为HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000);
因为STM32F073最大32M时钟,函数调用等等操作有可能超过32个时钟,所以用中断函数实现1us延时是有难度的。根据需要可以试试10us或者更大时间的延时
修改成这个样子,昨天试过了,程序下载下去之后直接不运行了,不晓得什么原因
好的,,我去试一下
嗯嗯嗯,我去试一下
感谢哦,,我研究一下,还真没有用过HAL库的定时器呢,,之前都是在用标准库的
你好,能不能麻烦你发一下完整的工程,这段代码不是太明白,。适应STM32CUBE生产的定时器工程也在看,但是和你发的这段代码不太相同