本帖最后由 autopccopy 于 2011-5-29 12:28 编辑
今天在试验STC15F204EA的模拟串口问题,使用官方数据表(STC15F204EA 20110319版PDF, 第8.2节 P187)的C例程:《8.2 利用定时器1实现模拟串口的测试程序----定时器1工作在16位自动重�模式》,C例程能编译成功,可惜串口无任何反应(本来应该循环发送0x55,0xaa)。用示波器也检查不到电平的变化。请问是什么原因呢?(确认单片机最小系统是正常的,使用的是STC15F204EA 1046批次和STC15L204EA 1034批次产品试验,均是这样的结果)。
后来也使用来了8.1节的定时器0的模式,也是不成功! :(
附测试例程,大家有机会也试试:
//-------------------------------------------------------------
//演示STC15系列单片机利用定时器1实现模拟串口功能
//使用STC15F数据表110319版 8.2节例程(P187)
//调试版,未通过$$$
//-------------------------------------------------------------
//11.05.29
#include "reg51.h"
#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
sfr AUXR=0x8E;
sbit RXB=P3^0; //define UART TX/RX port
sbit TXB=P3^1;
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
void UART_SEND(BYTE dat);
BYTE t, r;
void main()
{
TMOD=0x00; //timer1 in 16-bit auto reload mode
AUXR=0x40; //timer1 working at 1T mode
TL1=BAUD;
TH1=BAUD>>8; //initial timer1 and set reload value
TR1=1; //tiemr1 start running
ET1=1; //enable timer1 interrupt
PT1=1; //improve timer1 interrupt priority
EA=1; //open global interrupt switch
//UART_INIT
TING=0;
RING=0;
TEND=1;
REND=0;
TCNT=0;
RCNT=0;
P0=0; //点亮指示LED
while(1)
{
UART_SEND(0x55);
UART_SEND(0xaa);
}
}
void UART_SEND(BYTE dat)
{
while(!TEND);
TEND=0;
TBUF=dat;
TING=1;
}
//-----------------------------------------
//Timer interrupt routine for UART
void tm1() interrupt 3 using 1
{
if(RING)
{
if(--RCNT==0)
{
RCNT=3; //reset send baudrate counter
if(--RBIT==0)
{
RBUF=RDAT; //save the data to RBUF
RING=0; //stop receive
REND=1; //set receive completed flag
}
else
{
RDAT >>= 1;
if(RXB) RDAT |= 0x80; //shift RX data to RX buffer
}
}
}
else if(!RXB)
{
RING=1; //set start receive flag
RCNT=4; //initial receive baudrate counter
RBIT=9; //initial receive bit number (8 data bits + 1 stop bit)
}
if(--TCNT==0)
{
TCNT=3; //reset send baudrate counter
if(TING) //judge whether sending
{
if(TBIT==0)
{
TXB=0; //send start bit
TDAT=TBUF; //load data from TBUF to TDAT
TBIT=9; //initial send bit number (8 data bits + 1 stop bit)
}
else
{
TDAT >>= 1; //shift data to CY
if(--TBIT==0)
{
TXB=1;
TING=0; //stop send
TEND=1; //set send completed flag
}
else
{
TXB=CY; //write CY to TX port
}
}
}
}
}
|