STM32:串口发送+串口接收(内含:查询法+中断法+思路图+代码部分)
有两种方法:
1.查询法(适合程序简单的串口发送+接收)
思路图如下:
代码部分:
主函数代码部分:
Serial.c代码部分:
2.中断法:
向NVIC申请中断,在中断函数接收数据。
思路图如下:
Serial.h代码部分:
主函数部分:
代码部分如下:
主函数部分:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
uint8_t RxData;
int main(void)
{
OLED_Init();
Serial_Init();
OLED_ShowString(1, 1, "RxData:");
Serial_Init();
while (1)
{
if (Serial_GetRxFlag() == 1)
{
RxData = Serial_GetRxData();
Serial_SendByte(RxData);
OLED_ShowHexNum(1, 8, RxData, 2);
}
}
}
Serial.c代码部分:
#include "stm32f10x.h" // Device header
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate=9600;
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStruct.USART_Parity=USART_Parity_No;
USART_InitStruct.USART_StopBits=1;
USART_InitStruct.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStruct);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART1,ENABLE);
}
uint8_t Serial_GetRxFlag(void)
{
if (Serial_RxFlag == 1)
{
Serial_RxFlag = 0;
return 1;
}
return 0;
}
uint8_t Serial_GetRxData(void)
{
return Serial_RxData;
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
Serial_RxData = USART_ReceiveData(USART1);
Serial_RxFlag = 1;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1,Byte);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
void Serial_SendArray(uint16_t *Array,uint8_t Lenth)
{
uint8_t i;
for(i=0;i<Lenth;i++)
{
USART_SendData(USART1,Array[i]);
}
}
void Serial_SendString(char*String)
{
uint8_t i;
for(i=0;String[i]!= '\0';i++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint16_t x,uint16_t y)
{
uint32_t Return;
while(y--)
{
Return*=x;
}
return Return;
}
void Serial_SendNumber(uint32_t Number,uint8_t Lenth)
{
uint8_t i;
for(i=0;i<Number;i++)
{
USART_SendData(USART1,Number/Serial_Pow(10,Lenth-1-i)%10+0x40);
}
}
Serial.h代码部分:
#ifndef __SERIAL_H
#define __SERIAL_H
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint16_t *Array,uint8_t Lenth);
void Serial_SendString(uint16_t String);
uint32_t Serial_Pow(uint16_t x,uint16_t y);
void Serial_SendNumber(uint32_t Number,uint8_t Lenth);
uint8_t Serial_GetRxFlag(void);
uint8_t Serial_GetRxData(void);
#endif