OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 1709|回复: 7

STM32CUBEIDE 滴答定时器->延时函数编写

[复制链接]

53

主题

567

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2099
金钱
2099
注册时间
2017-2-11
在线时间
306 小时
发表于 2020-11-3 13:59:19 | 显示全部楼层 |阅读模式
本帖最后由 jiangyy 于 2021-3-25 13:29 编辑

1.滴答定时器中断配置,默认在HAL_Init(void)初始化了

  1. HAL_StatusTypeDef HAL_Init(void)
  2. {
  3.   /* Configure Flash prefetch, Instruction cache, Data cache */
  4. #if (INSTRUCTION_CACHE_ENABLE != 0U)
  5.   __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  6. #endif /* INSTRUCTION_CACHE_ENABLE */

  7. #if (DATA_CACHE_ENABLE != 0U)
  8.   __HAL_FLASH_DATA_CACHE_ENABLE();
  9. #endif /* DATA_CACHE_ENABLE */

  10. #if (PREFETCH_ENABLE != 0U)
  11.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  12. #endif /* PREFETCH_ENABLE */

  13.   /* Set Interrupt Group Priority */
  14.   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

  15.   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  16.   HAL_InitTick(TICK_INT_PRIORITY);//中断配置,最高

  17.   /* Init the low level hardware */
  18.   HAL_MspInit();

  19.   /* Return function status */
  20.   return HAL_OK;
  21. }
复制代码
2.初始化系统时钟:
  1. void SystemClock_Config(void)
  2. {
  3.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  4.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  5.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  6.   /** Configure the main internal regulator output voltage
  7.   */
  8.   __HAL_RCC_PWR_CLK_ENABLE();
  9.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  10.   /** Initializes the RCC Oscillators according to the specified parameters
  11.   * in the RCC_OscInitTypeDef structure.
  12.   */
  13.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  14.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  15.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  16.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  17.   RCC_OscInitStruct.PLL.PLLM = 4;
  18.   RCC_OscInitStruct.PLL.PLLN = 168;
  19.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  20.   RCC_OscInitStruct.PLL.PLLQ = 7;
  21.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  22.   {
  23.     Error_Handler();
  24.   }
  25.   /** Initializes the CPU, AHB and APB buses clocks
  26.   */
  27.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  28.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  29.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  30.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  31.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  32.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  33.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  34.   {
  35.     Error_Handler();
  36.   }
  37.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  38.   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV8;
  39.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  40.   {
  41.     Error_Handler();
  42.   }
  43. }
复制代码
3.自定义一个延时函数:
3.1 bsp_delay.c
  1. /*
  2. * bsp_delay.c
  3. *
  4. *  Created on: Aug 19, 2020
  5. *      Author: jiangyuanyuan
  6. */

  7. #include "bsp_delay.h"

  8. //初始化延迟函数
  9. //当使用ucos的时候,此函数会初始化ucos的时钟节拍
  10. //SYSTICK的时钟固定为AHB时钟
  11. //SYSCLK:系统时钟频率

  12. void delay_init(void)
  13. {
  14.           HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000);  // 配置并启动系统滴答定时器
  15.           HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  16. }

  17. //延时nus

  18. void delay_us(uint32_t nus)
  19. {
  20.         HAL_Delay(nus);
  21. }

  22. //延时nms
  23. //nms:要延时的ms数
  24. void delay_ms(uint16_t nms)
  25. {
  26.         uint32_t i;
  27.         for(i=0;i<nms;i++) HAL_Delay(1000);
  28. }


  29. /****************************************************************************
  30. * 名                称: void Delay_us(u32 nus)
  31. * 功                能:延时nus
  32. * 入口参数:药延时的微秒数
  33. * 返回参数:无
  34. * 说                明:nus得值,不要大于798915us
  35. ****************************************************************************/
  36. void Delay_us(uint32_t nus)
  37. {
  38.         uint32_t i,j;
  39.   for(i = 0; i < nus; i++)
  40.   {
  41.                 j=10;   //change it  for timing adjustment
  42.                 while(j--)
  43.                 {
  44.                         __NOP();  // nop's may be added or removed for timing
  45.                 }
  46.   }
  47. }

  48. void Delay_ms(uint16_t nms)
  49. {
  50.         uint16_t i,j,k;
  51.         for(i = 0; i < nms; i++)
  52.         {
  53.                 for(j = 0; j < 1000; j++)
  54.                 {
  55.                         k=10;   //change it  for timing adjustment
  56.                         while(k--)
  57.                         {
  58.                                 __NOP();  // nop's may be added or removed for timing
  59.                         }
  60.                 }
  61.         }
  62. }

  63. void delay_Xms(uint16_t nms,uint8_t Cnt)
  64. {
  65.         uint8_t ucCnt = 0;
  66.         for(;ucCnt<Cnt;ucCnt++)
  67.                 delay_ms(nms);
  68. }

