#include "reg52.h"
#define uchar unsigned char
unsigned char RDAT = 0;
unsigned char TDAT = 0;
//unsigned char chk_flag = 0;
unsigned char const code dianbiao[16]={0xfe,0x68,0x99,0x99,0x99,0x99,0x99,0x99,0x68,0x01,0x02,0x43,0xc3,0x6F,0x16,0x00};
sbit Key1 = P3^7;
sbit RE_DE = P3^2; //本例程采用方式二连接,即RE和DE相连接用一个IO口控制
void Delay(unsigned int k)
{
while(k--);
}
void Delay_ms(unsigned int
time)
{
unsigned char j;
for(;time>0;time--)
{
j = 150;
while(j--)
;
}
}
void Init(void)
{
TMOD|=0x20; //定时器1,工作方式2
TH1=0xe8; //波特率为1200
TL1=0xe8;
PCON=0x00;
SCON=0xd0; //工作方式1,8位UART,波特率可变,允许接收
TR1=1; //启动波特率发生
EA=1;
ES=1;
RE_DE=0; //关闭485模块发射使能,打开接收功能
}
void Send(unsigned char ldata)
{
ACC =ldata;
TB8=P;
SBUF=ldata; //写发送寄存器
while(!TI); //等待发送完成
TI=0; //对标志位清零
}
void SendString(uchar *s)
{
RE_DE=1; //打开485模块发射使能,关闭接收功能
while (*s)
{
Send(*s++);
}
RE_DE=0; //关闭485模块发射使能,打开接收功能
}
void KeyCount(void)
{
if(Key1 == 0)
{
Delay(120);
if(Key1 == 0)
{
SendString(dianbiao);
P0=0xfe;
Delay_ms(400) ;
}
while(Key1==0);
}
}
//主函数,C语言的入口函数:
void main(void)
{
Init();
while(1)
{ KeyCount();
}
}
void ser() interrupt 4
{ if(RI)
{
Delay_ms(1000) ;
RDAT=SBUF;
P0=RDAT;
RI=0;
}
}
一周热门 更多>