原理图显示使用usart3串口使用的是PB10和PB11引脚
直接配置usart3串口协议
void usart3_init(uint32_t baud)
{GPIO_InitTypeDef GPIO_InitStructureure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;//端口B硬件时钟打开RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);//串口3硬件时钟打开RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//配置PA9 PA10为AF模式(复用功能)GPIO_InitStructureure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10 ; //9 10号引脚GPIO_InitStructureure.GPIO_Mode = GPIO_Mode_AF;//输出模式第三种模式AF模式GPIO_InitStructureure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructureure.GPIO_Speed = GPIO_High_Speed;//高速,速度越高,响应越快,但是功耗会更高GPIO_InitStructureure.GPIO_PuPd = GPIO_PuPd_NOPULL;//不使能上下拉电阻GPIO_Init(GPIOB,&GPIO_InitStructureure);//由于引脚支持很多功能,需要指定该引脚的功能,当前要制定支持USART1GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);//配置USART1相关参数:波特率,数据位,停止位,校验位USART_InitStructure.USART_BaudRate = baud; //波特率,就是通信的速度USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位USART_InitStructure.USART_StopBits = USART_StopBits_1; //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(USART3, &USART_InitStructure);//配置中断触发方式,接收到一个字节,就通知CPU处理USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//NVIC配置其优先级NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //中断号NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开通道给NVIC管理NVIC_Init(&NVIC_InitStructure);//使能USART1工作USART_Cmd(USART3, ENABLE);}
主函数代码
//通过usart3串口发送数据给pc端
void usart3_send_str(char *str)
{char *p = str;while(*p!='\0'){/* 发送一个字节 */USART_SendData(USART3,*p);/* 等待发送完毕 */while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET);p++;}}int main(void)
{NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//抢占优先级0~3,支持4级!//响应优先级0~3,支持4级!key_init();Led_init();usart1_init(115200);usart3_init(9600);usart3_send_str("AT+NAMETWen\r\n");while(1){}
}void USART3_IRQHandler(void)
{uint8_t d;if(USART_GetITStatus(USART3,USART_IT_RXNE) == SET){d = USART_ReceiveData(USART3);if(d == '1')PFout(10)=0;if(d == 'a')PFout(10)=1;/* 发送一个字节 */USART_SendData(USART1,d);/* 等待发送完毕 */while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);/* 告诉CPU,已经完成接收中断请求,可以响应新的接收中断请求 */USART_ClearITPendingBit(USART3,USART_IT_RXNE);}}