复制代码
3.2 bsp_delay.h
  1. /*
  2. * bsp_delay.h
  3. *
  4. *  Created on: Aug 19, 2020
  5. *      Author: jiangyuanyuan
  6. */


  7. #ifndef INC_BSP_DELAY_H_
  8. #define INC_BSP_DELAY_H_

  9. #include "gpio.h"
  10. #include "stm32f4xx_hal.h"
  11. #include "bsp_delay.h"

  12. void delay_init(void);
  13. void delay_ms(uint16_t nms);
  14. void delay_us(uint32_t nus);
  15. void Delay_us(uint32_t nus);
  16. void Delay_ms(uint16_t nms);
  17. #endif /* INC_BSP_delay_H_ */
复制代码
4.主函数main初始化
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=175224]@file[/url]           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&#169; Copyright (c) 2020 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "dac.h"
  24. #include "dma.h"
  25. #include "fatfs.h"
  26. #include "rng.h"
  27. #include "rtc.h"
  28. #include "sdio.h"
  29. #include "spi.h"
  30. #include "tim.h"
  31. #include "usart.h"
  32. #include "usb_device.h"
  33. #include "gpio.h"
  34. #include "fsmc.h"

  35. /* Private includes ----------------------------------------------------------*/
  36. /* USER CODE BEGIN Includes */
  37. #include "stdio.h"
  38. #include "bsp_sys.h"
  39. #include "bsp_led.h"
  40. #include "bsp_key.h"
  41. #include "bsp_delay.h"
  42. #include "bsp_tim3.h"
  43. #include "bsp_errom.h"
  44. #include "bsp_adc.h"
  45. #include "bsp_dac.h"
  46. #include "bsp_pwm.h"
  47. #include "bsp_rtc.h"
  48. #include "bsp_stmflash.h"
  49. #include "bsp_w25q.h"
  50. #include "bsp_fsmc.h"
  51. #include "bsp_malloc.h"
  52. #include "bsp_fatfs.h"
  53. #include "rng.h"
  54. #include "parameter.h"

  55. /* USER CODE END Includes */

  56. /* Private typedef -----------------------------------------------------------*/
  57. /* USER CODE BEGIN PTD */

  58. /* USER CODE END PTD */

  59. /* Private define ------------------------------------------------------------*/
  60. /* USER CODE BEGIN PD */
  61. /* USER CODE END PD */

  62. /* Private macro -------------------------------------------------------------*/
  63. /* USER CODE BEGIN PM */

  64. /* USER CODE END PM */

  65. /* Private variables ---------------------------------------------------------*/

  66. /* USER CODE BEGIN PV */

  67. /* USER CODE END PV */

  68. /* Private function prototypes -----------------------------------------------*/
  69. void SystemClock_Config(void);
  70. /* USER CODE BEGIN PFP */

  71. /* USER CODE END PFP */

  72. /* Private user code ---------------------------------------------------------*/
  73. /* USER CODE BEGIN 0 */

  74. /* USER CODE END 0 */

  75. /**
  76.   * @brief  The application entry point.
  77.   * @retval int
  78.   */
  79. int main(void)
  80. {
  81.   /* USER CODE BEGIN 1 */
  82.         int cnt = 0;
  83.         u16 dacval=0;
  84.         u8 key;
  85.   /* USER CODE END 1 */

  86.   /* MCU Configuration--------------------------------------------------------*/

  87.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  88.   HAL_Init();

  89.   /* USER CODE BEGIN Init */

  90.   /* USER CODE END Init */

  91.   /* Configure the system clock */
  92.   SystemClock_Config();

  93.   /* USER CODE BEGIN SysInit */
  94.   delay_init();
  95.   /* USER CODE END SysInit */

  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init();
  98.   MX_DMA_Init();
  99.   MX_TIM3_Init();
  100.   MX_USART1_UART_Init();
  101.   MX_RNG_Init();
  102.   MX_TIM4_Init();
  103.   MX_ADC1_Init();
  104.   MX_RTC_Init();
  105.   MX_DAC_Init();
  106.   MX_SPI1_Init();
  107.   MX_FSMC_Init();
  108.   MX_SDIO_SD_Init();
  109.   MX_FATFS_Init();
  110.   MX_USB_DEVICE_Init();
  111.   /* USER CODE BEGIN 2 */
  112.   W25QXX_Init();
  113.   HAL_TIM_Base_Start_IT(&htim3);
  114.   HAL_UART_Receive_DMA(&huart1, UsartType1.usartDMA_rxBuf, RECEIVELEN);
  115.   __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  116.   HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
  117.   HAL_DAC_Start(&hdac,DAC_CHANNEL_1);
  118.   /* USER CODE END 2 */

  119.   /* Infinite loop */
  120.   /* USER CODE BEGIN WHILE */
  121.   handle_Param();
  122.   HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0);//初始值为0
  123.   W25QXX_Test();
  124.   fsmc_sram_test();
  125. //  stmflash_test();
  126.   my_mem_init(SRAMEX);                        //初始化外部内存池
  127.   Malloc_Test();
  128.   SD_Auto_NewFolder();

  129.   while (1)
  130.   {
  131.         cnt = RNG_Get_RandomRange(1,10);
  132. //        MRTC_Get();
  133.         Pwm_Test();
  134.         key=KEY_Scan(0);
  135.         if(key==KEY0_PRES)
  136.         {
  137.                 if(dacval<3300)dacval+=300;
  138.                 DAC1_Set_Vol(dacval);
  139.         }else if(key==KEY2_PRES)
  140.         {
  141.                 if(dacval>=300)dacval-=300;
  142.                 else dacval=0;
  143.                 DAC1_Set_Vol(dacval);
  144.         }
  145.         Adc_printf();
  146.     /* USER CODE END WHILE */

  147.     /* USER CODE BEGIN 3 */
  148.         delay_ms(1000);
  149.   }
  150.   /* USER CODE END 3 */
  151. }

  152. /**
  153.   * @brief System Clock Configuration
  154.   * @retval None
  155.   */
  156. void SystemClock_Config(void)
  157. {
  158.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  159.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  160.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  161.   /** Configure the main internal regulator output voltage
  162.   */
  163.   __HAL_RCC_PWR_CLK_ENABLE();
  164.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  165.   /** Initializes the RCC Oscillators according to the specified parameters
  166.   * in the RCC_OscInitTypeDef structure.
  167.   */
  168.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  169.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  170.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  171.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  172.   RCC_OscInitStruct.PLL.PLLM = 4;
  173.   RCC_OscInitStruct.PLL.PLLN = 168;
  174.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  175.   RCC_OscInitStruct.PLL.PLLQ = 7;
  176.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  177.   {
  178.     Error_Handler();
  179.   }
  180.   /** Initializes the CPU, AHB and APB buses clocks
  181.   */
  182.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  183.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  184.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  185.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  186.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  187.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  188.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  189.   {
  190.     Error_Handler();
  191.   }
  192.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  193.   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV8;
  194.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  195.   {
  196.     Error_Handler();
  197.   }
  198. }

  199. /* USER CODE BEGIN 4 */

  200. /* USER CODE END 4 */

  201. /**
  202.   * @brief  This function is executed in case of error occurrence.
  203.   * @retval None
  204.   */
  205. void Error_Handler(void)
  206. {
  207.   /* USER CODE BEGIN Error_Handler_Debug */
  208.   /* User can add his own implementation to report the HAL error return state */
  209.         printf("HAL error!\r\n");
  210.   /* USER CODE END Error_Handler_Debug */
  211. }

  212. #ifdef  USE_FULL_ASSERT
  213. /**
  214.   * @brief  Reports the name of the source file and the source line number
  215.   *         where the assert_param error has occurred.
  216.   * [url=home.php?mod=space&uid=271674]@param[/url]  file: pointer to the source file name
  217.   * @param  line: assert_param error line source number
  218.   * @retval None
  219.   */
  220. void assert_failed(uint8_t *file, uint32_t line)
  221. {
  222.   /* USER CODE BEGIN 6 */
  223.   /* User can add his own implementation to report the file name and line number,
  224.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  225.   /* USER CODE END 6 */
  226. }
  227. #endif /* USE_FULL_ASSERT */

  228. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
