
idata
SystemBuf[10]
//用于接收
uchar
Rx=0
//
接收数据
条数
uchar
code
AT[]="YES,IT
IS"
void
UART_init
(void)
{
TMOD=0x20
/慎胡/用定时器1
PCON=0x00
//
波特率
不加倍
SCON=0x50
//串行方式1
TH1=0xFD
//9600
TL1=0xFD
//
EA=1
//
ES=1
//
TR1
=1
}
void
sendchar(uchar
ch)
//串口送一个字节
{
SBUF=ch
while(TI==0)
TI=0
}
void
sendstring(uchar
*p)
//送字符串
{
while(*p)
{
sendchar(*p)
p++
}
}
///////
void
receive(void)
interrupt
4
using
1
//中断
{
if(RI)
{
if(Rx<10)
//这儿最多收10个字节
{
SystemBuf[Rx]=SBUF
Rx++
}
RI=0
}
}
///
void
main()
{
uchar
i
UART_init()
while(1)
{
if(SystemBuf=='S')
{
sendstring(AT)
for(i=0i<10i++)
//接收升孝局清0
{
SystemBuf[i]=0
}
Rx=0
}
}
}
//////吵让////如有小错误,自己调下,手打的,大小写可能不正常,这是标准程序,直接用,
#include <AT89X51.H>//单片机51头文知物件,存放着单片机的寄存器unsigned char dat//用于存储单片机接收发送缓冲寄存器SBUF里面的内容
sbit gewei=P2^2//个位选通定义
sbit shiwei=P2^3//十位选通定义
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,0x77,0x7c,0x39,0x5e,0x79,0x71}
//{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,}// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
void Delay(unsigned int tc) //延时程序
{
while( tc != 0 )
{unsigned int i
for(i=0i<100i++)
tc--}
}
void LED() //LED显示接收到的数据
{
shiwei=0
P0=~table[dat/16]
Delay(8)
shiwei=1
gewei=0
P0=~table[dat%16]
Delay(5)
gewei=1
}
void Init_Com(void)//功能:串口初始化,波特率9600,方式1
{
TMOD = 0x20
PCON = 0x00
SCON = 0x50
TH1 = 0xFd
TL1 = 0xFd
TR1 = 1
}
void main()//主扰空程序功能:实现接收数据并把接收到的数据原样发送回去///////
{
Init_Com()//串口初始化
// P1=0xf0
while(1)
{
if ( RI ) //扫描判断缓猛瞎是否接收到数据,
{
dat = SBUF //接收数据SBUF赋与dat
RI=0 //RI清零。
SBUF = dat //在原样把数据发送回去
}
LED()//显示接收到的数据
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)