STM32L151 usart2 串口2 程序

void USART2_config(unsigned int BaudRate)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	USART_InitTypeDef USART_InitStructure;
    
	/* Enable the USART2 Pins Software Remapping */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    
	/* Configure USART2 Rx (PA.03) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; 	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
    
	/* Configure USART2 Tx (PA.02) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
    
	/* Enable the USART2 Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);    
    
	USART_InitStructure.USART_BaudRate = BaudRate; 
	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_Rx | USART_Mode_Tx;	  
    
	USART_Init(USART2, &USART_InitStructure);
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	//USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
	/* Enable USART2 */
	USART_Cmd(USART2, ENABLE);
		
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
		
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
}
void USART2_IRQHandler(void)
{
	unsigned char temp=0;
	
	if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
	{  
		temp=USART_ReceiveData(USART2);
		USART_ClearFlag(USART2,USART_FLAG_RXNE);
		
		usart2_race_buf[usart2_race_count] = temp;
		usart2_race_count++;
		if(usart2_race_count>=2)
		{
			usart2_race_count = 0;
			usart2_race_over = 1;
		}
	}  
}


void USART2_send_all(unsigned char *byte,unsigned char send_mun)
{
	unsigned char i;

	for(i=0;i<send_mun;i++)
	{
		USART_SendData(USART2,*byte);
		byte++;
		
		while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){}
	}
}



接项目  代编程  13810175694    QQ727341554

STM32L151的USART1是一个全速UART通信接口,通常用于串行数据传输。为了配置它并连接到特定的GPIO引脚,你需要完成以下几个步骤: 1. **启用电源**:首先,确保USART1所需的供电已通过系统管理部分正确配置,如VDD串行接口。 2. **GPIO配置**: - 确定要用作RX(接收)和TX(发送)的IO口。例如,如果你选择PA9作为TX和PA10作为RX,需要配置它们为推挽输出模式(GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP)和推挽输入模式(GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING),然后设置相应的 Alternate Function (AF) 。 - 对于STM32L151,USART1的AF通常对应EXTI线,所以要将PA9和PA10配置为EXTI线的AF7和AF8。 3. **USART初始化**: - 使用`HAL_UART_Init()`函数初始化USART1,传入具体的结构体配置,比如波特率、数据位数、停止位和校验等。例如: ```c UART_HandleTypeDef huart1; huart1.Instance = USART1; huart1.Init.BaudRate = YOUR_BAUD_RATE; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; ``` 4. **打开中断**:如果需要,配置USART的中断处理程序,并开启中断。 5. **开启USART**: ```c HAL_UART_MspInit(&huart1); // 进行硬件资源初始化 HAL_UART_Start(&huart1); // 启动串口 ``` 6. **数据发送和接收**: 使用`HAL_UART_Transmit()`和`HAL_UART_Receive()`函数发送和接收数据。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值