sTM32的USART串行通信实验全部代码
时间: 2023-10-30 19:07:23 浏览: 85
以下是STM32的USART串行通信实验全部代码,供参考:
```c
#include "stm32f10x.h"
#include "USART.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
while(1)
{
USART_SendData(USART1, 'A');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'B');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'C');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'D');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'E');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'F');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'G');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'H');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'I');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'J');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'K');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'L');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'M');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'N');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'O');
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_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_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
uint8_t data = USART_ReceiveData(USART1);
USART_SendData(USART1, data);
}
}
```
其中,USART.h头文件的内容为:
```c
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
void USART_SendData(USART_TypeDef* USARTx, char c);
void USART_SendString(USART_TypeDef* USARTx, char* str);
void USART_SendNumber(USART_TypeDef* USARTx, uint32_t num);
void USART_SendFloat(USART_TypeDef* USARTx, float num);
#endif
```
USART.c文件的内容为:
```c
#include "USART.h"
void USART_SendData(USART_TypeDef* USARTx, char c)
{
USART_SendData(USARTx, (uint8_t)c);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
void USART_SendString(USART_TypeDef* USARTx, char* str)
{
while(*str)
{
USART_SendData(USARTx, *str++);
}
}
void USART_SendNumber(USART_TypeDef* USARTx, uint32_t num)
{
char buffer[10];
int i = 0;
do
{
buffer[i++] = num % 10 + '0';
num /= 10;
} while(num);
while(i > 0)
{
USART_SendData(USARTx, buffer[--i]);
}
}
void USART_SendFloat(USART_TypeDef* USARTx, float num)
{
char buffer[10];
sprintf(buffer, "%.2f", num);
USART_SendString(USARTx, buffer);
}
```
需要注意的是,以上代码仅供参考,具体实现需要根据具体的硬件环境进行调整。
阅读全文