我使用STC官方(VSCode中EDIE插件下载的,有点区别)的串口写入函数TX1_write2buff,但是烧录到 STC8H4K64TL / STC8G1K08A 单片机,运行到TX1_write2buff后并没有输出(阻塞的方式是有输出的),球大佬。
改了一下帖子,代码精简了一点。
这是我的主函数
#include "GPIO.h"
#include "UART.h"
#include "config.h"
#include "delay.h"/
#include <compiler.h>
// 串口波特率设置
#define BAUD 115200 // 波特率
void UART_Init()
{
/* ===========================
* UART1 参数配置
* =========================== */
COMx_InitDefine uart;
uart.UART_Mode = UART_8bit_BRTx; // 8 位 UART,波特率发生器
uart.UART_BRT_Use = BRT_Timer1; // 使用 Timer1
uart.UART_BaudRate = BAUD;
uart.Morecommunicate = DISABLE; // 不启用多机通信
uart.UART_RxEnable = ENABLE; // 允许接收
uart.BaudRateDouble = DISABLE; // 不倍速,稳定优先
uart.UART_Interrupt = ENABLE; // 开 UART1 中断
uart.UART_Priority = Priority_0; // ★最低优先级★
uart.UART_P_SW = UART1_SW_P30_P31; // P3.0 RX, P3.1 TX
/* ===========================
* 初始化 UART1
* =========================== */
int ret_code = UART_Configuration(UART1, &uart);
/* ===========================
* 初始化软件状态(很关键)
* 防止上电或重入时脏数据
* =========================== */
ES = 0; // 关闭中断
COM1.TX_read = 0;
COM1.TX_write = 0;
COM1.B_TX_busy = 0;
COM1.RX_Cnt = 0;
COM1.RX_TimeOut = 0;
COM1.B_RX_OK = 0;
ES = 1; // 开启中断
}
// 发送一个字节
void UART_SendByte(unsigned char c)
{
SBUF = c;
while (!TI)
; // 等待发送完成
TI = 0;
// TX1_write2buff(c); // 这里取消注释后,没有双倍的输出!!!!!!!!!!!!!!!
}
// 接收一个字节
unsigned char UART_ReceiveByte()
{
while (!RI)
; // 等待接收完成
RI = 0;
return SBUF;
}
void main()
{
unsigned char c;
UART_Init();
// ES = 1;
EA = 1;
while (1)
{
c = UART_ReceiveByte(); // 接收数据
UART_SendByte(c); // 原封不动发送回去
}
}
"""
这是我使用的UART.c(部分)
"""
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
#include "UART.h"
#ifdef UART1
COMx_Define COM1;
u8 __xdata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲
u8 __xdata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲
#endif
#ifdef UART2
COMx_Define COM2;
u8 __xdata TX2_Buffer[COM_TX2_Lenth]; //发送缓冲
u8 __xdata RX2_Buffer[COM_RX2_Lenth]; //接收缓冲
#endif
#ifdef UART3
COMx_Define COM3;
u8 __xdata TX3_Buffer[COM_TX3_Lenth]; //发送缓冲
u8 __xdata RX3_Buffer[COM_RX3_Lenth]; //接收缓冲
#endif
#ifdef UART4
COMx_Define COM4;
u8 __xdata TX4_Buffer[COM_TX4_Lenth]; //发送缓冲
u8 __xdata RX4_Buffer[COM_RX4_Lenth]; //接收缓冲
#endif
u8 UART_Configuration(u8 UARTx, COMx_InitDefine *COMx)
{
u8 i;
u32 j;
#ifdef UART1
if(UARTx == UART1)
{
COM1.id = 1;
COM1.TX_read = 0;
COM1.TX_write = 0;
COM1.B_TX_busy = 0;
COM1.RX_Cnt = 0;
COM1.RX_TimeOut = 0;
COM1.B_RX_OK = 0;
for(i=0; i<COM_TX1_Lenth; i++) TX1_Buffer[ i] = 0;
for(i=0; i<COM_RX1_Lenth; i++) RX1_Buffer[ i] = 0;
if(COMx->UART_Priority > Priority_3) return 2; //错误
UART1_Priority(COMx->UART_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
if(COMx->UART_Mode > UART_9bit_BRTx) return 2; //模式错误
SCON = (SCON & 0x3f) | COMx->UART_Mode;
if((COMx->UART_Mode == UART_9bit_BRTx) || (COMx->UART_Mode == UART_8bit_BRTx)) //可变波特率
{
j = (MAIN_Fosc / 4) / COMx->UART_BaudRate; //按1T计算
if(j >= 65536UL) return 2; //错误
j = 65536UL - j;
if(COMx->UART_BRT_Use == BRT_Timer1)
{
TR1 = 0;
AUXR &= ~0x01; //S1 BRT Use Timer1;
TMOD &= ~(1<<6); //Timer1 set As Timer
TMOD &= ~0x30; //Timer1_16bitAutoReload;
AUXR |= (1<<6); //Timer1 set as 1T mode
TH1 = (u8)(j>>8);
TL1 = (u8)j;
ET1 = 0; //禁止中断
TMOD &= ~0x40; //定时
INT_CLKO &= ~0x02; //不输出时钟
TR1 = 1;
}
else if(COMx->UART_BRT_Use == BRT_Timer2)
{
AUXR &= ~(1<<4); //Timer stop
AUXR |= 0x01; //S1 BRT Use Timer2;
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
TH2 = (u8)(j>>8);
TL2 = (u8)j;
IE2 &= ~(1<<2); //禁止中断
AUXR |= (1<<4); //Timer run enable
}
else return 2; //错误
}
else if(COMx->UART_Mode == UART_ShiftRight)
{
if(COMx->BaudRateDouble == ENABLE) AUXR |= (1<<5); //固定波特率SysClk/2
else AUXR &= ~(1<<5); //固定波特率SysClk/12
}
else if(COMx->UART_Mode == UART_9bit) //固定波特率SysClk*2^SMOD/64
{
if(COMx->BaudRateDouble == ENABLE) PCON |= (1<<7); //固定波特率SysClk/32
else PCON &= ~(1<<7); //固定波特率SysClk/64
}
if(COMx->UART_Interrupt == ENABLE) ES = 1; //允许中断
else ES = 0; //禁止中断
if(COMx->UART_RxEnable == ENABLE) REN = 1; //允许接收
else REN = 0; //禁止接收
P_SW1 = (P_SW1 & 0x3f) | (COMx->UART_P_SW & 0xc0); //切换IO
return 0;
}
#endif
#ifdef UART2
// 没用到就删了
#endif
#ifdef UART3
// 没用到就删了
#endif
#ifdef UART4
// 没用到就删了
#endif
return 2; //错误
}
/*********************************************************/
/********************* UART1 函数 ************************/
#ifdef UART1
void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1
{
TX1_Buffer[COM1.TX_write] = dat; //装发送缓冲
if(++COM1.TX_write >= COM_TX1_Lenth) COM1.TX_write = 0;
if(COM1.B_TX_busy == 0) //空闲
{
COM1.B_TX_busy = 1; //标志忙
TI = 1; //触发发送中断
}
}
void PrintString1(u8 *puts)
{
for (; *puts != 0; puts++) TX1_write2buff(*puts); //遇到停止符0结束
}
void UART1_int (void) __interrupt(UART1_VECTOR)
{
if(RI)
{
RI = 0;
if(COM1.B_RX_OK == 0)
{
if(COM1.RX_Cnt >= COM_RX1_Lenth) COM1.RX_Cnt = 0;
RX1_Buffer[COM1.RX_Cnt++] = SBUF;
COM1.RX_TimeOut = TimeOutSet1;
}
}
if(TI)
{
TI = 0;
if(COM1.TX_read != COM1.TX_write)
{
SBUF = TX1_Buffer[COM1.TX_read];
if(++COM1.TX_read >= COM_TX1_Lenth) COM1.TX_read = 0;
}
else COM1.B_TX_busy = 0;
}
}
#endif
/********************* UART2 函数 ************************/
// 没用到就删了
"""
这是我使用的UART.h
"""
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
#ifndef __UART_H
#define __UART_H
#include "config.h"
#define UART1 1
//#define UART2 2
//#define UART3 3
//#define UART4 4
#ifdef UART1
#define COM_TX1_Lenth 128
#define COM_RX1_Lenth 128
#endif
#ifdef UART2
#define COM_TX2_Lenth 128
#define COM_RX2_Lenth 128
#endif
#ifdef UART3
#define COM_TX3_Lenth 128
#define COM_RX3_Lenth 128
#endif
#ifdef UART4
#define COM_TX4_Lenth 128
#define COM_RX4_Lenth 128
#endif
#define UART_ShiftRight 0 //同步移位输出
#define UART_8bit_BRTx (1<<6) //8位数据,可变波特率
#define UART_9bit (2<<6) //9位数据,固定波特率
#define UART_9bit_BRTx (3<<6) //9位数据,可变波特率
#define UART1_SW_P30_P31 0
#define UART1_SW_P36_P37 (1<<6)
#define UART1_SW_P16_P17 (2<<6)
#define UART1_SW_P43_P44 (3<<6)
#define UART2_SW_P10_P11 0
#define UART2_SW_P46_P47 1
#define UART3_SW_P00_P01 0
#define UART3_SW_P50_P51 2
#define UART4_SW_P02_P03 0
#define UART4_SW_P52_P53 4
#define TimeOutSet1 5
#define TimeOutSet2 5
#define TimeOutSet3 5
#define TimeOutSet4 5
#define BRT_Timer1 1
#define BRT_Timer2 2
#define BRT_Timer3 3
#define BRT_Timer4 4
typedef struct
{
u8 id; //串口号
u8 TX_read; //发送读指针
u8 TX_write; //发送写指针
u8 B_TX_busy; //忙标志
u8 RX_Cnt; //接收字节计数
u8 RX_TimeOut; //接收超时
u8 B_RX_OK; //接收块完成
} COMx_Define;
typedef struct
{
u8 UART_Mode; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
u8 UART_BRT_Use; //使用波特率, BRT_Timer1,BRT_Timer2
u32 UART_BaudRate; //波特率, ENABLE,DISABLE
u8 Morecommunicate; //多机通讯允许, ENABLE,DISABLE
u8 UART_RxEnable; //允许接收, ENABLE,DISABLE
u8 BaudRateDouble; //波特率加倍, ENABLE,DISABLE
u8 UART_Interrupt; //中断控制, ENABLE,DISABLE
u8 UART_Priority; //优先级, Priority_0,Priority_1,Priority_2,Priority_3
u8 UART_P_SW; //切换端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
} COMx_InitDefine;
#ifdef UART1
extern COMx_Define COM1;
extern u8 __xdata TX1_Buffer[COM_TX1_Lenth]; //发送缓冲
extern u8 __xdata RX1_Buffer[COM_RX1_Lenth]; //接收缓冲
#endif
#ifdef UART2
extern COMx_Define COM2;
extern u8 __xdata TX2_Buffer[COM_TX2_Lenth]; //发送缓冲
extern u8 __xdata RX2_Buffer[COM_RX2_Lenth]; //接收缓冲
#endif
#ifdef UART3
extern COMx_Define COM3;
extern u8 __xdata TX3_Buffer[COM_TX3_Lenth]; //发送缓冲
extern u8 __xdata RX3_Buffer[COM_RX3_Lenth]; //接收缓冲
#endif
#ifdef UART4
extern COMx_Define COM4;
extern u8 __xdata TX4_Buffer[COM_TX4_Lenth]; //发送缓冲
extern u8 __xdata RX4_Buffer[COM_RX4_Lenth]; //接收缓冲
#endif
u8 UART_Configuration(u8 UARTx, COMx_InitDefine *COMx);
#ifdef UART1
void TX1_write2buff(u8 dat); //写入发送缓冲,指针+1
void PrintString1(u8 *puts);
#endif
#ifdef UART2
void TX2_write2buff(u8 dat); //写入发送缓冲,指针+1
void PrintString2(u8 *puts);
#endif
#ifdef UART3
void TX3_write2buff(u8 dat); //写入发送缓冲,指针+1
void PrintString3(u8 *puts);
#endif
#ifdef UART4
void TX4_write2buff(u8 dat); //写入发送缓冲,指针+1
void PrintString4(u8 *puts);
#endif
//void COMx_write2buff(COMx_Define *COMx, u8 dat); //写入发送缓冲,指针+1
//void PrintString(COMx_Define *COMx, u8 *puts);
#endif |