单片机 串口通信实验
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
0x11,0x21,0x41,0x81,0x12,0x22,0x42,0x82,
0x14,0x24,0x44,0x84,0x18,0x28,0x48,0x88};
/**********************************************************
初始化
**********************************************************/
#include <reg51.h>
unsigned char shuju;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA 来自百度文库 1;
ES = 1;
TR1 = 1;
}
void SendOneByte(unsigned char c)
while(1);
}
#include <reg51.h>
unsigned char code disp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char code key_code[]={
{SBUF = c;
while(!TI);
TI = 0;
}
/**********************************************************
串口接收
**********************************************************/
void UARTInterrupt(void) interrupt 4
{unsigned char i;
if(RI)
{RI = 0;
i=SBUF;
P0=disp_code[i];
}
}
/**********************************************************
void InitUART(void)
{TMOD = 0x20;SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
/**********************************************************
if((P1&0xf0)!=0xf0)
{
sccode=0xfe;
while((sccode&0x10)!=0)
{P1=sccode;
if((P1&0xf0)!=0xf0)
{recode=(P1&0Xf0)|0x0f;
return((~sccode)+(~recode));
}
else sccode=(sccode<<1)|0x01;
{key=keyscan();
for(i=0;i<16;i++)
{if(key==key_code[i])
{P0=disp_code[i];
SendOneByte(i);
}
}
delayms(300);
}
}
}
{
SBUF = c;
while(!TI);
TI= 0;
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
shuju=SBUF;
shuju++;
SendOneByte(shuju);
}
else
TI = 0;
}
void main(void)
{
InitUART();
}
}
}
return (0);
}
/**********************************************************
串口发送
**********************************************************/
void SendOneByte(unsigned char c)
延时函数
**********************************************************/
void delayms(unsigned int ms)
{unsigned char t;
while(ms--)
{for(t = 0; t < 100; t++);
}
}
/**********************************************************
键盘扫描子函数
**********************************************************/
unsigned char keyscan()
{unsigned char sccode,recode;
P1=0xF0;
if((P1&0XF0)!=0XF0)
{delayms(30);
主函数
**********************************************************/
void main(void)
{unsigned char i,key;
InitUART();P0 = 0x00;P2=0Xfe;
while(1)
{if(keyscan()!=0)
0x14,0x24,0x44,0x84,0x18,0x28,0x48,0x88};
/**********************************************************
初始化
**********************************************************/
#include <reg51.h>
unsigned char shuju;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA 来自百度文库 1;
ES = 1;
TR1 = 1;
}
void SendOneByte(unsigned char c)
while(1);
}
#include <reg51.h>
unsigned char code disp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char code key_code[]={
{SBUF = c;
while(!TI);
TI = 0;
}
/**********************************************************
串口接收
**********************************************************/
void UARTInterrupt(void) interrupt 4
{unsigned char i;
if(RI)
{RI = 0;
i=SBUF;
P0=disp_code[i];
}
}
/**********************************************************
void InitUART(void)
{TMOD = 0x20;SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
/**********************************************************
if((P1&0xf0)!=0xf0)
{
sccode=0xfe;
while((sccode&0x10)!=0)
{P1=sccode;
if((P1&0xf0)!=0xf0)
{recode=(P1&0Xf0)|0x0f;
return((~sccode)+(~recode));
}
else sccode=(sccode<<1)|0x01;
{key=keyscan();
for(i=0;i<16;i++)
{if(key==key_code[i])
{P0=disp_code[i];
SendOneByte(i);
}
}
delayms(300);
}
}
}
{
SBUF = c;
while(!TI);
TI= 0;
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
shuju=SBUF;
shuju++;
SendOneByte(shuju);
}
else
TI = 0;
}
void main(void)
{
InitUART();
}
}
}
return (0);
}
/**********************************************************
串口发送
**********************************************************/
void SendOneByte(unsigned char c)
延时函数
**********************************************************/
void delayms(unsigned int ms)
{unsigned char t;
while(ms--)
{for(t = 0; t < 100; t++);
}
}
/**********************************************************
键盘扫描子函数
**********************************************************/
unsigned char keyscan()
{unsigned char sccode,recode;
P1=0xF0;
if((P1&0XF0)!=0XF0)
{delayms(30);
主函数
**********************************************************/
void main(void)
{unsigned char i,key;
InitUART();P0 = 0x00;P2=0Xfe;
while(1)
{if(keyscan()!=0)