4.调试结果
  1. Memory SRAMEX ADDR:0X680EF800
  2. mount sucess!!!
  3. open file sucess!!!
  4. write file sucess!!!
  5. write Data : This is a HDSC Card!!!
  6. close sucess!!!
  7. open file sucess!!!
  8. read sucess!!!
  9. read Data : This is a HDSC Card!!!
  10. close sucess!!!
  11. FatFs is working well!!!
  12. temp = 0.04
  13. temp = 0.05
复制代码


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

53

主题

567

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2099
金钱
2099
注册时间
2017-2-11
在线时间
306 小时
 楼主| 发表于 2020-11-13 18:04:27 | 显示全部楼层

使用道具 举报

53

主题

567

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2099
金钱
2099
注册时间
2017-2-11
在线时间
306 小时
 楼主| 发表于 2021-1-4 19:14:13 | 显示全部楼层
此帖仅作者可见

使用道具 举报

0

主题

5

帖子

0

精华

新手入门

积分
14
金钱
14
注册时间
2019-7-30
在线时间
2 小时
发表于 2021-1-11 09:35:03 | 显示全部楼层
此帖仅作者可见

使用道具 举报

53

主题

567

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2099
金钱
2099
注册时间
2017-2-11
在线时间
306 小时
 楼主| 发表于 2021-1-11 09:49:03 | 显示全部楼层
此帖仅作者可见

使用道具 举报

0

主题

5

帖子

0

精华

新手入门

积分
14
金钱
14
注册时间
2019-7-30
在线时间
2 小时
发表于 2021-1-14 11:17:59 | 显示全部楼层
此帖仅作者可见

使用道具 举报

53

主题

567

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2099
金钱
2099
注册时间
2017-2-11
在线时间
306 小时
 楼主| 发表于 2021-1-15 09:00:36 | 显示全部楼层
此帖仅作者可见

使用道具 举报

53

主题

567

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2099
金钱
2099
注册时间
2017-2-11
在线时间
306 小时
 楼主| 发表于 2021-2-26 10:51:49 | 显示全部楼层
此帖仅作者可见

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-14 00:34

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表