使用串口2的中断方式。
初始化函数- void Fuc_RS232_USART_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; // 定义一个结构体
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- USART_DeInit(USART2);
- // 定义PA2 PA3 为串口1所用
- // 定义UART2 TX(PA.2)引脚为复用推挽输出
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // IO口的第10引脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // IO口速度为50MHz
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // IO口复用为推挽输出
- GPIO_Init(GPIOA,&GPIO_InitStructure); // 初始化串口1 输出IO口
- // 定义USART2 Rx(PA.3) 为悬空输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // IO口的第11引脚
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // IO口速度为50MHz
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // IO口悬空输入
- GPIO_Init(GPIOA,&GPIO_InitStructure); // 初始化串口1 输入IO口
-
-
-
-
- USART_ClockInitTypeDef USART_ClockInitStructure;
- USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- /* Configure the USART1 synchronous paramters */
- USART_ClockInit(USART2, &USART_ClockInitStructure);
- // 串口2参数初始化定义部分,串口1参数为19200,8,1,N 使能接收和发送
- USART_InitStructure.USART_BaudRate = 19200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置数据传输速率
- 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(USART2,&USART_InitStructure); // 初始化串口1
-
-
- // 使能串口2中断
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // 3.0库以上 USART2_IRQChannel更改为USART2_IRQn
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
- USART_Cmd(USART2,ENABLE); // 使能串口
- }
是什么情况啊?有谁遇到过?
|