STC32G12K128串口3 不进入发送中断,求教各位如何解决
标准例程中的串口3 中断收发 也不好使 求教各位大神如何解决#include "..\..\comm\STC32G.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 Baudrate3 (65536 - MAIN_Fosc / 115200 / 4)
#define UART3_BUF_LENGTH 64
//==========================================================================
/*************本地常量声明 **************/
/*************IO口定义 **************/
/*************本地变量声明 **************/
u8TX3_Cnt; //发送计数
u8RX3_Cnt; //接收计数
bit B_TX3_Busy; //发送忙标志
u8RX3_Buffer; //接收缓冲
/*************本地函数声明 **************/
void UART3_config(u8 brt); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
void PrintString3(u8 *puts);
/****************外部函数声明和外部变量声明 *****************/
/******************** 主函数 **************************/
void main(void)
{
WTST = 0;//设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
EAXFR = 1; //扩展寄存器(XFR)访问使能
CKCON = 0; //提高访问XRAM速度
P0M1 = 0x30; P0M0 = 0x30; //设置P0.4、P0.5为漏极开路(实验箱加了上拉电阻到3.3V)
P1M1 = 0x30; P1M0 = 0x30; //设置P1.4、P1.5为漏极开路(实验箱加了上拉电阻到3.3V)
P2M1 = 0x3c; P2M0 = 0x3c; //设置P2.2~P2.5为漏极开路(实验箱加了上拉电阻到3.3V)
P3M1 = 0x50; P3M0 = 0x50; //设置P3.4、P3.6为漏极开路(实验箱加了上拉电阻到3.3V)
P4M1 = 0x3c; P4M0 = 0x3c; //设置P4.2~P4.5为漏极开路(实验箱加了上拉电阻到3.3V)
P5M1 = 0x0c; P5M0 = 0x0c; //设置P5.2、P5.3为漏极开路(实验箱加了上拉电阻到3.3V)
P6M1 = 0xff; P6M0 = 0xff; //设置为漏极开路(实验箱加了上拉电阻到3.3V)
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
UART3_config(0); //选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
EA = 1; //允许全局中断
PrintString3("STC32G UART3 Test Programme!\r\n");//UART3发送一个字符串
while (1)
{
if((TX3_Cnt != RX3_Cnt) && (!B_TX3_Busy)) //收到数据, 发送空闲
{
S3BUF = RX3_Buffer;
B_TX3_Busy = 1;
if(++TX3_Cnt >= UART3_BUF_LENGTH) TX3_Cnt = 0;
}
}
}
//========================================================================
// 函数: void PrintString3(u8 *puts)
// 描述: 串口3发送字符串函数。
// 参数: puts:字符串指针.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void PrintString3(u8 *puts)
{
for (; *puts != 0;puts++) //遇到停止符0结束
{
S3BUF = *puts;
B_TX3_Busy = 1;
while(B_TX3_Busy);
}
}
//========================================================================
// 函数: SetTimer2Baudraye(u32 dat)
// 描述: 设置Timer2做波特率发生器。
// 参数: dat: Timer2的重装值.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void SetTimer2Baudraye(u32 dat)// 使用Timer2做波特率.
{
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 UART3_config(u8 brt)
// 描述: UART3初始化函数。
// 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART3_config(u8 brt) // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
{
if(brt == 2)
{
SetTimer2Baudraye(Baudrate3);
S3CON = 0x10; //8位数据, 使用Timer2做波特率发生器, 允许接收
}
else
{
T3R = 0; //Timer stop
S3CON = 0x50; //8位数据, 使用Timer3做波特率发生器, 允许接收
T3H = (u8)(Baudrate3 / 256);
T3L = (u8)(Baudrate3 % 256);
T3_CT = 0; //Timer3 set As Timer
T3x12 = 1; //Timer3 set as 1T mode
T3R = 1; //Timer run enable
}
ES3= 1; //允许UART3中断
S3_S = 1; //UART3 switch bit1 to: 0: P0.0 P0.1,1: P5.0 P5.1
B_TX3_Busy = 0;
TX3_Cnt = 0;
RX3_Cnt = 0;
}
//========================================================================
// 函数: void UART3_int (void) interrupt UART3_VECTOR
// 描述: UART3中断函数。
// 参数: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 备注:
//========================================================================
void UART3_int (void) interrupt 17
{
if(S3RI)
{
S3RI = 0; //Clear Rx flag
RX3_Buffer = S3BUF;
if(++RX3_Cnt >= UART3_BUF_LENGTH) RX3_Cnt = 0;
}
if(S3TI)
{
S3TI = 0; //Clear Tx flag
B_TX3_Busy = 0;
}
}
一直卡在 发送忙标志位 ,发送中断不进入 发送忙标志位不能清0 函数库的竟然可以用?
都打开看下,还有DMA 支持的
找到问题了例程中少了中断 EA=1 加上就可以了 你这个程序里主函数不是有EA=1;允许全局中断的吗?发送字符串上面那一行,,,我也遇到了个问题,用官方例程uart3不能发
页:
[1]