stm32f103c8t6串口通信的测试程序 要标准库的 c语言的
时间: 2023-09-08 15:08:31 浏览: 40
以下是一个使用标准库的 C 语言的 STM32F103C8T6 串口通信测试程序:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define USARTx USART1
void USART_Config(void);
void GPIO_Config(void);
void USART_SendString(char* string);
int main(void) {
GPIO_Config();
USART_Config();
while (1) {
USART_SendString("Hello, World!\n");
delay();
}
}
void GPIO_Config(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void USART_Config(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStruct);
USART_Cmd(USARTx, ENABLE);
}
void USART_SendString(char* string) {
while (*string) {
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *string++);
}
}
void delay(void) {
volatile uint32_t i;
for (i = 0; i < 1000000; i++);
}
```
该程序使用 USART1 串口,将 "Hello, World!" 发送到终端。使用 GPIOA9 作为 USART1 的 TX 引脚,使用 GPIOA10 作为 USART1 的 RX 引脚。在 `USART_SendString` 函数中,使用 `USART_GetFlagStatus` 函数检查 USART 发送缓冲区是否为空,如果为空则等待直到缓冲区非空,然后使用 `USART_SendData` 函数发送数据。在 `delay` 函数中,使用一个空循环实现简单的延迟。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)