US延时1--基于HAL_Delay修改时基

一个软IIC的模块【模仿刘洋老师】
准备工作US的延时函数:

方法1:
原文是
HAL_Delay(500);


它是MS的延时函数
__weak void HAL_Delay(uint32_t Delay)
{
  uint32_t tickstart = HAL_GetTick();
  uint32_t wait = Delay;

  /* Add a period to guaranty minimum wait */
  if (wait < HAL_MAX_DELAY)
  {
    wait += (uint32_t)(uwTickFreq);
  }

  while((HAL_GetTick() - tickstart) < wait)
  {
  }
}
也就是 Delay 计数一个数 就是1MS 我要操作为一个数就是1US 怎么办?
继续分析
__weak uint32_t HAL_GetTick(void)
{
  return uwTick;
}
__weak void HAL_IncTick(void)
{
  uwTick += uwTickFreq;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM6) {
    HAL_IncTick();
  }
}
也就是一个全局变量 uwTick 每次TIM6的周期到的时候 就 执行HAL_IncTick 也就是 uwTick += uwTickFreq;那么步进值就是 TIM6+uwTickFreq 共同作用的!

uint32_t uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */

#define  HAL_TICK_FREQ_10HZ         100U
#define  HAL_TICK_FREQ_100HZ        10U
#define  HAL_TICK_FREQ_1KHZ         1U
#define  HAL_TICK_FREQ_DEFAULT      HAL_TICK_FREQ_1KHZ
也就是 uwTickFreq = 1
也就是+1

它是如何控制到时钟1MS加一次的呢?
__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
  HAL_StatusTypeDef  status = HAL_OK;

  if (uwTickFreq != 0U)
  {
    /*Configure the SysTick to have interrupt in 1ms time basis*/
    if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq)) == 0U)
也就是上面的函数可以配置出1MS加一次的时基!
注意 我选的是TIM6 上海的函数重新写了一次
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
  RCC_ClkInitTypeDef    clkconfig;
  uint32_t              uwTimclock = 0;
  uint32_t              uwPrescalerValue = 0;
  uint32_t              pFLatency;
  
  /*Configure the TIM6 IRQ priority */
  HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0); 
  
  /* Enable the TIM6 global Interrupt */
  HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); 
  
  /* Enable TIM6 clock */
  __HAL_RCC_TIM6_CLK_ENABLE();
  
  /* Get clock configuration */
  HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  
  /* Compute TIM6 clock */
  uwTimclock = HAL_RCC_GetPCLK1Freq();
   
  /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
  uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
  
  /* Initialize TIM6 */
  htim6.Instance = TIM6;
  
  /* Initialize TIMx peripheral as follow:
  + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
  + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  + ClockDivision = 0
  + Counter direction = Up
  */
  htim6.Init.Period = (1000000 / 1000) - 1;
  htim6.Init.Prescaler = uwPrescalerValue;
  htim6.Init.ClockDivision = 0;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  if(HAL_TIM_Base_Init(&htim6) == HAL_OK)	


如何修改呢?
先研究一下

#include "gunit.h"
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
  RCC_ClkInitTypeDef    clkconfig;
  uint32_t              uwTimclock = 0;
  uint32_t              uwPrescalerValue = 0;
  uint32_t              pFLatency;
  
  /*Configure the TIM6 IRQ priority */
  HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0); 
  
  /* Enable the TIM6 global Interrupt */
  HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); 
  
  /* Enable TIM6 clock */
  __HAL_RCC_TIM6_CLK_ENABLE();
  
  /* Get clock configuration */
  HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  
  /* Compute TIM6 clock */
  uwTimclock = HAL_RCC_GetPCLK1Freq();
   
printf("uwTimclock=%d\r\n",uwTimclock);

注意是不行的!不能printf!因为时钟还是最前面的函数 USART还没有好呢!!!
只能debug自己去看!!
uwTimclock -----------【80000000】
uwPrescalerValue -----【79】

这样去初始化TIM6就是
  htim6.Init.Period = (1000000 / 1000) - 1;-----1000
  htim6.Init.Prescaler = uwPrescalerValue;------80
  
  好了 周期是[以前STM32F103是72M现在是80M 看MX的时钟树]  80 000 000 /1 000 *80 =1000  频率是1KHZ 周期是1MS
  
  我希望是频率是 1MHZ 周期是1US 也就是结果是1000-->1000000 分母【1 000 *80】缩小1000倍即可
  修改   htim6.Init.Period = (1000 / 1000) - 1;-----1
  
  有点儿问题 死机了!
  
修改另外一个
htim6.Init.Prescaler = uwPrescalerValue;------80
它是
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
修改为:  uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000000) - 1);

一样也是死机【故意测试的 你已经0-1的 必须死机】

需要同时修改 来完成缩小1000的目标!
uwPrescalerValue = (uint32_t) ((uwTimclock / 10000000) - 1);//1ge 0
htim6.Init.Period = (1000000 / 100000) - 1;

此时完成了!!!
HAL_Delay(500000);完成了以前 延时500MS的功能!!以前是HAL_Delay(500);现在多了3个0
 
  

  
  

 

你可能感兴趣的:(US延时1--基于HAL_Delay修改时基)