keil5接收数据代码
时间: 2023-07-02 18:22:05 浏览: 91
stm32f103XXX 串口接收不定长数据 KEIL5 程序
3星 · 编辑精心推荐
以下是使用Keil5接收数据的代码示例:
```
#include <stdio.h>
#include <stm32f4xx.h>
#include <stm32f4xx_usart.h>
#define USARTx USART2
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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(USARTx, &USART_InitStructure);
USART_Cmd(USARTx, ENABLE);
}
int main()
{
USART_Configuration();
while (1)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET);
uint8_t data = USART_ReceiveData(USARTx);
printf("Received data: %c\n", data);
}
}
```
这个示例代码配置了USART2作为串口,使用GPIOA的引脚2和3作为TX和RX线,波特率为115200。在主函数中,使用while循环不断接收USART接收缓冲区中的数据,并打印出来。
阅读全文