STM32串口通信:串口通信庫(kù)
從箱底撈出來(lái)的f103,支持的c語(yǔ)言太基礎(chǔ),雖然性能很高,然而開(kāi)發(fā)時(shí)間長(zhǎng)難以快速的使用,一開(kāi)始把 c++那些該有的都搬進(jìn)來(lái),結(jié)果當(dāng)然是覺(jué)得有的函數(shù)都沒(méi)有了。小小的寫(xiě)了一個(gè)庫(kù)用來(lái)通過(guò)串口來(lái)DEBUG
一共有這幾個(gè)函數(shù),串口為PA9和PA10,在C8T6最小系統(tǒng)上面通過(guò)測(cè)試
Serial_Begin()
初始化USART,設(shè)定波特率
Serial_WriteLine()
傳輸數(shù)據(jù)并換行
Serial_Write()
傳輸數(shù)據(jù),沒(méi)有換行符
例:
char str[] = {"THIS IS STM32C8T6"};
void main()
{
Serial_Begin(115200);
while(1)
{
Serial_WriteLine(str);
}
}
頭文件:
#define ARRAY_LEN(array,len) {len = (sizeof(array) / sizeof(array[0]));}
void Serial_WriteLine(char *data);
void Serial_Write(char *data);
void Serial_Begin(int baud);
int Strsize(char *data);
void Error_Process();
void Serial_Printf(char *data);
c文件:
#include "stm32f1xx_hal.h"
#include "Sp_Command.h"
UART_HandleTypeDef MyUart;
char str1[14]= {"SIZE OF STR:"};
char newline[] = {"rn"};
void Serial_Begin(int baud)//初始化UART
{
MyUart.Instance = USART1;
MyUart.Init.BaudRate = baud;
MyUart.Init.WordLength = UART_WORDLENGTH_8B;
MyUart.Init.StopBits = UART_STOPBITS_1;
MyUart.Init.Parity = UART_PARITY_NONE;
MyUart.Init.Mode = UART_MODE_TX_RX;
MyUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
MyUart.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&MyUart) != HAL_OK)
{
Error_Process();
}
}
void Serial_Write(char *data)//寫(xiě)字符串?dāng)?shù)組
{
while(*data!='