完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
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的程序 |
|
相关推荐
20个回答
|
|
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
这个函数是设置重载寄存器值得,为1ms,如果想设置1us要改为HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000); 因为STM32F073最大32M时钟,函数调用等等操作有可能超过32个时钟,所以用中断函数实现1us延时是有难度的。根据需要可以试试10us或者更大时间的延时 |
|
|
|
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000);
修改成这个样子,昨天试过了,程序下载下去之后直接不运行了,不晓得什么原因 |
|
|
|
函数库里写明了HAL_Delay会用于HAL库本身配置过程中的很多地方,不能随便改,,可以用另外一个定时器实现
|
|
|
|
还是使用定时器实现吧,那样更好操作些
|
|
|
|
|
|
|
|
嗯嗯嗯,我去试一下 |
|
|
|
HAL_Delay 这个函数里面有说明的,它就是毫秒级的延时呀。
|
|
|
|
用systick写个定时,可以到1us,挺准的
|
|
|
|
试了,,但是也是不行,今天晚上回去写个像用51时的延时函数,不行的话就开个定时器
|
|
|
|
我去试一下
|
|
|
|
本帖最后由 jtc_88 于 2016-5-12 08:26 编辑
我使用的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****/ |
|
|
|
吔屎蛋拉雷 发表于 2018-10-31 14:33 感谢哦,,我研究一下,还真没有用过HAL库的定时器呢,,之前都是在用标准库的 |
|
|
|
吔屎蛋拉雷 发表于 2018-10-31 14:33 你好,能不能麻烦你发一下完整的工程,这段代码不是太明白,。适应STM32CUBE生产的定时器工程也在看,但是和你发的这段代码不太相同 |
|
|
|
是不是写IIC的那个孩子? nop啊
|
|
|
|
|
|
|
|
|
|
|
|
汗,DHT11不是IIC驱动的,是单总线的,,,我没有在群里问us级延时,只在这边论坛发的帖子 |
|
|
|
好吧,我记错了。 SHT21 是用的IIC。。。 |
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2033 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1885 浏览 3 评论
4475 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2031 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2541 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 19:25 , Processed in 1.092588 second(s), Total 109, Slave 93 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号