前言
本篇文章属于stm32单片机(以下简称单片机)的学习笔记,来源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记,只能做参考,细节方面建议观看视频,肯定受益匪浅。
STM32入门教程-2023版 细致讲解 中文字幕_哔哩哔哩_bilibili
一、通信接口
二、串口通信
三、电平标准
四、串口参数及时序
五、USART简介
六、实例一(串口发送)
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>void Serial_Init(void)
{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);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)
{uint8_t i = 0;while(String[i] != '\0'){Serial_SendByte(String[i]);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');}
}
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("HelloWorld!\r\n");Serial_SendNumber(12345, 5);while (1){}
}
移植printf函数的方法,有三种
第一种:在Serial.c中加入,注意Serial.c和Serial.h中都要#include <stdio.h>
int fputc(int ch, FILE *f) {Serial_SendByte(ch);return ch; }
然后在main函数中使用即可,这个方法适用于仅一个USART需要使用
第二种:在main函数中使用sprintf
char String[100];sprintf(String,"Num=%d\r\n",777);Serial_SendString(String);
第三种:封装sprintf,在Serial.c中加入封装代码
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中调用
Serial_Printf("Num=%d\r\n",888);
七、实例二(串口接收)
Serial.c
#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);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;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)
{uint8_t i = 0;while(String[i] != '\0'){Serial_SendByte(String[i]);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)
{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()
{if (Serial_RxFlag == 1){Serial_RxFlag = 0;return 1;}else {return 0;}
}uint8_t Serial_GetRxData()
{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);}
}
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();OLED_ShowString(1,1,"RxData:");while (1){if (Serial_GetRxFlag() == 1){RxData = Serial_GetRxData(USART1);Serial_SendByte(RxData);OLED_ShowHexNum(1,8,RxData,2);}}
}