1.配置流程
①开启时钟,把需要用到的USART和GPIO的时钟打开
②GPIO初始化,把TX配置成复用输出,RX配置成输入模式
③配置USART,直接使用一个结构体来配置
④如果只需要发送功能,就直接开启USART,初始化结束了,如果需要接受的功能,那就还需要配置中断:在开启USART之前,加上ITConfig和NVIC的代码
2.库函数
配置同步时钟输出(时钟是否要输出、时钟极性、相位等参数)
`void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);` `void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);`
中断输出控制
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
开启USART到DMA的触发通道
void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
发送数据(写DR寄存器)
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
接收数据(读DR寄存器)
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
3.实验一——串口发送~代码
(1)main.c
#include "stm32f10x.h" // Device header
#include "delay.h"
#include "OLED.h"
#include "Serial.h"
int main( void)
{
OLED_Init();
Serial_Init();
//Serial_SendByte(0x41);
uint8_t MyArray[] = {0x42,0x43,0x44,0x45};
Serial_SendArray(MyArray,4);
Serial_SendString("\r\nNum1 = ");
Serial_SendNumber(123,3);
printf("\r\nNum2 = %d",666);
char string[100];
sprintf(string,"\r\nNum3 = %d",789);
Serial_SendString(string);
Serial_Printf("\r\nNum4 = %d",456);
Serial_Printf("\r\n");
while(1)
{
}
}
(2)Serial.h
#ifndef __SERIAL_H
#define __SERIAL_H
#include <stdio.h>
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);
#endif
(3)Serial.c
#include "stm32f10x.h"
#include <stdio.h>
#include <stdarg.h>
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //波特率
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Tx; //发送模式
USART_InitStructure.USART_Parity = USART_Parity_No; //校验位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1,Byte);
while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
uint16_t i;
for (i = 0;i < Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)
{
uint16_t i;
for (i = 0;String[1] != 0;i++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint32_t x,uint32_t y)
{
uint32_t result = 1;
while(y--)
{
result *= x;
}
return result;
}
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
uint8_t i;
for(i = 0;i < Length;i++)
{
Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');
}
}
int fputc(int ch,FILE *f) //重定向printf输出到串口
{
Serial_SendByte(ch);
return ch;
}
void Serial_Printf(char *format,...)
{
char string[100];
va_list arg;
va_start(arg,format);
vsprintf(string,format,arg);
va_end(arg); //释放参数表
Serial_SendString(string);
}
4.实验二——串口发送+接收~代码
(1)main.c
#include "stm32f10x.h" // Device header
#include "delay.h"
#include "OLED.h"
#include "Serial.h"
uint8_t RxData;
int main( void)
{
OLED_Init();
Serial_Init();
while(1)
{
if(Serial_GetRxFlag()==1)
{
RxData = Serial_GetRxData();
OLED_ShowHexNum(1,1,RxData,2);
}
}
}
(2)Serial.h
#ifndef __SERIAL_H
#define __SERIAL_H
#include <stdio.h>
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);
uint8_t Serial_GetRxData(void);
uint8_t Serial_GetRxFlag(void);
void USART1_IRQHandler(void);
#endif
(3)Serial.c
#include "stm32f10x.h"
#include <stdio.h>
#include <stdarg.h>
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //波特率
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送模式
USART_InitStructure.USART_Parity = USART_Parity_No; //校验位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1,Byte);
while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
void Serial_SendArray(uint8_t *Array,uint16_t Length)
{
uint16_t i;
for (i = 0;i < Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)
{
uint16_t i;
for (i = 0;String[1] != 0;i++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint32_t x,uint32_t y)
{
uint32_t result = 1;
while(y--)
{
result *= x;
}
return result;
}
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
uint8_t i;
for(i = 0;i < Length;i++)
{
Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');
}
}
int fputc(int ch,FILE *f) //重定向printf输出到串口
{
Serial_SendByte(ch);
return ch;
}
void Serial_Printf(char *format,...)
{
char string[100];
va_list arg;
va_start(arg,format);
vsprintf(string,format,arg);
va_end(arg); //释放参数表
Serial_SendString(string);
}
uint8_t Serial_GetRxFlag(void)
{
if(Serial_RxFlag==1)
{
Serial_RxFlag = 0;
return 1;
}
else
{
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);
}
}
距离返校还有6天!!!加油~