找回密码
 注册

QQ登录

只需一步,快速开始

搜索

基于STM32F103VE实现呼吸灯源程序

[复制链接]
路漫漫 发表于 2020-6-10 17:14:36 | 显示全部楼层 |阅读模式
完整源码: stm32_test_20200417_呼吸灯.7z (176.35 KB, 售价: 1 E币)
部分源码:
  1. int main(void)
  2. {
  3.         led_init();         // LED初始化函数
  4.         nvic_init();        // NVIC配置函数
  5.         exti_init();        // EXTI配置函数
  6.         usart_init();        // USART配置函数
  7.         tim_init();                // TIM配置函数
  8.         
  9.         usart1_sendstring("This is a testing\n");
  10.         usart1_sendstring("Hello STM32\n");

  11.   /* Infinite loop */
  12.   while (1)
  13.   {
  14.   }
  15. }
  16. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

  17. it.c:
  18. // EXTI0中断服务函数
  19. void EXTI0_IRQHandler(void)
  20. {
  21.                 if(EXTI_GetITStatus(EXTI_Line0) != RESET) // 确保相应中断发生了
  22.                 {
  23.                                 EXTI_ClearITPendingBit(EXTI_Line0);                // 清除EXTI0中断标志位
  24.                                 
  25.                                 if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5) == 0) // 判断LED引脚电平是否为0
  26.                                 {
  27.                                                 GPIO_SetBits(GPIOB, GPIO_Pin_5);                // LED OFF
  28.                                 }
  29.                                 else // GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5) == 1
  30.                                 {
  31.                                                 GPIO_ResetBits(GPIOB, GPIO_Pin_5);        // LED ON
  32.                                 }
  33.                 }
  34. }

  35. uint8_t rx_buffer_flag = 0;
  36. uint8_t rx_buffer[5] = {0};
  37. uint8_t rx_buffer_cnt = 0;
  38. void USART1_IRQHandler(void)
  39. {
  40.                 uint8_t rcv_data = 0;
  41.                 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 确保相应的中断发生
  42.                 {
  43.                                 USART_ClearITPendingBit(USART1, USART_IT_RXNE);                // 清除USART1接收中断位
  44.                         
  45.                                 rcv_data = USART_ReceiveData(USART1);                                                        // 接收数据
  46. //                                usart1_sendbyte(rcv_data);                                                                                                // 发送数据
  47.                                 if(rcv_data == 0xAA)
  48.                                 {
  49.                                                 rx_buffer_flag = 1;
  50.                                 }
  51.                                 if(rx_buffer_flag == 1)
  52.                                 {
  53.                                                 rx_buffer[rx_buffer_cnt] = rcv_data;
  54.                                                 rx_buffer_cnt++;
  55.                                 }
  56.                                 if(rx_buffer_cnt >= 4)
  57.                                 {
  58.                                                 rx_buffer_flag = 0;
  59.                                                 rx_buffer[4] = '\0';
  60.                                                 usart1_sendstring((char *)(rx_buffer + 1));
  61.                                                 rx_buffer_cnt = 0;
  62.                                 }
  63.                                 
  64.                 }
  65. }

  66. // TIM6中断服务函数
  67. void TIM6_IRQHandler(void)
  68. {
  69.                 if(TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)
  70.                 {
  71.                                 TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
  72.                         
  73. //                                if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5) == 0) // 判断LED引脚电平是否为0
  74. //                                {
  75. //                                                GPIO_SetBits(GPIOB, GPIO_Pin_5);                // LED OFF
  76. //                                }
  77. //                                else // GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5) == 1
  78. //                                {
  79. //                                                GPIO_ResetBits(GPIOB, GPIO_Pin_5);        // LED ON
  80. //                                }
  81.                 }
  82. }

  83. // TIM3中断服务函数
  84. uint8_t cnt1 = 0;
  85. uint8_t cnt2 = 0;
  86. uint8_t cnt_flag = 0;
  87. void TIM3_IRQHandler(void)
  88. {
  89.                 if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  90.                 {
  91.                                 TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  92.                         
  93.                                 cnt1++;
  94.                                 if(cnt1 >= 100)
  95.                                 {
  96.                                                 cnt1 = 0;
  97.                                                 TIM3->CCR3 = cnt2;
  98.                                                 if(cnt_flag == 0)
  99.                                                 {
  100.                                                                 cnt2++;
  101.                                                 }
  102.                                                 else
  103.                                                 {
  104.                                                                 cnt2--;
  105.                                                 }
  106.                                                 if(cnt2 >= 100)
  107.                                                 {
  108.                                                                 cnt_flag = 1;
  109.                                                 }
  110.                                                 else if(cnt2 == 0)
  111.                                                 {
  112.                                                                 cnt_flag = 0;
  113.                                                 }
  114.                                 }
  115.                
  116.                 }

  117. }
复制代码


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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-20 12:37

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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