1.初始化LED灯(根据原理图修改pin和gpio):
//初始化led
void Led_Init(void)
{
GPIO_InitTypeDef GPIOB_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIOB_InitStructure);
GPIO_SetBits(GPIOB, 0xFF);
}
2.初始化串口
void Uart_Init(void)
{
//第一结构体变量
GPIO_InitTypeDef GPIOA_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//配置引脚
GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIOA_InitStructure);
GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIOA_InitStructure);
//初始化串口
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_P

该代码示例展示了如何在STM32微控制器上初始化LED灯和串口通信。通过配置GPIO和NVIC中断,实现了对多个LED的状态控制,并通过串口接收数据来切换LED的开/关状态。当收到特定字符时,对应的LED会进行翻转操作,并通过串口发送确认信息。
3295

被折叠的 条评论
为什么被折叠?



