//本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
//若无特别说明,工作频率一般为11.0592MHz
#include "reg51.h"
//-----------------------------------------
//define baudrate const
//BAUD = 65536 - FOSC/3/BAUDRATE/M (1T:M=1; 12T:M=12)
//NOTE: (FOSC/3/BAUDRATE) must be greater then 98, (RECOMMEND GREATER THEN 110)
//#define BAUD 0xF400 // 1200bps @ 11.0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11.0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11.0592MHz
//#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
//#define BAUD 0xFF40 //19200bps @ 11.0592MHz
#define BAUD 0xFFA0 //38400bps @ 11.0592MHz
//#define BAUD 0xEC00 // 1200bps @ 18.432MHz
//#define BAUD 0xF600 // 2400bps @ 18.432MHz
//#define BAUD 0xFB00 // 4800bps @ 18.432MHz
//#define BAUD 0xFD80 // 9600bps @ 18.432MHz
//#define BAUD 0xFEC0 //19200bps @ 18.432MHz
//#define BAUD 0xFF60 //38400bps @ 18.432MHz
//#define BAUD 0xE800 // 1200bps @ 22.1184MHz
//#define BAUD 0xF400 // 2400bps @ 22.1184MHz
//#define BAUD 0xFA00 // 4800bps @ 22.1184MHz
//#define BAUD 0xFD00 // 9600bps @ 22.1184MHz
//#define BAUD 0xFE80 //19200bps @ 22.1184MHz
//#define BAUD 0xFF40 //38400bps @ 22.1184MHz
//#define BAUD 0xFF80 //57600bps @ 22.1184MHz
#define ID_ADDR_RAM 0x71 //STC15F104W系列ID号的存放在RAM区的地址
//#define ID_ADDR_RAM 0x71 //STC15F104W系列ID号的存放在RAM区的地址
//#define ID_ADDR_RAM 0xF1 //STC15F204EA系列ID号的存放在RAM区的地址
#define ID_ADDR_ROM 0x0ff9 //STC104W系列ID号的存放在ROM区的地址
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
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_INIT();
void UART_SEND(BYTE dat);
BYTE t, r;
BYTE buf[16];
void main()
{
// BYTE idata *iptr;
BYTE code *cptr;
BYTE i;
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
P7M0 = 0x00;
P7M1 = 0x00;
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch
UART_INIT();
// iptr = ID_ADDR_RAM; //从RAM区读取ID号
// for (i=0; i<7; i++) //读7个字节
// {
// UART_SEND(*iptr++); //发送ID到串口
// }
cptr = ID_ADDR_ROM; //从程序区读取ID号
for (i=0; i<7; i++) //读7个字节
{
UART_SEND(*cptr++); //发送ID到串口
}
while (1);
}
//-----------------------------------------
//Timer interrupt routine for UART
void tm0() interrupt 1 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
}
}
}
}
}
//-----------------------------------------
//initial UART module variable
void UART_INIT()
{
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}
//-----------------------------------------
//Send UART data
void UART_SEND(BYTE dat)
{
while (!TEND);
TEND = 0;
TBUF = dat;
TING = 1;
}
|