- 打卡等级:以坛为家II
- 打卡总天数:524
- 最近打卡:2025-05-04 02:13:29
论坛元老
- 积分
- 5133
|
发表于 2024-9-15 13:39:46
|
显示全部楼层
/************* 功能说明 **************
本例程基于AI8051U为主控芯片的实验箱进行编写测试。
使用Keil C251编译器,Memory Model推荐设置XSmall模式,默认定义变量在edata,单时钟存取访问速度快。
edata建议保留1K给堆栈使用,空间不够时可将大数组、不常用变量加xdata关键字定义到xdata空间。
串口2全双工中断方式收发通讯程序。
通过PC向MCU发送数据, MCU收到后通过串口2把收到的数据原样返回.
用定时器做波特率发生器,建议使用1T模式(除非低波特率用12T),并选择可被波特率整除的时钟频率,以提高精度。
下载时, 选择时钟 22.1184MHz (用户可自行修改频率).
******************************************/
#include "..\..\comm\AI8051U.h"
#include "stdio.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
#define MAIN_Fosc 22118400L //定义主时钟(精确计算115200波特率)
//==========================================================================
#define Baudrate2 (65536 - MAIN_Fosc / 115200 / 4)
#define UART2_BUF_LENGTH 128
//==========================================================================
/************* 本地常量声明 **************/
/************* IO口定义 **************/
/************* 本地变量声明 **************/
u8 TX2_Cnt; //发送计数
u8 RX2_Cnt; //接收计数
bit B_TX2_Busy; //发送忙标志
u8 RX2_Buffer[UART2_BUF_LENGTH]; //接收缓冲
/************* 本地函数声明 **************/
void UART2_config(u8 brt); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
void PrintString2(u8 *puts);
/**************** 外部函数声明和外部变量声明 *****************/
/******************** 主函数 **************************/
void main(void)
{
WTST = 0; //设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
EAXFR = 1; //扩展寄存器(XFR)访问使能
CKCON = 0; //提高访问XRAM速度
P0M1 = 0x00; P0M0 = 0x00; //设置为准双向口
P1M1 = 0x00; P1M0 = 0x00; //设置为准双向口
P2M1 = 0x00; P2M0 = 0x00; //设置为准双向口
P3M1 = 0x00; P3M0 = 0x00; //设置为准双向口
P4M1 = 0x00; P4M0 = 0x00; //设置为准双向口
P5M1 = 0x00; P5M0 = 0x00; //设置为准双向口
P6M1 = 0x00; P6M0 = 0x00; //设置为准双向口
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
UART2_config(2); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
EA = 1; //允许全局中断
PrintString2("AI8051U UART2 Test Programme!\r\n"); //UART2发送一个字符串
while (1)
{
if((TX2_Cnt != RX2_Cnt) && (!B_TX2_Busy)) //收到数据, 发送空闲
{
S2BUF = RX2_Buffer[TX2_Cnt];
B_TX2_Busy = 1;
if(++TX2_Cnt >= UART2_BUF_LENGTH) TX2_Cnt = 0;
}
}
}
//========================================================================
// 函数: void PrintString2(u8 *puts)
// 描述: 串口2发送字符串函数。
// 参数: puts: 字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void PrintString2(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0结束
{
S2BUF = *puts;
B_TX2_Busy = 1;
while(B_TX2_Busy);
}
}
//========================================================================
// 函数: SetTimer2Baudraye(u32 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void SetTimer2Baudraye(u32 dat)
{
T2R = 0; //Timer stop
T2_CT = 0; //Timer2 set As Timer
T2x12 = 1; //Timer2 set as 1T mode
T2H = (u8)(dat / 256);
T2L = (u8)(dat % 256);
ET2 = 0; //禁止中断
T2R = 1; //Timer run enable
}
//========================================================================
// 函数: void UART2_config(u8 brt)
// 描述: UART2初始化函数。
// 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART2_config(u8 brt) // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
{
if(brt == 2)
{
SetTimer2Baudraye(Baudrate2);
S2CFG |= 0x01; //使用串口2时,W1位必需设置为1,否则可能会产生不可预期的错误
S2CON = (S2CON & 0x3f) | 0x40; //UART2模式, 0x00: 同步移位输出, 0x40: 8位数据,可变波特率, 0x80: 9位数据,固定波特率, 0xc0: 9位数据,可变波特率
ES2 = 1; //允许中断
S2REN = 1; //允许接收
S2_S = 1; //UART2 switch to: 0: P1.2 P1.3, 1: P4.2 P4.3
B_TX2_Busy = 0;
TX2_Cnt = 0;
RX2_Cnt = 0;
}
}
//========================================================================
// 函数: void UART2_int (void) interrupt UART2_VECTOR
// 描述: UART2中断函数。
// 参数: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART2_int (void) interrupt 8
{
if(S2RI)
{
S2RI = 0; //Clear Rx flag
RX2_Buffer[RX2_Cnt] = S2BUF;
if(++RX2_Cnt >= UART2_BUF_LENGTH) RX2_Cnt = 0;
}
if(S2TI)
{
S2TI = 0; //Clear Tx flag
B_TX2_Busy = 0;
}
}
|
|