STM32开发板的TFTLCD显示屏显示串口发送过来的数据的标准库函数版本代码?
时间: 2024-05-16 07:16:08 浏览: 9
以下是使用STM32标准库函数驱动TFTLCD显示屏显示串口发送过来的数据的示例代码:
```
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_exti.h"
#include "ili9341.h"
#define USARTx USART1
#define USARTx_IRQn USART1_IRQn
#define USARTx_IRQHandler USART1_IRQHandler
/**
* @brief Configure the USART1 pins.
* @param None
* @retval None
*/
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable USART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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);
/* Enable USART1 */
USART_Cmd(USARTx, ENABLE);
}
/**
* @brief Send a string to the USART.
* @param s: pointer to string
* @retval None
*/
void USART_puts(USART_TypeDef* USARTx, volatile char *s)
{
while(*s)
{
// wait until data register is empty
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
// send data
USART_SendData(USARTx, *s);
// next byte
s++;
}
}
int main(void)
{
USART_Configuration();
ILI9341_Init();
ILI9341_setRotation(1);
ILI9341_fillScreen(ILI9341_BLACK);
char buffer[50];
while (1)
{
// wait until data is received
while (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET);
// read data
char data = USART_ReceiveData(USARTx);
// convert data to string
sprintf(buffer, "Received: %c", data);
// display string on LCD
ILI9341_setTextColor(ILI9341_WHITE);
ILI9341_setCursor(0, 0);
ILI9341_puts(buffer);
// send data back to serial port
USART_puts(USARTx, buffer);
}
}
```
这段代码使用USART1接收串口数据,并将接收到的数据转换为字符串,然后在TFTLCD显示屏上显示该字符串,并将该字符串发送回串口。