实验原理:
  STM32的定时器有PWM功能,iCore4的蓝色LED连接在定时器的输出接口上,

可以通过定时器的PWM输出控制LED的亮度,从而实验呼吸灯的功能。

核心代码:

  1. int main(void)
  2. {
  3. /* USER CODE BEGIN 1 */
  4. int brightness = 0;
  5. float temp = 0.0;
  6. int data = 0;
  7. /* USER CODE END 1 */
  8.  
  9. /* MCU Configuration----------------------------------------------------------*/
  10.  
  11. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  12. HAL_Init();
  13. /* USER CODE BEGIN Init */
  14.  
  15. /* USER CODE END Init */
  16.  
  17. /* Configure the system clock */
  18. SystemClock_Config();
  19. /* USER CODE BEGIN SysInit */
  20.  
  21. /* USER CODE END SysInit */
  22.  
  23. /* Initialize all configured peripherals */
  24. MX_GPIO_Init();
  25. MX_TIM1_Init();
  26. /* USER CODE BEGIN 2 */
  27.  
  28. /* USER CODE END 2 */
  29.  
  30. /* Infinite loop */
  31. /* USER CODE BEGIN WHILE */
  32. while (1)
  33. {
  34. /* USER CODE END WHILE */
  35.  
  36. /* USER CODE BEGIN 3 */
  37. HAL_Delay(30);
  38. if(brightness == 100){
  39. brightness = 0;
  40. }
  41. temp = 199 * (sin(2 * PI / 99.0 * brightness) * 0.5 + 0.5);
  42. data = (int)temp;
  43. set_compare(data);
  44. brightness ++;
  45. }
  46. /* USER CODE END 3 */
  47. }

源代码下载链接:

链接:http://pan.baidu.com/s/1hsQnbEW 密码:z12v

iCore4链接:

版权声明:本文为xiaomagee原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaomagee/p/7373996.html