| 函数代码: Serial.c
 #include "stm32f10x.h"                  // Device header
 #include <stdio.h>
 #include <stdarg.h>
 uint8_t Serial_RxData;
 uint8_t Serial_RxFlag;
 
 void Serial_Init() {
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,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;//硬件流控制(不使用,CTS,CTS&RTS)
 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//串口模式 可以使用(或)|符号实现Tx和Rx同时设置
 USART_InitStructure.USART_Parity = USART_Parity_No;//校验位,无需校验
 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位,选择1位
 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长
 USART_Init(USART1, &USART_InitStructure);
 //串口接收部分可以采用查询或者中断的方式,如果采用中断就需要在这里配置NVIC
 
 //开启中断
 
 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);//开启USART
 }
 void Serial_SendByte(uint8_t Byte) {
 USART_SendData(USART1, Byte);//发送数据
 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {//等待发送寄存器空,
 //TXE就是发送寄存器空的标志位,不需要手动清零,下一次发送数据时候会自动清零
 }
 }
 void Serial_SendArray(uint8_t *Array, uint16_t Length){
 uint16_t i;
 for(int i = 0; i < Length; i++) {
 Serial_SendByte(Array[i]);
 }
 
 }
 void Serial_SendString(char *Str) {//字符串自带结束标志位
 uint8_t i;
 for(int i = 0; Str[i] != '\0'; i++) {
 Serial_SendByte(Str[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(int i = 0; i < Length; i++){
 Serial_SendByte((Number / Serial_Pow(10, Length - i - 1)) % 10 + '0');
 }
 
 }
 int fputc(int ch, FILE* f){
 Serial_SendByte(ch);//重定向到串口,使得Printf打印到串口
 return ch;
 
 }
 //使用sprintf让其他的串口也能使用,sprintf可以把格式化字符输出到一个字符串里
 void Serial_Printf(char* format,...){//三个点用来接收后面可变参数列表
 char String[100];
 va_list arg;
 va_start(arg, format);//从format位置开始接收参数表,放在arg里面
 vsprintf(String, format, arg);
 va_end(arg);
 Serial_SendString(String);
 }
 uint8_t Serial_GetRxFlag() {
 if(Serial_RxFlag == 1){
 Serial_RxFlag = 0;
 return 1;
 }
 return 0;
 }
 uint8_t SerialGetRxData() {
 return Serial_RxData;
 }
 void USART1_IRQHandler() {
 if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) {
 //如果读取DR就自动清除标志位,如果没有就需要手动清除
 Serial_RxData = USART_ReceiveData(USART1);
 Serial_RxFlag = 1;
 USART_ClearITPendingBit(USART1, USART_IT_RXNE);
 }
 }
 |