• STM32F103ZE单片机呼吸灯源代码


    1. //这个是中断文件
    2. /* USER CODE BEGIN Header */
    3. /**
    4. ******************************************************************************
    5. * @file stm32f1xx_it.c
    6. * @brief Interrupt Service Routines.
    7. ******************************************************************************
    8. * @attention
    9. *
    10. * Copyright (c) 2023 STMicroelectronics.
    11. * All rights reserved.
    12. *
    13. * This software is licensed under terms that can be found in the LICENSE file
    14. * in the root directory of this software component.
    15. * If no LICENSE file comes with this software, it is provided AS-IS.
    16. *
    17. ******************************************************************************
    18. */
    19. /* USER CODE END Header */
    20. /* Includes ------------------------------------------------------------------*/
    21. #include "main.h"
    22. #include "stm32f1xx_it.h"
    23. /* Private includes ----------------------------------------------------------*/
    24. /* USER CODE BEGIN Includes */
    25. /* USER CODE END Includes */
    26. /* Private typedef -----------------------------------------------------------*/
    27. /* USER CODE BEGIN TD */
    28. /* USER CODE END TD */
    29. /* Private define ------------------------------------------------------------*/
    30. /* USER CODE BEGIN PD */
    31. /* USER CODE END PD */
    32. /* Private macro -------------------------------------------------------------*/
    33. /* USER CODE BEGIN PM */
    34. /* USER CODE END PM */
    35. /* Private variables ---------------------------------------------------------*/
    36. /* USER CODE BEGIN PV */
    37. /* USER CODE END PV */
    38. /* Private function prototypes -----------------------------------------------*/
    39. /* USER CODE BEGIN PFP */
    40. /* USER CODE END PFP */
    41. /* Private user code ---------------------------------------------------------*/
    42. /* USER CODE BEGIN 0 */
    43. /* USER CODE END 0 */
    44. /* External variables --------------------------------------------------------*/
    45. extern TIM_HandleTypeDef htim2;
    46. /* USER CODE BEGIN EV */
    47. //产生正弦值的函数
    48. #include "math.h"
    49. void CreateSin(uint32_t T,uint8_t repeatTime,TIM_HandleTypeDef * htimx,TIM_TypeDef * timx,uint32_t TIM_CHANNEL_X)
    50. {
    51. static float Y=0,X=0;
    52. static uint32_t flag=0;
    53. //设置分频数
    54. if(flag==0)
    55. {//这个数不是指针无法发挥效果
    56. timx->PSC=T*HAL_RCC_GetSysClockFreq()/(float)(htimx->Init.Period)/repeatTime;
    57. }
    58. flag++;if(flag==65536*65536-1)flag=1;
    59. Y=sin(X)*htimx->Init.Period;
    60. if(X>3.1415926)X=0;
    61. X+=T/(float)repeatTime;
    62. __HAL_TIM_SET_COMPARE(htimx,TIM_CHANNEL_X, Y);
    63. }
    64. /* USER CODE END EV */
    65. /******************************************************************************/
    66. /* Cortex-M3 Processor Interruption and Exception Handlers */
    67. /******************************************************************************/
    68. /**
    69. * @brief This function handles Non maskable interrupt.
    70. */
    71. void NMI_Handler(void)
    72. {
    73. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
    74. /* USER CODE END NonMaskableInt_IRQn 0 */
    75. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
    76. while (1)
    77. {
    78. }
    79. /* USER CODE END NonMaskableInt_IRQn 1 */
    80. }
    81. /**
    82. * @brief This function handles Hard fault interrupt.
    83. */
    84. void HardFault_Handler(void)
    85. {
    86. /* USER CODE BEGIN HardFault_IRQn 0 */
    87. /* USER CODE END HardFault_IRQn 0 */
    88. while (1)
    89. {
    90. /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    91. /* USER CODE END W1_HardFault_IRQn 0 */
    92. }
    93. }
    94. /**
    95. * @brief This function handles Memory management fault.
    96. */
    97. void MemManage_Handler(void)
    98. {
    99. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
    100. /* USER CODE END MemoryManagement_IRQn 0 */
    101. while (1)
    102. {
    103. /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    104. /* USER CODE END W1_MemoryManagement_IRQn 0 */
    105. }
    106. }
    107. /**
    108. * @brief This function handles Prefetch fault, memory access fault.
    109. */
    110. void BusFault_Handler(void)
    111. {
    112. /* USER CODE BEGIN BusFault_IRQn 0 */
    113. /* USER CODE END BusFault_IRQn 0 */
    114. while (1)
    115. {
    116. /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    117. /* USER CODE END W1_BusFault_IRQn 0 */
    118. }
    119. }
    120. /**
    121. * @brief This function handles Undefined instruction or illegal state.
    122. */
    123. void UsageFault_Handler(void)
    124. {
    125. /* USER CODE BEGIN UsageFault_IRQn 0 */
    126. /* USER CODE END UsageFault_IRQn 0 */
    127. while (1)
    128. {
    129. /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    130. /* USER CODE END W1_UsageFault_IRQn 0 */
    131. }
    132. }
    133. /**
    134. * @brief This function handles System service call via SWI instruction.
    135. */
    136. void SVC_Handler(void)
    137. {
    138. /* USER CODE BEGIN SVCall_IRQn 0 */
    139. /* USER CODE END SVCall_IRQn 0 */
    140. /* USER CODE BEGIN SVCall_IRQn 1 */
    141. /* USER CODE END SVCall_IRQn 1 */
    142. }
    143. /**
    144. * @brief This function handles Debug monitor.
    145. */
    146. void DebugMon_Handler(void)
    147. {
    148. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
    149. /* USER CODE END DebugMonitor_IRQn 0 */
    150. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
    151. /* USER CODE END DebugMonitor_IRQn 1 */
    152. }
    153. /**
    154. * @brief This function handles Pendable request for system service.
    155. */
    156. void PendSV_Handler(void)
    157. {
    158. /* USER CODE BEGIN PendSV_IRQn 0 */
    159. /* USER CODE END PendSV_IRQn 0 */
    160. /* USER CODE BEGIN PendSV_IRQn 1 */
    161. /* USER CODE END PendSV_IRQn 1 */
    162. }
    163. /**
    164. * @brief This function handles System tick timer.
    165. */
    166. void SysTick_Handler(void)
    167. {
    168. /* USER CODE BEGIN SysTick_IRQn 0 */
    169. /* USER CODE END SysTick_IRQn 0 */
    170. HAL_IncTick();
    171. /* USER CODE BEGIN SysTick_IRQn 1 */
    172. /* USER CODE END SysTick_IRQn 1 */
    173. }
    174. /******************************************************************************/
    175. /* STM32F1xx Peripheral Interrupt Handlers */
    176. /* Add here the Interrupt Handlers for the used peripherals. */
    177. /* For the available peripheral interrupt handler names, */
    178. /* please refer to the startup file (startup_stm32f1xx.s). */
    179. /******************************************************************************/
    180. /**
    181. * @brief This function handles RCC global interrupt.
    182. */
    183. void RCC_IRQHandler(void)
    184. {
    185. /* USER CODE BEGIN RCC_IRQn 0 */
    186. /* USER CODE END RCC_IRQn 0 */
    187. /* USER CODE BEGIN RCC_IRQn 1 */
    188. /* USER CODE END RCC_IRQn 1 */
    189. }
    190. /**
    191. * @brief This function handles TIM2 global interrupt.
    192. */
    193. void TIM2_IRQHandler(void)
    194. {
    195. /* USER CODE BEGIN TIM2_IRQn 0 */
    196. /* USER CODE END TIM2_IRQn 0 */
    197. HAL_TIM_IRQHandler(&htim2);
    198. /* USER CODE BEGIN TIM2_IRQn 1 */
    199. CreateSin(4,160,&htim2,TIM2,TIM_CHANNEL_1);
    200. HAL_TIM_Base_Start_IT(&htim2);
    201. /* USER CODE END TIM2_IRQn 1 */
    202. }
    203. /* USER CODE BEGIN 1 */
    204. /* USER CODE END 1 */

    1. //这个是main文件
    2. /* USER CODE BEGIN Header */
    3. /**
    4. ******************************************************************************
    5. * @file : main.c
    6. * @brief : Main program body
    7. ******************************************************************************
    8. * @attention
    9. *
    10. * Copyright (c) 2023 STMicroelectronics.
    11. * All rights reserved.
    12. *
    13. * This software is licensed under terms that can be found in the LICENSE file
    14. * in the root directory of this software component.
    15. * If no LICENSE file comes with this software, it is provided AS-IS.
    16. *
    17. ******************************************************************************
    18. */
    19. /* USER CODE END Header */
    20. /* Includes ------------------------------------------------------------------*/
    21. #include "main.h"
    22. #include "tim.h"
    23. #include "gpio.h"
    24. /* Private includes ----------------------------------------------------------*/
    25. /* USER CODE BEGIN Includes */
    26. /* USER CODE END Includes */
    27. /* Private typedef -----------------------------------------------------------*/
    28. /* USER CODE BEGIN PTD */
    29. /* USER CODE END PTD */
    30. /* Private define ------------------------------------------------------------*/
    31. /* USER CODE BEGIN PD */
    32. /* USER CODE END PD */
    33. /* Private macro -------------------------------------------------------------*/
    34. /* USER CODE BEGIN PM */
    35. /* USER CODE END PM */
    36. /* Private variables ---------------------------------------------------------*/
    37. /* USER CODE BEGIN PV */
    38. uint8_t sz[]="please input aa1 or bb2\n\r";
    39. uint8_t sz2[]="congradulations you are right\n\r";
    40. uint8_t sz3[]="iam sorry you have make a mistake\n\r";
    41. /* USER CODE END PV */
    42. /* Private function prototypes -----------------------------------------------*/
    43. void SystemClock_Config(void);
    44. /* USER CODE BEGIN PFP */
    45. /* USER CODE END PFP */
    46. /* Private user code ---------------------------------------------------------*/
    47. /* USER CODE BEGIN 0 */
    48. /* USER CODE END 0 */
    49. /**
    50. * @brief The application entry point.
    51. * @retval int
    52. */
    53. int main(void)
    54. {
    55. /* USER CODE BEGIN 1 */
    56. /* USER CODE END 1 */
    57. /* MCU Configuration--------------------------------------------------------*/
    58. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    59. HAL_Init();
    60. /* USER CODE BEGIN Init */
    61. /* USER CODE END Init */
    62. /* Configure the system clock */
    63. SystemClock_Config();
    64. /* USER CODE BEGIN SysInit */
    65. /* USER CODE END SysInit */
    66. /* Initialize all configured peripherals */
    67. MX_GPIO_Init();
    68. MX_TIM2_Init();
    69. /* USER CODE BEGIN 2 */
    70. ///private code///
    71. HAL_TIM_Base_Start_IT(&htim2);
    72. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
    73. /* USER CODE END 2 */
    74. /* Infinite loop */
    75. /* USER CODE BEGIN WHILE */
    76. while (1)
    77. {
    78. /* USER CODE END WHILE */
    79. /* USER CODE BEGIN 3 */
    80. }
    81. /* USER CODE END 3 */
    82. }
    83. /**
    84. * @brief System Clock Configuration
    85. * @retval None
    86. */
    87. void SystemClock_Config(void)
    88. {
    89. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    90. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    91. /** Initializes the RCC Oscillators according to the specified parameters
    92. * in the RCC_OscInitTypeDef structure.
    93. */
    94. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    95. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    96. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    97. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    98. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    99. {
    100. Error_Handler();
    101. }
    102. /** Initializes the CPU, AHB and APB buses clocks
    103. */
    104. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    105. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    106. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    107. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    108. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    109. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    110. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    111. {
    112. Error_Handler();
    113. }
    114. }
    115. /* USER CODE BEGIN 4 */
    116. /* USER CODE END 4 */
    117. /**
    118. * @brief This function is executed in case of error occurrence.
    119. * @retval None
    120. */
    121. void Error_Handler(void)
    122. {
    123. /* USER CODE BEGIN Error_Handler_Debug */
    124. /* User can add his own implementation to report the HAL error return state */
    125. __disable_irq();
    126. while (1)
    127. {
    128. }
    129. /* USER CODE END Error_Handler_Debug */
    130. }
    131. #ifdef USE_FULL_ASSERT
    132. /**
    133. * @brief Reports the name of the source file and the source line number
    134. * where the assert_param error has occurred.
    135. * @param file: pointer to the source file name
    136. * @param line: assert_param error line source number
    137. * @retval None
    138. */
    139. void assert_failed(uint8_t *file, uint32_t line)
    140. {
    141. /* USER CODE BEGIN 6 */
    142. /* User can add his own implementation to report the file name and line number,
    143. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    144. /* USER CODE END 6 */
    145. }
    146. #endif /* USE_FULL_ASSERT */

    本实验采用的系统频率SYSTIM为8MHZ,如果频率改变需要修改一个数值

  • 相关阅读:
    有效保护敏感数据的最佳实践
    【Linux操作系统】-- 多线程(三)-- 线程池+单例模式
    大模型prompt-文章生成
    文生视频Sora模型发布,是否引爆AI芯片热潮
    锐捷MPLS跨域方案C2实验配置
    华为机试 - 数字字符串组合倒序
    mybatisPlus实现分页
    windows下安装rabbitmq
    【Spring Boot】自定义MessageConverter
    Hive用户中文使用手册系列(一)
  • 原文地址:https://blog.csdn.net/geniusChinaHN/article/details/132744552