各位同志好,看过了第一篇的点灯任务,这一篇我们来点亮板用LED灯,并且采用外部中断来调整呼吸灯呼吸的频率。

我们采用第一篇的程序模板,查看PC9对应了TIM3定时器的通道4,所以我们配置TIM3的CH4通道为PWM模式,在底部配置TIM3进行48分频(48÷48=1Mhz);重装载值为100,我们就得到了10kHz的频率从PC9中输出。这样基本配置就结束了,鉴于大部分配置可以参考上一篇,所以这里仅仅展示TIM3的配置,如下图所示:


程序基本原理是:MCU上电后先初始化一堆东西(将PC13配置成外部中断模式以及开启定时器)并使用串口打印相关作者信息,接着进入主函数中,分别在按下板用按键后会出现LED2熄灭、LED2亮起、LED2快速呼吸、LED2正常呼吸、LED2慢速呼吸,同时串口打印当前按键按下了几次,如下图:

呼吸灯(快速)

呼吸灯(正常)

呼吸灯(慢速)

呼吸灯的本质就是在频率一定时,调整PC9低电平的时间,当PC9低电平时间越长,LED2灯就越亮;反之灯就越暗,我们用一个for循环渐渐减少占空比的时间灯就越来越暗,反之相同,会越来越亮。
程序实现如下:
/*
*作者:下行路轨上的C70E 论坛ID钟详
*Nucleo-C092 demo程序任务2:使用定时器产生PWM控制LED灯常亮、不同频次呼吸,并由串口打印当前按下次数
*使用意法半导体Nucleo板卡,配置外置晶体48M,配置定时器TIM3,使用通道4
*本站可换乘地铁3号线,持有交通卡的乘客可在出站后30分钟内换乘1号线
*/
/*包含所需头文件*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "stm32c0xx_it.h"
/*定义切换模式用变量*/
int i;
/*函数声明*/
void SystemClock_Config(void);
void LED_Breathe(int Num);
/*主函数*/
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TIM3_Init();
/*启动定时器HTIM3的4通道*/
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo2:PWM control The breathe LED\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
/*进入循环体*/
while (1){
switch(i){
case 0:
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,100);
break;
case 1:
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,0);
break;
case 2:
LED_Breathe(5);
break;
case 3:
LED_Breathe(10);
break;
case 4:
LED_Breathe(20);
break;
}
}
}
/*控制LED呼吸*/
void LED_Breathe(int Num){
for(uint8_t i=99;i>0;i--){
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,i);
HAL_Delay(Num);
}
for(uint8_t i=0;i<100;i++){
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,i);
HAL_Delay(Num);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/*进入外部中断时处理*/
void EXTI4_15_IRQHandler(void){
HAL_GPIO_EXTI_IRQHandler(User_button_Pin);//清除用户按键产生的中断
i=i+1;//模式值加一
if(i>4){//模式值限位
i=0;
}
/*串口打印按键被触发了几次*/
printf("press down:");
printf("%d\n",i);
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
好的,感谢各位的观看,这样我们就实现了基本的呼吸灯效果。 |