USB转双串口例程如何改回单串口,只使用P36P37?| 已解决
例程 STC-USB-TO-2UART-TINY-STC8H8K64U-20231101\12-CDC(Communication Device Class)协议范例 - USB-CDC转单串口,使用的串口端口是P10P11。
而例程 “A1, USB CDC转双串口开源程序 - 做自动停电上电烧录器”,
使用的双串口端口分别是P10P11和P36P37。
现在想改回单串口,并且只使用P36P37,P10P11要另加作他用,改了一个上午都没法实现目的。
请问大佬:用上述的哪一个来改比较容易些,并且该从哪个地方进行修改?
uart.c
/*---------------------------------------------------------------------*/
/* --- 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 "util.h"
#include "usb_req_class.h"
BOOL UartBusy;
void uart_init()
{
P_SW1 |= 0x40;
P3M0 = 0x00; // P3.0,P3.1设置为准双向模式
P3M1 = 0x00;
AUXR |= 0x14; // 定时器2时钟1T模式,开始计时
IE2 |= 1; // 允许中断
SCON = 0x50;// 8位数据,可变波特率
AUXR |= 0x01; // 串口1选择定时器2为波特率发生器
AUXR |= 0x04; // 定时器时钟1T模式
T2L = BR(115200);
T2H = BR(115200) >> 8;
AUXR |= 0x10; // 定时器2开始计时
LineCoding.dwDTERate = REV4(115200);
LineCoding.bCharFormat = 0;
LineCoding.bParityType = 0;
LineCoding.bDataBits = 8;
}
void uart1_isr() interrupt 4
{
if (TI) // 检测串口1发送中断
{
TI = 0; // 清除串口1发送中断请求位
UartBusy = 0;
}
if (RI) // 检测串口1接收中断
{
RI = 0; // 清除串口1接收中断请求位
TxBuffer = S2BUF;
}
}
void uart_set_parity(BYTE parity)
{
switch (parity)
{
case NONE_PARITY:
SCON = 0x50;
break;
case ODD_PARITY:
case EVEN_PARITY:
case MARK_PARITY:
SCON = 0xda;
break;
case SPACE_PARITY:
SCON = 0xd2;
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)
return;
if (!UsbInBusy && (TxRptr != TxWptr))
{
IE2 &= ~0x80; // EUSB = 0;
UsbInBusy = 1;
usb_write_reg(INDEX, 1);
cnt = 0;
while (TxRptr != TxWptr)
{
usb_write_reg(FIFO1, TxBuffer);
cnt++;
if (cnt == EP1IN_SIZE)
break;
}
usb_write_reg(INCSR1, INIPRDY);
IE2 |= 0x80; // EUSB = 1;
}
if (!UartBusy && (RxRptr != RxWptr))
{
dat = RxBuffer;
UartBusy = 1;
switch (LineCoding.bParityType)
{
case NONE_PARITY:
case SPACE_PARITY:
SCON &= ~0x08; // STB8 = 0;
break;
case ODD_PARITY:
ACC = dat;
if (P)
SCON &= ~0x08; // STB8 = !P;
else
SCON |= 0x08;
break;
case EVEN_PARITY:
ACC = dat;
if (P)
SCON |= 0x08; // STB8 = P;
else
SCON &= ~0x08;
break;
case MARK_PARITY:
SCON |= 0x08; // STB8 = 1;
break;
}
SBUF = dat;
while (UartBusy)
;
}
if (UsbOutBusy)
{
IE2 &= ~0x80; // EUSB = 0;
if (RxWptr - RxRptr < 256 - EP1OUT_SIZE)
{
UsbOutBusy = 0;
usb_write_reg(INDEX, 1);
usb_write_reg(OUTCSR1, 0);
}
IE2 |= 0x80; // EUSB = 1;
}
}
【新提醒】一箭一雕, USB转单串口例程 - USB:USB-CDC虚拟串口/就是串口,一箭双雕之USB转双串口,[鼠标+键盘]的HID复合设备 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
这 【一箭双雕之USB转双串口】程序包中,已单独列出这个程序了
【新提醒】USB-超强演示程序包全家福发布,RMB0.99 !一箭双雕之USB转双串口 全新升级 - USB:USB-CDC虚拟串口/就是串口,一箭双雕之USB转双串口,[鼠标+键盘]的HID复合设备 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
本帖最后由 xxkj2010 于 2024-1-29 11:41 编辑
uart.c修改后的代码如下,但仍然尚未正常。
#include "stc.h"
#include "uart.h"
#include "usb.h"
#include "util.h"
#include "usb_req_class.h"
BOOL UartBusy;
void uart_init()
{
P_SW1 |= 0x40;
P3M0 = 0x00; // P3.6,P3.7设置为准双向模式
P3M1 = 0x00;
AUXR |= 0x14; // 定时器2时钟1T模式,开始计时
IE2 |= 1; // 允许中断
SCON = 0x50;// 8位数据,可变波特率
AUXR |= 0x01; // 串口1选择定时器2为波特率发生器
AUXR |= 0x04; // 定时器时钟1T模式
T2L = BR(115200);
T2H = BR(115200) >> 8;
AUXR |= 0x10; // 定时器2开始计时
ES = 1;
LineCoding.dwDTERate = REV4(115200);
LineCoding.bCharFormat = 0;
LineCoding.bParityType = 0;
LineCoding.bDataBits = 8;
}
void uart1_isr() interrupt 4
{
if (TI) // 检测串口1发送中断
{
TI = 0; // 清除串口1发送中断请求位
UartBusy = 0;
}
if (RI) // 检测串口1接收中断
{
RI = 0; // 清除串口1接收中断请求位
TxBuffer = S2BUF;
}
}
void uart_set_parity(BYTE parity)
{
switch (parity)
{
case NONE_PARITY:
SCON = 0x50;
break;
case ODD_PARITY:
case EVEN_PARITY:
case MARK_PARITY:
SCON = 0xda;
break;
case SPACE_PARITY:
SCON = 0xd2;
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)
return;
if (!UsbInBusy && (TxRptr != TxWptr))
{
IE2 &= ~0x80; // EUSB = 0;
UsbInBusy = 1;
usb_write_reg(INDEX, 1);
cnt = 0;
while (TxRptr != TxWptr)
{
usb_write_reg(FIFO1, TxBuffer);
cnt++;
if (cnt == EP1IN_SIZE)
break;
}
usb_write_reg(INCSR1, INIPRDY);
IE2 |= 0x80; // EUSB = 1;
}
if (!UartBusy && (RxRptr != RxWptr))
{
dat = RxBuffer;
UartBusy = 1;
switch (LineCoding.bParityType)
{
case NONE_PARITY:
case SPACE_PARITY:
SCON &= ~0x08; // STB8 = 0;
break;
case ODD_PARITY:
ACC = dat;
if (P)
SCON &= ~0x08; // STB8 = !P;
else
SCON |= 0x08;
break;
case EVEN_PARITY:
ACC = dat;
if (P)
SCON |= 0x08; // STB8 = P;
else
SCON &= ~0x08;
break;
case MARK_PARITY:
SCON |= 0x08; // STB8 = 1;
break;
}
SBUF = dat;
while (UartBusy)
;
}
if (UsbOutBusy)
{
IE2 &= ~0x80; // EUSB = 0;
if (RxWptr - RxRptr < 256 - EP1OUT_SIZE)
{
UsbOutBusy = 0;
usb_write_reg(INDEX, 1);
usb_write_reg(OUTCSR1, 0);
}
IE2 |= 0x80; // EUSB = 1;
}
}
现在的问题是这样的:从CDC发送出去的数据正常,但从另一个串口向USB-CDC发送数据,CDC接收不正常,只显示几个00,不知道需要再修改哪个地方。
哦,发现问题了,改好了改好了。
页:
[1]