P3口为准双向口。可以字节访问,也可以位访问。 P3.0---RXD,串行输入口。 P3.1---TXD,串行输出口。 P3.2---INT0,外部中断0的请求。 P3.3---INT1,外部中断1的请求。
P3.4---T0,定时器/计数器0外部计数脉冲。 P3.5---T1,定时器/计数器,1外部计数脉冲。 P3.6---WR,外部数据存储器写选通。 P3.7---RD,外部数据存储器读选通。
RST(9引脚):复位输入。当输入连续两个机器周期以上高电平时为有效,用来完成单片机单片机的复位初始化操作。
ALE(30引脚):地址锁存控制信号(ALE)是访问外部程序存储器时,锁存低8位地址的输出脉冲。
XTAL1(19引脚):振荡器反相放大器和内部时钟发生电路的输入端。 XTAL2(18引脚):振荡器反相放大器的输入端。STC89C52引脚图如图3-2所示。
图3-3 STC89C52引脚图
四、流程图设计
4.1主程序流程图
4
图4-1 主程序流程图
4.2 从机程序流程图
图4-2 从机程序流程图
五、程序设计
#include \
static data bit Tran_mark;
5
static data uchar Reci_num; static data uchar char_num; static datauchar Ask; uchar data xbuf[10]; uchar data *Sram; uchar counter=0; ulong timer_tick,grand; uint ExtInt=0,ExInt1=0; uchar Fre_0,Fre_1,Fre_2;
uchar grand_0,grand_1,grand_2,grand_3; uchar Timer0OverFlowCnt,Timer0OverFlowCntBUF; uchar TL0BUF,TH0BUF; void Init_System() { IT1=1; EX1=1; EA=1; P14=0; }
void Uart_Init() {
TMOD=0x21; SCON=0x50; PCON=0x00; TL1=0xFB; TH1=0xFB; ES=1; TR1=1;
TL0=(TIMER0_COUNT & 0x00FF); TH0=(TIMER0_COUNT >> 8); //TR0=1; ET0=1; }
void main()
6
{ uchar i; Init_System(); Uart_Init(); while(1) { P10=0; P11=0; P00=1; Ask=0; Reci_num=0; char_num=0; Sram=xbuf;
memset(xbuf,0,10); while(Reci_num==0);
if(xbuf[2]==(0xff&(xbuf[0]+xbuf[1]))) {
if(xbuf[1]==0x01) { P10=1; P11=1; P00=0;
memset(xbuf,0,10); xbuf[0]=0xff; xbuf[1]=0x01;
xbuf[2]=0xff&timer_tick; xbuf[3]=0xff&(timer_tick>>8); xbuf[4]=0xff&(timer_tick>>16); xbuf[5]=0xff&grand; xbuf[6]=0xff&(grand>>8); xbuf[7]=0xff&(grand>>16); xbuf[8]=0xff&(grand>>24);
xbuf[9]=0xff&(xbuf[0]+xbuf[1]+xbuf[2]+xbuf[3]+xbuf[4]+xbuf[5]+xbuf[6]+xbuf[7]+xbuf[8]);
7
Sram=xbuf; for(i=0;i<10;i++) {
Tran_mark=1; SBUF=*Sram++; while(Tran_mark); } } } } }
static void Timer0_int(void) interrupt 1 {
TL0=(TIMER0_COUNT & 0x00FF); TH0=(TIMER0_COUNT >> 8); Timer0OverFlowCnt++; if(Timer0OverFlowCnt>=50) {
Timer0OverFlowCnt=0; ExtInt=0; } }
void Serial_int() interrupt 4 {
uchar tsbuf; ES=0; if(RI) {
tsbuf=SBUF; if(tsbuf==0xff) { Ask++; } if(Ask)
8