0%

9-3串口发送&串口发送+接收

常用函数

1
void USART_DeInit(USART_TypeDef* USARTx);

USART恢复缺省配置

1
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);

USART初始化结构体

1
void USART_StructInit(USART_InitTypeDef* USART_InitStruct);

USART结构体初始化

1
2
void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);

配置同步时钟输出,时钟是否输出、时钟的极性、相位等参数,使用结构体配置。

1
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);

USART开启指令

1
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);

USART中断配置

1
void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);

开启USART到DMA的触发通道

1
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);

发送数据,写DR寄存器

1
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);

接收数据,读DR寄存器

串口发送

Serial.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启USART1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA的时钟

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//引脚模式,TX是USART外设控制的输出脚,选择复用推挽输出;RX引脚是USART外设控制的输入脚,选择输入模式,一般RX配置为浮空输入或者上拉输入。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//PA9是USART1的TX复用端口
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;//字长,由于前面没有选择校验,这里选8位
USART_Init(USART1,&USART_InitStructure);

USART_Cmd(USART1,ENABLE);//USART使能
}

void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1, Byte);//USART发送数据
while( USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET );//获取标志位,发送寄存器空标志位,避免产生数据覆盖的问题
}

void Serial_SendArray(uint8_t *Array, uint16_t Length)//USART发送数组
{
uint16_t i;
for (i=0;i<Length;i++)
{
Serial_SendByte(Array[i]);
}
}

void Serial_SendString(char *String)//USART发送字符串
{
uint8_t i;
for ( i = 0; String[i] != '\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)//USART发送数字
{
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);
}

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"

int main(void)
{
OLED_Init();
Serial_Init();

// Serial_SendByte('A');

// uint8_t MyArray[] = {0x41,0x42,0x43,0x44};
// Serial_SendArray(MyArray, 4);

// Serial_SendString("Hello World!");
// Serial_SendNumber(12345, 5);
// printf("Num = %d\r\n",666);

// char String[100];
// sprintf(String,"Num = %d\r\n",666);
// Serial_SendString(String);

Serial_Printf("Num = %d\r\n",666);
}

串口发送+接收

Serial.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_RxData;
uint8_t Serial_RxFlag;



void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启USART1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA的时钟

GPIO_InitTypeDef GPIO_InitStructure;
//配置PA9为TX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//引脚模式,TX是USART外设控制的输出脚,选择复用推挽输出;RX引脚是USART外设控制的输入脚,选择输入模式,一般RX配置为浮空输入或者上拉输入。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置PA10为RX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//RX配置为浮空输入
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的RX和TX模式打开
USART_InitStructure.USART_Parity = USART_Parity_No;//校验位,选择不需要校验
USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长,由于前面没有选择校验,这里选8位
USART_Init(USART1,&USART_InitStructure);

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启RXNE标志位到NVIC的输出
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)
{
uint8_t i;
for ( i = 0; String[i] != '\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;
}
return 0;
}

uint8_t Serial_GetRxData(void)
{
return Serial_RxData;
}

void USART1_IRQHandler(void)
{
if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET)
{
Serial_RxData = USART_ReceiveData(USART1);
Serial_RxFlag = 1;
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
uint8_t RxData;
int main(void)
{
OLED_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);

}
}
}