15W408AS的串口通信有什么不一样吗?
请问:15W408AS的串口通信有什么不一样吗?8H4K64上的串口通信程序移过来,没有数据发送,更换发送引脚也不行,大家手上有没有15W408AS的串口通信程序,麻烦发一份,谢谢STC15W408AS, 无T1, 有T0/T2, 用 T2 做波特率发生器
用例程也不行:
/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* --- QQ:800003751 ----------------------------------------------*/
/* If you want to use the program or the program referenced in the*/
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
/************* 功能说明 **************
请先别修改程序, 直接下载"UART1.hex"测试, 下载时选择主频22.1184MHZ.
串口1全双工中断方式收发通讯程序.
串口设置为:115200,8,n,1.P3.0--RXD1, P3.1--TXD1.
MCU上电后先发送一串字符串.
上位机通过串口向MCU发送数据, MCU收到后通过串口把收到的数据原样返回.
******************************************/
#define MAIN_Fosc 22118400L //定义主时钟
#include "..\..\STC15Fxxxx.H"
/************* 本地常量声明 **************/
#define RX1_Lenth 32 //串口接收缓冲长度
/************* 本地变量声明 **************/
u8 idata RX1_Buffer; //接收缓冲
u8 TX1_Cnt; //发送计数
u8 RX1_Cnt; //接收计数
bit B_TX1_Busy; //发送忙标志
/************* 本地函数声明 **************/
void UART1_config(u32 brt, u8 timer, u8 io); // brt: 通信波特率,timer=2: 使用定时器2做波特率发生器, 其余值: 使用定时器1做波特率发生器
// io=0: 串口切换到P3.0 P3.1, 1: 串口切换到P3.6 P3.7,2: 串口切换到P1.6 P1.7
void UART1_PrintString(u8 *pt);
//========================================================================
// 函数: void main(void)
// 描述: 主函数
// 参数: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void main(void)
{
EA = 1; //允许全局中断
UART1_config(115200L, 2, 0); // brt: 通信波特率,timer=2: 使用定时器2做波特率发生器, 其余值: 使用定时器1做波特率发生器
// io=0: 串口切换到P3.0 P3.1, 1: 串口切换到P3.6 P3.7,2: 串口切换到P1.6 P1.7
UART1_PrintString("STC15xxxx UART1 测试程序!\r\n"); //UART1发送一个字符串
while (1)
{
if((TX1_Cnt != RX1_Cnt) && !B_TX1_Busy) //收到过数据, 并且发送空闲
{
B_TX1_Busy = 1; //标志发送忙
SBUF = RX1_Buffer; //发一个字节
if(++TX1_Cnt >= RX1_Lenth) TX1_Cnt = 0; //避免溢出处理
}
}
}
//================== 发送一个字符串函数 ========================
void UART1_PrintString(u8 *pt)
{
for( ; *pt!=0; pt++)
{
B_TX1_Busy = 1; // 标志发送忙
SBUF = *pt; // 发一个字节
while(B_TX1_Busy);
}
}
//========================================================================
// 函数: SetTimer2Baudraye(u16 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void SetTimer2Baudraye(u16 dat) //使用Timer2做波特率.
{
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |=(1<<2); //Timer2 set as 1T mode
TH2 = (u8)(dat >> 8);
TL2 = (u8)dat;
IE2&= ~(1<<2); //禁止中断
AUXR |=(1<<4); //Timer run enable
}
//========================================================================
// 函数: void UART1_config(u32 brt, u8 timer, u8 io)
// 描述: UART1初始化函数。
// 参数: brt: 通信波特率.
// timer: 波特率使用的定时器, timer=2: 波特率使用定时器2, 其它值: 使用Timer1做波特率.
// io: 串口1切换到的IO,io=1: 串口1切换到P3.0 P3.1,=1: 切换到P3.6 P3.7,=2: 切换到P1.6 P1.7.
// 返回: none.
// 版本: VER1.0
// 日期: 2018-4-2
// 备注:
//========================================================================
void UART1_config(u32 brt, u8 timer, u8 io) // brt: 通信波特率,timer=2: 使用定时器2做波特率发生器, 其余值: 使用定时器1做波特率发生器
// io=0: 串口切换到P3.0 P3.1, 1: 串口切换到P3.6 P3.7,2: 串口切换到P1.6 P1.7
{
brt = 65536UL - (MAIN_Fosc / 4) / brt;
if(timer == 2) //波特率使用定时器2
{
AUXR |= 0x01; //S1 BRT Use Timer2;
SetTimer2Baudraye((u16)brt);
}
else //波特率使用定时器1
{
TR1 = 0;
AUXR &= ~0x01; //S1 BRT Use Timer1;
AUXR |=(1<<6); //Timer1 set as 1T mode
TMOD &= ~(1<<6); //Timer1 set As Timer
TMOD &= ~0x30; //Timer1_16bitAutoReload;
TH1 = (u8)(brt >> 8);
TL1 = (u8)brt;
ET1 = 0; // 禁止Timer1中断
INT_CLKO &= ~0x02; // Timer1不输出高速时钟
TR1= 1; // 运行Timer1
}
S1_8bit(); //头文件的宏定义,8位数据, 1位起始位, 1位停止位, 无校验
if(io == 1) { S1_USE_P36P37(); P3n_standard(0xc0); } //UART1 使用P36 P37口
else if(io == 2) { S1_USE_P16P17(); P1n_standard(0xc0); } //UART1 使用P16 P17口
else { S1_USE_P30P31(); P3n_standard(0x03); } //UART1 使用P30 P31口
REN = 1; //允许接收
ES= 1; //允许中断
// PS= 1; //高优先级中断
}
/********************* UART1中断函数************************/
void UART1_int (void) interrupt UART1_VECTOR
{
if(RI)
{
RI = 0;
RX1_Buffer = SBUF; //保存一个字节
if(++RX1_Cnt >= RX1_Lenth) RX1_Cnt = 0; //避免溢出处理
}
if(TI)
{
TI = 0;
B_TX1_Busy = 0; //清除发送忙标志
}
}
神农鼎 发表于 2023-10-3 16:51
STC15W408AS, 无T1, 有T0/T2, 用 T2 做波特率发生器
谢谢你,还有一个问题请教,用了408AS后,循环语句for,dowhile都不终止,但编译能通过,不知道是什么原因 你换能仿真的 STC8G系列,用仿真来发现你的错
如何仿真演示视频,STC单片机硬件仿真演示视频 合集 - 仿真/ISP下载/做自己的ISP/编译器/头文件 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
页:
[1]