通过串口接收到的数据改变PA8输出的占空比来控制LED小灯的亮度
主函数
char *str_light = "LIGHT:";
unsigned char PWM_BUF = 0;
int main(void)
{
PWM_Init();
USART3_TIM2_Init(115200);
delay_init();
while(1)
{
if(RX_SATA == 1)
{
if(strstr(RX_BUF,str_light) != 0)
{
delay_ms(1);
PWM_BUF =(RX_BUF[6]-0x30)*10+(RX_BUF[7]-0x30);
TIM_SetCompare1(TIM1,PWM_BUF);
}
RX_SATA = 0;
count = 0;
memset(RX_BUF,0,strlen(RX_BUF));
}
}
}
配置USART3和TIM2
char RX_BUF[20] = {0};
u8 count = 0;
u8 RX_SATA = 0;// 0表示接受未完成 ;1表示接收完成
void USART3_TIM2_Init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//开启GPIOB 和 复用功能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
//开启USART3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
//PB10 ->Tx 配置为复用推挽式输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//PB11 ->Rx 配置为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//配置串口
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//配置串口接收为中断模式
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART3,ENABLE);
USART_GetFlagStatus(USART3,USART_FLAG_TC);//防止第一个字符被吞
//使能定时器2的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseStructure.TIM_Period = (100-1);//ARR
TIM_TimeBaseStructure.TIM_Prescaler = (7200-1);//PSC
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
//使能定时器2的中断
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
//配置定时器2的中断优先等级
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//使能定时器2
TIM_Cmd(TIM2,DISABLE);
}
void USART3_IRQHandler(void)
{
USART_ClearFlag(USART3,USART_FLAG_RXNE);
RX_BUF[count] = USART_ReceiveData(USART3);
count++;
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2,ENABLE);
}
void TIM2_IRQHandler(void)
{
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_Cmd(TIM2,DISABLE);
RX_SATA = 1;
}
配置PWM
void PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//初始化PA8管脚为复用推挽式输出
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽式输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化定时器1的时基单元
//使能定时器1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_TimeBaseStructure.TIM_Period = (100-1);//ARR
TIM_TimeBaseStructure.TIM_Prescaler = (72-1);//PSC
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, & TIM_TimeBaseStructure);
//初始化PWM输出配置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
//使能PWM外设
TIM_CtrlPWMOutputs(TIM1,ENABLE);
//激活CCR寄存器
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
//激活ARR寄存器
TIM_ARRPreloadConfig(TIM1,ENABLE);
//使能定时器1
TIM_Cmd(TIM1,ENABLE);
}
上一篇:stm32 通过串口控制led灯亮灭 并通过lcd显示led灯的亮灭状态
下一篇:STM32总结一 STM32三种点亮LED灯方式的不同之处
推荐阅读最新更新时间:2024-03-16 16:27
设计资源 培训 开发板 精华推荐
- 柔灵科技陈涵:将小型、柔性的脑机接口睡眠设备,做到千家万户
- 微灵医疗李骁健:脑机接口技术正在开启意识与AI融合的新纪元
- USB Type-C® 和 USB Power Delivery:专为扩展功率范围和电池供电型系统而设计
- 景昱医疗耿东:脑机接口DBS治疗技术已实现国产替代
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度
- 英飞凌推出简化电机控制开发的ModusToolbox™电机套件
- 意法半导体IO-Link执行器电路板为工业监控和设备厂商带来一站式参考设计
- 有奖活动|NI 助您设计更靠谱的数据采集系统
- 【已结束】 Qorvo、村田、NI直播【UWB最新技术、方案、市场、应用解析】(13:30开始入场)
- 有奖直播|TI 工业多协议通信应用中的优化解决方案
- 有奖国产芯直播:先楫800MHz RISC-V MCU高能秀,岂止控4只伺服电机,干货多多
- Mouser 福利活动:邀请新用户下单,各得百元奖励!
- 点评Deyisupport精品博文 抢楼赢好礼!
- 跟上未来,走进恩智浦最新技术大讲堂
- 【EEWORLD第二十三届】2011年02月社区明星人物揭晓!
- PI HiperPFS-4系列功率因数校正IC,了解产品特色,答题享好礼!
- 有奖活动“庖丁”解智能睡眠监测仪,一波“水军”来围观