STC32G 系列单片机有4个全双工通用串行通信接口,串口1与串口2既有异步通信功能、又有同步通信功能,串口1与串口2都可进行SPI通信,SPI是一个全双工高速同步串行;通信总线串口3、串口4只有异步通信功能。本文将重点讨论其异步通信(UART)功能。
STC32G 单片机通用串行通信接口可以进行管脚切换。串口编程涉及接口管脚选择设置,通信模式设置,波特率发生器定时器(需要定时器做波特率发生器,单片机串口通信接口内部没有波特率发生器)选择设置,波特率设置、数据发生与数据接收等。
串口1的管脚可在4组管脚间切换,通过寄存器P_SW1的B6~B7位设置,如下:
串口2的管脚可在2组管脚间切换,通过寄存器P_SW2的B0位设置,如下:
串口3及串口4的管脚可分别在2组管脚间切换,串口3通过寄存器P_SW2的B1位设置,串口4通过寄存器P_SW2的B2位设置,如下:
STC32G 系列单片机串口通信(UART)模式有4种,如下:
串口1与串口2可工作在以上4种的任一模式下,串口3与串口4只能工作在可变波特率8位数据方式或9位可变数据方式。串口1的工作模式通过串口1的控制寄存器SCON的B6~B7位来设置,如下:
串口2的工作模式通过串口2的控制寄存器S2CON的B6~B7位来设置,如下:
串口3的工作模式通过串口1的控制寄存器S3CON的B7位来设置,如下:
串口4的工作模式通过串口4的控制寄存器S4CON的B7位来设置,如下:
串口1可选择(通用)定时器1或2做波特率发生器,通用定时器0不可做波特率发生器。串口1的波特率发生定时器通过辅助寄存器AUXR的B0位(S1BRT)来设置:0-选择T2做波特率发生器,1-选择T1做波特率发生器。串口2固定用(通用)定时器2做波特率发生器。串口3可选择(通用)定时器T2或T3做波特率发生器,串口4可选择(通用)定时器T2或T4做波特率发生器。串口3的波特率发生定时器通过寄存器S3CON的B6位(S3ST3)来设置:0-选择T2做波特率发生器,1-选择T3做波特率发生器。串口4的波特率发生定时器通过寄存器S4CON的B6位(S3ST3)来设置:0-选择T2做波特率发生器,1-选择T4做波特率发生器。
模式0与模式2为固定波特率。串口1模式0的波特率固定为系统时钟的12分频或2分频,。当辅助寄存器AUXR的B5位(S1M0x6)设置为0时波特率固定为系统时钟的12分频,设置为1时波特率固定为系统时钟的2分频。串口2模式0时,情况也相似。串口2是通过S2M0x6(S2CFG的B5位)来设置,设置为0时波特率固定为系统时钟的12分频,设置为1时波特率固定为系统时钟的2分频。模式2的波特率固定位系统时钟32分频或64分频。串口1通过PCON中的SMOD来设置:0-波特率为系统时钟的64分频,1-波特率为系统时钟的32分频。串口2通过S2CFG寄存器B6位的S2MOD0来设置:0-波特率为系统时钟的64分频,1-波特率为系统时钟的32分频。以上两种模式为串口1与串口2特有工作模式。
应用最多的位可变波特率8位数据、其次为可变工作频率9位数据,可变波特率模式的常用的波特率如下:4800,9600,19200,38400,57600,115200;最常用的是9600,很多仪器设备缺省的波特率都是9600,。在设置波特率时,最好在常用波特率中选择,最好不要设置随意的波特率,特别是与上位机通信时更应如此。串口1波特率的计算公式如下:
其实当SMOD设置为0如果我们将选做比特率发生器的定时器其都设置为16位自动重装模式,定时器时钟设置为1T或12T模式,不同串口波特率计算及定时器重装值都可采用相同公式,即:
定时器在1T模式
定时器在12T模式
这里不过多讨论波特率计算问题,前面“STC32G 单片机系列通用定时器的用法及编程”一文中已经给出了定时器做波特率发生器的初始化函数。
如果要发送数据,在串口初始化设置好后,就向串口的数据寄存器写入要发生的数据。要接收数据,就在接收中断标志请求位置1后都数据寄存器。
要进行串口编程离不开对串口相关寄存器的操控,STC32G 系列单片机串口相关的寄存器如下:
要详细了解各寄存器的作用,可仔细阅读STC32G用户手册。这里不再做进一步介绍。
STC提供了串口通信的官方库函数,我觉得用起来有些不习惯,自己写了库函数,如下:
头文件
/*STC32G_UART.h
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu ,07/12/2022
///enum//
UART_MODE //SYC_SHIFT~VBAUD_9BITS
PIN_GROUP //G1~G4
/macro function//
UART1_MODESEL_SYC_SHIFT();
UART1_MODESEL_VBAUD_8BITS();
UART1_MODESEL_CBAUD_9BITS();
UART1_MODESEL_VBAUD_9BITS();
UART1_MODESEL(selMode);
UART1_PINSEL_P30P31();
UART1_PINSEL_P36P37();
UART1_PINSEL_P16P17();
UART1_PINSEL_P43P44();
UART1_PINSEL(selPins); //selPins type is PIN_GROUP
UART1_BGTIMERSEL_T1(); //sel T1 as baudrate generator
UART1_BGTIMERSEL_T2(); //sel T1 as baudrate generator
UART1_BGTIMERSEL(selT2); //selT2 type is BOOL
UART1_RESETRI(); //reset RI flag
UART1_RESETRI(); //reset RI flag
UART1_CLEARFLAG(); //clear flag
UART2_MODESEL_SYC_SHIFT();
UART2_MODESEL_VBAUD_8BIT();
UART2_MODESEL_CBAUD_9BITS();
UART2_MODESEL_VBAUD_9BITS();
UART2_MODESEL(selMode);
UART2_PINSEL_P10P11();
UART2_PINSELP_46P47();
UART2_PINSEL(selPins); //selPins type is PIN_GROUP
UART2_RESETRI();
UART2_RESETTI();
UART2_CLEARFLAG();
UART3_MODESEL_VBAUD_8BITS();
UART3_MODESEL_VBAUD_9BITS();
UART3_MODESEL(selMode);
Uart3_PinSel_P00P01();
UART3_PINSEL_P50P51();
UART3_PINSEL(selPins); //selPins type:s PIN_GROUP
UART3_BGTIMERSEL_T2();
UART3_BGTIMERSEL_T2();
UART3_BGTIMERSEL(selT3); //selT3 type: BOOL
UART3_RESETRI();
UART3_RESETTI();
UART3_CLEARFLAG();
UART4_MODESEL_VBAUD_8BITS();
UART4_MODESEL_VBAUD_9BITS();
UART4_MODESEL(selMode); //selMode type:UART_MODE
UART4_PINSEL_P02P03();
UART4_PINSEL_P52P53();
UART4_PINSEL(selPins; //selPins type:PIN_GROUP
UART4_BGTIMERSEL_T2();
UART4_BGTIMERSEL_T4();
UART4_BGTIMERSEL(selT4); //selT4 type: BOOL
UART4_RESETRI();
UART4_RESETTI();
UART4_CLEARFLAG();
///Function/
Uart1_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate); //return void
Uart1_SendByte(BYTE mData); //return void
Uart1_RecByte(BYTE* mchar); //return BOOL
Uart1_SendString(BYTE* str); //return void
Uart1_SendStrAndEof(BYTE* str); //return void
Uart1_RecString(BYTE* str, ui8 nchar); //return BOOL
Uart1_RecStrTillEof(BYTE* str); //return BOOL
Uart2_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate); //return void
Uart2_SendByte(BYTE mData); //return void
Uart2_RecByte(BYTE* mchar); //return BOOL
Uart2_SendString(BYTE* str); //return void
Uart2_SendStrAndEof(BYTE* str); //return void
Uart2_RecString(BYTE* str, ui8 nchar); //return BOOL
Uart2_RecStrTillEof(BYTE* str); //return BOOL
Uart3_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate); //return void
Uart3_SendByte(BYTE mData); //return void
Uart3_RecByte(BYTE* mchar); //return BOOL
Uart3_SendString(BYTE* str); //return void
Uart3_SendStrAndEof(BYTE* str); //return void
Uart3_RecString(BYTE* str, ui8 nchar); //return BOOL
Uart3_RecStrTillEof(BYTE* str); //return BOOL
Uart3_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate); //return void
Uart3_SendByte(BYTE mData); //return void
Uart3_RecByte(BYTE* mchar); //return BOOL
Uart3_SendString(BYTE* str); //return void
Uart3_SendStrAndEof(BYTE* str); //return void
Uart3_RecString(BYTE* str, ui8 nchar); //return BOOL
Uart3_RecStrTillEof(BYTE* str); //return BOOL
*/
#ifndef __STC32G_UART_H
#define __STC32G_UART_H
#include "config.h"
#include "mtype.h"
#include <STC32G.H>
#include "STC32G_Timer.h"
typedef enum
{
SYC_SHIFT = 0, //sychronous shift, mode0, for S1 S2only
VBAUD_8BITS, //8bits, variable baudrate,mode1 for S1,S2,S3,S4
CBAUD_9BITS, //9bits, constant baudrate,mode2,for S1 S2 only
VBAUD_9BITS //9bits, variable baudrate, mode3,for S1,S2,S3,S4
}UART_MODE;
//************UART PIN GROUP****************************
typedef enum
{
G1 = 0, //uart1:P30/Rx,P31/Tx uart2:P10/Rx,P11/Tx uart3: P00/Rx,P01/Tx uart4: P02/Rx,P03/Tx
G2, //uart1:P36/Rx,P37/Tx uart2:P46/Rx,P47/Tx uart3: P50/Rx,P51/Tx uart4: P52/Rx,P53/Tx
G3, //uart1:P16/Rx,P17/Tx
G4 //uart1:P43/Rx, P44/Tx
}PIN_GROUP;
///Uart S1//
#define UART1_MODESEL_SYC_SHIFT() {SM1 = 0; SM0 = 0; }
#define UART1_MODESEL_VBAUD_8BITS() {SM1 = 1; SM0 = 0; }
#define UART1_MODESEL_CBAUD_9BITS() {SM1 = 0; SM0 = 1; }
#define UART1_MODESEL_VBAUD_9BITS() {SM1 = 1; SM0 = 1; }
#define UART1_MODESEL(selMode) {SM1 = (selMode)&0x01; SM0 = ((selMode) >> 1)&0x01;} //selMode type is UART_MODE
/**********************************************************************/
#define UART1_PINSEL_P30P31() {S1_S1 = 0; S1_S0 = 0;}
#define UART1_PINSEL_P36P37() {S1_S1 = 0; S1_S0 = 1;}
#define UART1_PINSEL_P16P17() {S1_S1 = 1; S1_S0 = 0;}
#define UART1_PINSEL_P43P44() {S1_S1 = 1; S1_S0 = 1;}
#define UART1_PINSEL(selPins) {S1_S0 = (selPins)&0x01; S1_S1 = ((selPins) >> 1)&0x01;} //selPins type is PIN_GROUP
/**********************************************************************/
#define UART1_BGTIMERSEL_T1() {S1BRT = 0;}
#define UART1_BGTIMERSEL_T2() {S1BRT = 1;}
#define UART1_BGTIMERSEL(selT2) {S1BRT = (selT2);} //selT2 type is BOOL
#define UART1_RESETRI() {RI = 0;}
#define UART1_RESETTI() {TI = 0;}
#define UART1_CLEARFLAG() {RI = 0;TI = 0;}
/***********************************************************
Function: Uart1_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate);
Return value: void
mode: uart1 work mode
pGroup: pin group uart1 will use
selT1: 1-select T1 ar baudrate generator, 0-T2 as baudrate generator
baudrate: badrate uart1 will use
Discription: uart1(s1) init
Example:
UartS1_Init(VBAUD_8BITS, G1, 0, 9600); //8bit variable baudrate, Pin:P30/P31, T2 as generator, badrate: 9600
***********************************************************************/
void Uart1_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate);
/***********************************************************
Function: Uart1_Sendbyte(BYTE mData);
Return value: void
mData: byte to send
Discription: send byte by uart1(S1)
Example:
Uart1_SendByte(0xFE);
***********************************************************************/
void Uart1_SendByte(BYTE mData);
/***********************************************************
Function: Uart1_RecByte(BYTE* mchar);
Return value: BOOL
mchar: variable to store received data
Discription: uart1(S1) receive byte data
Example:
char tem;
BOOL mBOOL;
mBOOL= Uart1_RecByte(&tem);
***********************************************************************/
BOOL Uart1_RecByte(BYTE* mchar);
/***********************************************************
Function: Uart1_SendString(BYTE* str);
Return value: void
str: string will be sent
Discription: uart1(S1) send string
Example:
Uart1_SendString("Hello world!");
***********************************************************************/
void Uart1_SendString(BYTE* str);
/***********************************************************
Function: Uart1_SendStrAndEof(BYTE* str);
Return value: void
str: string will be sent
Discription: uart1(S1) send string and end flag
Example:
Uart1_SendStrAndEof("Hello world!");
***********************************************************************/
void Uart1_SendStrAndEof(BYTE* str);
/***********************************************************
Function: Uart1_RecString(BYTE* str, ui8 nchar);
Return value: BOOL
str: char array to store received string
nchar; number of char to receive
Discription: S1 receive n character & store in str
Example:
Uart1_RecString(cmstr, 8);
***********************************************************************/
BOOL Uart1_RecString(BYTE* str, ui8 nchar);
/***********************************************************
Function: Uart1_RecStrTillEof(BYTE* str);
Return value: BOOL
str: char array to store received string
Discription: uart1(S1) receive character continue ,till received Eof
Example:
ui8 mstr[20];
Uart1_RecStrTillEof(mstr);
***********************************************************************/
BOOL Uart1_RecStrTillEof(BYTE* str);
///Uart S2//
#define UART2_MODESEL_SYC_SHIFT() {S2SM1 = 0; S2SM0 = 0;}
#define UART2_MODESEL_VBAUD_8BIT() {S2SM1 = 0; S2SM0 = 1;}
#define UART2_MODESEL_CBAUD_9BITS() {S2SM1 = 1; S2SM0 = 0;}
#define UART2_MODESEL_VBAUD_9BITS() {S2SM1 = 1; S2SM0 = 1;}
#define UART2_MODESEL(selMode) {S2SM1 = (selMode)&0x01; S2SM0 = ((selMode) >> 1)&0x01;} //selMode type is UART_MODE
#define UART2_PINSEL_P10P11() {S2_S = 0;}
#define UART2_PINSELP_46P47() {S2_S = 1;}
#define UART2_PINSEL(selPins) {S2_S = (selPins);} //selPins type is PIN_GROUP
#define UART2_RESETRI() {S2RI = 0;}
#define UART2_RESETTI() {S2TI = 0;}
#define UART2_CLEARFLAG() {S2RI = 0;S2TI = 0;}
/***********************************************************
Function: Uart2_Init(UART_MODE mode,PIN_GROUP pGroup,ui32 baudrate);
Return value: void
Discription: uart2(s2) init
Example:
Uart2_Init(VBAUD_8BITS, G1, 9600);
***********************************************************************/
void Uart2_Init(UART_MODE mode,PIN_GROUP pGroup,ui32 baudrate);
/***********************************************************
Function: Uart2_SendByte(BYTE mData);
Return value: void
mData: byte to send
Discription: send byte by uart2(S2)
Example:
Uart2_SendByte(0xFE);
***********************************************************************/
void Uart2_SendByte(BYTE mData);
/***********************************************************
Function: Uart2_RecByte(BYTE* mchar);
Return value: BOOL
mchar: char variable to receive char
Discription: S2 receive byte data
Example:
char tem;
BOOL bRece;
bRece = Uart2_RecByet(&tem);
***********************************************************************/
BOOL Uart2_RecByte(BYTE* mchar);
/***********************************************************
Function: Uart2_SendString(char* str);
Return value: void
str: string to send
Discription: uart2(S2) send string
Example:
Uart2_SendString("Hello world!");
***********************************************************************/
void Uart2_SendString(BYTE* str);
/***********************************************************
Function: Uart2_SendStrAndEof(BYTE* str);
Return value: void
str: string to send
Discription: uart2(S2) send string and end flag
Example:
Uart2_SendStrAndEof("Hello world!");
***********************************************************************/
void Uart2_SendStrAndEof(BYTE* str);
/***********************************************************
Function: Uart2_RecString(BYTE* str, ui8 nchar);
Return value: BOOL
str: char array to store received string
nchar: number of char to receive
Discription: uart2(S2) receive n character & store in str
Example:
Uart2_RecString(cmstr, 8);
***********************************************************************/
BOOL Uart2_RecString(BYTE* str, ui8 nchar);
/***********************************************************
Function: Uart2_RecStrTillEof(BYTE* str);
Return value: BOOL
str: char array to store received string
Discription: S2 receive character continue ,till received Eof
Example:
ui8 mstr[20];
Uart2_RecStrTillEof(mstr);
***********************************************************************/
BOOL Uart2_RecStrTillEof(BYTE* str);
///Uart S3//
#define UART3_MODESEL_VBAUD_8BITS() {S3SM0 = 0;}
#define UART3_MODESEL_VBAUD_9BITS() {S3SM0 = 1;}
#define UART3_MODESEL(selMode) {S3SM0 = ((selMode) >> 1)&0x01;} //selMode type:UART_MODE
/***********************************************************************/
#define UART3_PINSEL_P00P01() {S3_S = 0;}
#define UART3_PINSEL_P50P51() {S3_S = 1;}
#define UART3_PINSEL(selPins) {S3_S = (selPins);} //selPins type:s PIN_GROUP
/***********************************************************************/
#define UART3_BGTIMERSEL_T2() {S3ST3 = 0;}
#define UART3_BGTIMERSEL_T3() {S3ST3 = 1;}
#define UART3_BGTIMERSEL(selT3) {S3ST3 = (selT3);} //selT3 type:BOOL
/***********************************************************************/
#define UART3_RESETRI() {S3RI = 0;}
#define UART3_RESETTI() {S3TI = 0;}
#define UART3_CLEARFLAG() {S3RI = 0; S3TI = 0;}
/***********************************************************
Function: Uart3_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT3, ui32 baudrate);
Return value: void
mode: uart3 work mode
pGroup: uart3 pin group
selT3: 1-select T3 as baudrate generator, 0-T2 as baudrate generator
Discription: uart3(s3) init
Example:
Uart3_Init(VBAUD_8BITS, G1, 0, 9600);
***********************************************************************/
void Uart3_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT3, ui32 baudrate);
/***********************************************************
Function: Uart3_SendByte(char mData);
Return value: void
mData: byte to send
Discription: send byte by uart3(S3)
Example:
Uart3_SendByte(0xFE);
***********************************************************************/
void Uart3_SendByte(char mData);
/***********************************************************
Function: Uart3_RecByte(BYTE* mchar);
Return value: BOOL
mchar: char variable to store received byte
Discription: S3 receive BYTE data
Example:
char tem;
BOOL mBOOL;
mBOOL= UartS3_RecBYTE(&tem);
***********************************************************************/
BOOL Uart3_RecByte(BYTE* mchar);
/***********************************************************
Function: Uart3_SendString(BYTE* str);
Return value: void
str: char array to store received string
Discription: S3 send string
Example:
UartS3_SendString("Hello world!");
***********************************************************************/
void Uart3_SendString(BYTE* str);
/***********************************************************
Function: UartS3_SendStrAndEof(BYTE* str);
Return value: void
str: char array to store received string
Discription: S3 send string and end flag
Example:
UartS3_SendStrAndEof("Hello world!");
***********************************************************************/
void UartS3_SendStrAndEof(BYTE* str);
/***********************************************************
Function: Uart3_RecString(BYTE* str, ui8 nchar);
Return value: BOOL
str: char array to store received string
nchar: number of receiving char
Discription: S1 receive n character & store in str
Example:
UartS3_RecString(cmstr, 8);
***********************************************************************/
BOOL Uart3_RecString(BYTE* str, ui8 nchar);
/***********************************************************
Function: Uart3_RecStrTillEof(BYTE* str);
Return value: BOOL
str: char array to store received string
Discription: S2 receive character continue ,till received flag of end
Example:
ui8 mstr[20];
Uart3_RecStrTillEof(mstr);
***********************************************************************/
BOOL Uart3_RecStrTillEof(BYTE* str);
///Uart S4//
#define UART4_MODESEL_VBAUD_8BITS() {S4SM0 = 0;}
#define UART4_MODESEL_VBAUD_9BITS() {S4SM0 = 1;}
#define UART4_MODESEL(selMode) {S4SM0 = ((selMode) >> 1);} //selMode type:UART_MODE
//***********************************************************************/
#define UART4_PINSEL_P02P03(){S4_S = 0;}
#define UART4_PINSEL_P52P53(){S4_S = 1;}
#define UART4_PINSEL(selPins) {S4_S = (selPins);} //selPins type:PIN_GROUP
/***********************************************************************/
#define UART4_BGTIMERSEL_T2() {S4ST4 = 0;}
#define UART4_BGTIMERSEL_T4() {S4ST4 = 1;}
#define UART4_BGTIMERSEL(selT4) {S4ST4 = (selT4);} //selT4 type: BOOL
/***********************************************************************/
#define UART4_RESETRI() {S4RI = 0;}
#define UART4_RESETTI() {S4TI = 0;}
#define UART4_CLEARFLAG() {S4RI = 0; S4TI = 0;}
/***********************************************************
Function: Uart4_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT4, ui32 baudrate);
Return value: void
Discription: uart4(s4) init
Example:
UartS4_Init(VBAUD_8BITS, G1, 0, 9600);
***********************************************************************/
void Uart4_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT4, ui32 baudrate);
/***********************************************************
Function: Uart4_SendByte(BYTE mData);
Return value: void
mData: byte to send
Discription: send BYTE by S1
Example:
Uart4_SendByte(0xFE); //send 0xFE by uart4
***********************************************************************/
void Uart4_SendByte(BYTE mData);
/***********************************************************
Function: Uart4_RecByte(BYTE* mchar);
Return value: BOOL
mchar: variable to store receive char
Discription: uart4(S4) receive BYTE data
Example:
char tem;
BOOL mBOOL;
mBOOL= UartS4_RecByte(&tem);
***********************************************************************/
BOOL Uart4_RecByte(BYTE* mchar);
/***********************************************************
Function: Uart4_SendString(BYTE* str);
Return value: void
str: string to send
Discription: uart4(S4) send string
Example:
Uart4_SendString("Hello world!");
***********************************************************************/
void Uart4_SendString(BYTE* str);
/***********************************************************
Function: Uart4_SendStrAndEof(BYTE* str);
Return value: void
str: string to send
Discription: uart4(S4) send string and end flag
Example:
Uart4_SendStrAndEof("Hello world!");
***********************************************************************/
void UartS4_SendStrAndEof(BYTE* str);
/***********************************************************
Function: UartS4_RecString(BYTE* str, ui8 nchar);
Return value: BOOL
str: char array to store received string
nchar: number of char to receive
Discription: uart4(S4) receive n character & store in str
Example:
Uart4_RecString(cmstr, 8);
***********************************************************************/
BOOL Uart4_RecString(BYTE* str, ui8 nchar);
/***********************************************************
Function: Uart4_RecStrTillEof(BYTE* str);
Return value: BOOL
str: char array to store receive string
Discription: S4 receive character continue ,till received Eof
Example:
ui8 mstr[20];
UartS4_RecStrTillEof(mstr);
***********************************************************************/
BOOL Uart4_RecStrTillEof(BYTE* str);
#endif
源代码
/*STC32G_UART.c
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu, 07/12/2022
*/
#include "STC32G_UART.h"
///Uart S1//
//***********************************************************
void Uart1_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate)
{
//Uart1_ClearFlag();
TI = 0;
RI = 0;
Uart1_ModeSel(mode);
Uart1_PinSel(pGroup);
switch(pGroup)
{
case G1:
STC32G_P3MODE_BIIO(PIN0|PIN1);
break;
case G2:
STC32G_P3MODE_BIIO(PIN6|PIN7);
break;
case G3:
STC32G_P1MODE_BIIO(PIN6|PIN7);
break;
case G4:
STC32G_P4MODE_BIIO(PIN3|PIN4);
break;
}
Uart1_BGTimerSel(selT2);
REN = 1; //receive enable
if(!selT2)
T1BrOutInit(baudrate);
else
T2BrOutInit(baudrate);
}
//End of Uart1_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT2, ui32 baudrate)
//***********************************************************************
void Uart1_SendByte(BYTE mData)
{
TI = 0;
SBUF = mData;
while(!TI);
//TI = 0;
}
//End of Uart1_SendByte(BYTE mData)
//***********************************************************************
BOOL Uart1_RecByte(BYTE* mchar)
{
u32 i = 0;
RI = 0;
while(!RI)
{
if(i < FOSC/1000)
i++;
else
return 0;
}
*mchar = SBUF;
//RI = 0;
return 1;
}
//End of Uart1_RecByte(BYTE* mchar)
//***********************************************************************
void Uart1_SendString(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart1_SendByte(str[i]);
i++;
}
TI = 0;
EA = tem;
}
//End of Uart1_SendString(BYTE* str)
//***********************************************************************
void Uart1_SendStrAndEof(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart1_SendByte(str[i]);
i++;
}
Uart1_SendByte(0xFF);
TI = 0;
EA = tem;
}
//End of Uart1_SendStrAndEof(BYTE* str)
//***********************************************************************
BOOL Uart1_RecString(BYTE* str, ui8 nchar)
{
ui8 i = 0;
ui8 tem = EA;
memset(str,0,strlen(str));
EA = 0;
while(i < nchar)
{
if(Uart1_RecByte(&str[i]))
i++;
else
{
EA = tem;
return 0;
}
}
RI = 0;
EA = tem;
return 1;
}
//End of Uart1_RecString(BYTE* str, ui8 nchar)
//***********************************************************************
BOOL Uart1_RecStrTillEof(BYTE* str)
{
ui8 i = 0;
ui8 tem = EA;
memset(str,0,strlen(str));
EA = 0;
for(;;)
{
if(Uart1_RecByte(&str[i]))
{
if(str[i] == 0xFF)
break;
else
i++;
}
else
{
EA = tem;
return 0;
}
}
RI = 0;
EA = tem;
return 1;
}
//End of Uart1_RecStrTillEof(BYTE* str)
///Uart S2//
//***********************************************************
void Uart2_Init(UART_MODE mode,PIN_GROUP pGroup,ui32 baudrate)
{
Uart2_ClearFlag();
Uart2_ModeSel(mode);
Uart2_PinSel(pGroup);
switch( pGroup)
{
case G1:
STC32G_P1MODE_BIIO(PIN0|PIN1);
break;
case G2:
STC32G_P4MODE_BIIO(PIN6|PIN7);
break;
}
S2REN = 1; //S2 receive enbale
T2BrOutInit(baudrate);
}
//End of Uart2_Init(UART_MODE mode,PIN_GROUP pGroup,ui32 baudrate)
//***********************************************************************
void Uart2_SendByte(BYTE mData)
{
S2TI = 0;
S2BUF = mData;
while(!S2TI);
//S2TI = 0;
}
//End of Uart2_SendByte(BYTE mData)
//***********************************************************************
BOOL Uart2_RecByte(BYTE* mchar)
{
u32 i = 0;
S2RI = 0;
while(!S2RI)
{
if(i < FOSC/1000) //FOSC was defineed in config.h
i++;
else
return 0;
}
*mchar = S2BUF;
//S2RI = 0;
return 1;
}
//End of Uart2_RecByte(BYTE* mchar)
//***********************************************************************
void Uart2_SendString(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart2_SendByte(str[i]);
i++;
}
S2TI = 0;
EA = tem;
}
//End of Uart2_SendString(BYTE* str)
//***********************************************************************
void Uart2_SendStrAndEof(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart2_SendByte(str[i]);
i++;
}
Uart2_SendByte(0xFF);
S2TI = 0;
EA = tem;
}
//End of Uart2_SendStrAndEof(BYTE* str)
//***********************************************************************
BOOL Uart2_RecString(BYTE* str, ui8 nchar)
{
ui8 tem = EA;
ui8 i = 0;
memset(str,0,strlen(str));
EA = 0;
while(i < nchar)
{
if(Uart2_RecByte(&str[i]))
i++;
else
{
EA = tem;
return 0;
}
}
S2RI = 0;
EA = tem;
return 1;
}
//End of Uart2_RecString(BYTE* str, ui8 nchar)
//***********************************************************************
BOOL Uart2_RecStrTillEof(BYTE* str)
{
ui8 i = 0;
ui8 tem = EA;
memset(str,0,strlen(str));
EA = 0;
for(;;)
{
if(Uart2_RecByte(&str[i]))
{
if(str[i] == 0xFF)
break;
else
i++;
}
else
{
EA = tem;
return 0;
}
}
S2RI = 0;
EA = tem;
return 1;
}
//End of Uart2_RecStrTillEof(BYTE* str)
///Uart S3//
//***********************************************************
void Uart3_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT3, ui32 baudrate)
{
Uart3_ClearFlag();
Uart3_ModeSel(mode);
Uart3_PinSel(pGroup);
switch( pGroup)
{
case G1:
STC32G_P0MODE_BIIO(PIN0|PIN1);
break;
case G2:
STC32G_P5MODE_BIIO(PIN0|PIN1);
break;
}
Uart3_BGTimerSel(selT3);
S3REN = 1; //receive enable
if(selT3)
T3BrOutInit(baudrate);
else
T2BrOutInit(baudrate);
}
//End of Uart3_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT3, ui32 baudrate)
//***********************************************************************
void Uart3_SendByte(char mData)
{
S3TI = 0;
S3BUF = mData;
while(!S3TI);
//S3TI = 0;
}
//End of Uart3_SendByte(char mData)
//***********************************************************************
BOOL Uart3_RecByte(BYTE* mchar)
{
u32 i = 0;
S3RI = 0;
while(!S3RI)
{
if(i < FOSC)
i++;
else
return 0;
}
*mchar = S3BUF;
//S3RI = 0;
return 1;
}
//End of Uart3_RecByte(BYTE* mchar)
//***********************************************************************
void Uart3_SendString(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart3_SendByte(str[i]);
i++;
}
S3TI = 0;
EA = tem;
}
//End of Uart3_SendString(BYTE* str)
//***********************************************************************
void UartS3_SendStrAndEof(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart3_SendByte(str[i]);
i++;
}
Uart3_SendByte(0xFF);
S3TI = 0;
EA = tem;
}
//End of UartS3_SendStrAndEof(BYTE* str)
//***********************************************************************
BOOL Uart3_RecString(BYTE* str, ui8 nchar)
{
ui8 tem = EA;
ui8 i = 0;
memset(str,0,strlen(str));
EA = 0;
while(i < nchar)
{
if(Uart3_RecByte(&str[i]))
i++;
else
{
EA = tem;
return 0;
}
}
EA = tem;
return 1;
}
//End of Uart3_RecString(BYTE* str, ui8 nchar)
//***********************************************************************
BOOL Uart3_RecStrTillEof(BYTE* str)
{
ui8 i = 0;
ui8 tem = EA;
memset(str,0,strlen(str));
EA = 0;
for(;;)
{
if(Uart3_RecByte(&str[i]))
{
if(str[i] == 0xFF)
break;
else
i++;
}
else
{
EA = tem;
return 0;
}
}
EA = tem;
return 1;
}
//End of Uart3_RecStrTillEof(BYTE* str)
///Uart S4//
//***********************************************************
void Uart4_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT4, ui32 baudrate)
{
Uart4_ClearFlag();
Uart4_ModeSel(mode);
Uart4_PinSel(pGroup);
switch( pGroup)
{
case G1:
STC32G_P0MODE_BIIO(PIN2|PIN3);
break;
case G2:
STC32G_P5MODE_BIIO(PIN2|PIN3);
break;
}
Uart4_BGTimerSel(selT4);
S4REN = 1; //receive enable
if(selT4)
T4BrOutInit(baudrate);
else
T2BrOutInit(baudrate);
}
//End of Uart4_Init(UART_MODE mode,PIN_GROUP pGroup, BOOL selT4, ui32 baudrate)
//***********************************************************************
void Uart4_SendByte(BYTE mData)
{
S4TI = 0;
S4BUF = mData;
while(!S4TI);
//S4RI = 0;
}
//End of Uart4_SendByte(BYTE mData)
//***********************************************************************
BOOL Uart4_RecByte(BYTE* mchar)
{
u32 i = 0;
ui8 tem = 1;
S4RI = 0;
while(!S4RI)
{
if(i < FOSC)
i++;
else
return 0;
}
*mchar = S4BUF;
//S4RI = 0;
return tem;
}
//End of Uart4_RecByte(BYTE* mchar)
//***********************************************************************
void Uart4_SendString(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart4_SendByte(str[i]);
i++;
}
S4TI = 0;
EA = tem;
}
//End of Uart4_SendString(BYTE* str)
//***********************************************************************
void UartS4_SendStrAndEof(BYTE* str)
{
ui8 tem = EA;
ui8 i = 0;
EA = 0;
while(str[i] != 0)
{
Uart4_SendByte(str[i]);
i++;
}
Uart4_SendByte(0xFF);
S4TI = 0;
EA = tem;
}
//End of UartS4_SendStrAndEof(BYTE* str)
//***********************************************************************
BOOL Uart4_RecString(BYTE* str, ui8 nchar)
{
ui8 tem = EA;
ui8 i = 0;
memset(str,0,strlen(str));
EA = 0;
while(i < nchar)
{
if(Uart4_RecByte(&str[i]))
i++;
else
{
EA = tem;
return 0;
}
}
S4RI = 0;
EA = tem;
return 1;
}
//End of Uart4_RecString(BYTE* str, ui8 nchar)
//***********************************************************************
BOOL Uart4_RecStrTillEof(BYTE* str)
{
ui8 i = 0;
ui8 tem = EA;
memset(str,0,strlen(str));
EA = 0;
for(;;)
{
if(!Uart4_RecByte(&str[i]))
{
if(str[i] == 0xFF)
break;
else
i++;
}
else
{
EA = tem;
return 0;
}
}
S4RI = 0;
EA = tem;
return 1;
}
//End of Uart4_RecStrTillEof(BYTE* str)
现在写一个测试程序将一些字符串发送到,串口助手,看能否发送数据成功。
头文件
/*main.h
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu ,01/27/2023
*/
#ifndef __MAIN_H__
#define __MAIN_H__
#include "myport.h"
#include "mtype.h"
#include "config.h"
#include "STC32G_GPIO.h"
#include "STC32G_UART.h"
char str[25] = "This is uart test";
char str1[25] = "1234567890";
char str2[25] = "ABCDEFGHIJKLMN";
char str3[25] = "abcdefghijklmn";
#endif
源代码:
/*main.c
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu, 01/27/2023
*/
#include "main.h"
void main()
{
SysInit();
Uart1_Init(VBAUD_8BITS,G1, 0, 9600);
while(1)
{
Uart1_SendString(str);
Uart1_SendString("\r\n");
Uart1_SendString(str1);
Uart1_SendString("\r\n");
Uart1_SendString(str2);
Uart1_SendString("\r\n");
Uart1_SendString(str3);
Uart1_SendString("\r\n");
Uart1_SendString(str3);
Uart1_SendString("\r\n");
Uart1_SendString(" ");
Uart1_SendString("\r\n");
}
}
//End of main()
编译结果如下:
先将编译后的程序下载到STC32G12K128单片机,注意在config.h中设置的系统频率为30MHz,如下:
在下载时需设置程序运行时IRC频率为30MHz(否则会导致波特率不正确),如下:
下载结果如下:
打开串口,在串口助手上看到的结果如下:
说明数据发送成功,且数据发送无误。
库函数及验证程序源码下载链接:STC32G系列单片机定时器及串行通信接口函数库(非官方函数库)
文章来源:https://uudwc.com/A/19DGo
文章来源地址https://uudwc.com/A/19DGo