- 打卡等级:常住居民I
- 打卡总天数:78
- 最近打卡:2026-06-29 10:44:00
注册会员
- 积分
- 144
|
发表于 2026-6-3 16:26:08
|
显示全部楼层
测试你的代码用的串口1不会有卡死现象,就把uart文件所有关于串口1的全部换成串口2,发个10来次就卡死,现象为串口工具点都点不动了
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCAI.com ---------------------------------------------*/
/* --- BBS: www.STCAIMCU.com -----------------------------------------*/
/* --- QQ: 800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
#include "stc.h"
#include "uart.h"
#include "usb.h"
#include "usb_req_class.h"
BOOL UartBusy;
void uart_init()
{
P_SW2 &= ~0x01; //UART2/USART2: RxD2(P1.0), TxD2(P1.1)
S2CON = (S2CON & 0x3f) | 0x40;
AUXR |= 0x04; //定时器时钟1T模式
T2L = BR(115200);
T2H = BR(115200) >> 8;
AUXR |= T2R; //启动定时器
IE2 |= ES2; //使能串口中断
S2CON |= S2REN; //允许接收
LineCoding.dwDTERate = 0x00c20100; //115200
LineCoding.bCharFormat = 0;
LineCoding.bParityType = 0;
LineCoding.bDataBits = 8;
}
void UART2_Isr() interrupt UART2_VECTOR
{
if (S2CON & 0x02)
{
S2CON &= ~0x02; //清中断标志
TxBuffer[TxWptr++] = S2BUF;
}
if (S2CON & 0x01)
{
S2CON &= ~0x01; //清中断标志
UartBusy = 0;
}
}
void uart_set_parity(BYTE parity)
{
switch (parity)
{
default:
case NONE_PARITY:
S2CON = 0x50;
break;
case ODD_PARITY:
case EVEN_PARITY:
case MARK_PARITY:
S2CON = 0xd8;
break;
case SPACE_PARITY:
S2CON = 0xd0;
break;
}
}
void uart_set_baud(DWORD baud)
{
WORD temp;
temp = (WORD)BR(baud);
T2L = temp;
T2H = temp >> 8;
}
void uart_polling()
{
BYTE dat;
BYTE cnt;
if (DeviceState != DEVSTATE_CONFIGURED) //如果USB配置没有完成,就直接退出
return;
if (!UsbInBusy && (TxRptr != TxWptr))
{
IE2 &= ~0x80; //EUSB = 0;
UsbInBusy = 1;
usb_write_reg(INDEX, 2);
cnt = 0;
while (TxRptr != TxWptr)
{
usb_write_reg(FIFO2, TxBuffer[TxRptr++]);
cnt++;
if (cnt == EP2IN_SIZE) break;
}
usb_write_reg(INCSR1, INIPRDY);
IE2 |= 0x80; //EUSB = 1;
}
if (!UartBusy && (RxRptr != RxWptr))
{
dat = RxBuffer[RxRptr++];
UartBusy = 1;
switch (LineCoding.bParityType)
{
case NONE_PARITY:
case SPACE_PARITY:
// TB8 = 0;
S2CON &= ~S2TB8;
break;
case ODD_PARITY:
ACC = dat;
// TB8 = !P;
if(!P)
S2CON |= S2TB8;
else
S2CON &= ~S2TB8;
break;
case EVEN_PARITY:
ACC = dat;
// TB8 = P;
if(P)
S2CON |= S2TB8;
else
S2CON &= ~S2TB8;
break;
case MARK_PARITY:
// TB8 = 1;
S2CON |= S2TB8;
break;
}
S2BUF = dat;
while (UartBusy);
}
if (UsbOutBusy)
{
IE2 &= ~0x80; //EUSB = 0;
if ((BYTE)(RxWptr - RxRptr) < (BYTE)(256 - EP2OUT_SIZE))
{
UsbOutBusy = 0;
usb_write_reg(INDEX, 2);
usb_write_reg(OUTCSR1, 0);
}
IE2 |= 0x80; //EUSB = 1;
}
}
|
|