本程序要通过232接收数据通过1602显示,数据的内容是一个键盘上每一个按键对应的键值,键值是8位的16进制数例如''FC 11 FF FD''。
#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
unsigned int Count;
char ch[4];
char g1,d1,g2,d2,g3,d3,g4,d4,x,y,z,w;
//bit read_flag=0;
sbit lcden= P2^5;
sbit lcdrw= P2^6;
sbit lcdrs= P2^7;
void delay1ms()//延时1ms
{
uchar i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++);
}
void delayms(uchar n)//延时nms
{
uchar i;
for(i=0;i<n;i++)
delay1ms();
}
void write_com(uchar com)//写指令
{
lcdrs=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_data(uchar date)//写数据
{
lcdrs=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void init()
{
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0xc0);
}
void LCD() //LED显示接收到的数据
{ while(1)
{ write_com(0x81); //从第一行第2位开始显示
g1=ch[0]/0x16;
if(g1<0x09)
{write_data(0x30+g1);}
else
{write_data(0x41+g1);}
delayms(5);
write_com(0x82); //从第一行第3位显示
d1=ch[0]%0x16;;
if(d1<0x09)
{write_data(0x30+d1);}
else
{write_data(0x41+d1);}
delayms(5);
write_com(0x83); //LCD1602从第一行第4位开始显示
g2=ch[0]/0x16;
if(g2<0x09)
{write_data(0x30+g2);}
else
{write_data(0x41+g2);}
delayms(5);
write_com(0x84); //LCD1602从第一行第5位开始显示
d2=ch[0]%0x16;
if(d2<0x09)
{write_data(0x30+d2);}
else
{write_data(0x41+d2);}
delayms(5);
write_com(0x85); //LCD1602从第一行第6位开始显示
g3=ch[0]/0x16;
if(g3<0x09)
{write_data(0x30+g3);}
else
{ write_data(0x41+g3);}
delayms(5);
write_com(0x86); //LCD1602从第一行第7位开始显示
d3=ch[0]%0x16;
if(d3<0x09)
{write_data(0x30+d3);}
else
{ write_data(0x41+d3);}
delayms(5);
write_com(0x87); //LCD1602从第一行第8位开始显示
g4=ch[0]/0x16;
if(g4<0x09)
{write_data(0x30+g4);}
else
{ write_data(0x41+g4);}
delayms(5);
write_com(0x88); //LCD1602从第一行第9位开始显示
d4=ch[0]%0x16;
if(d4<0x09)
{write_data(0x30+d4);}
else
{write_data(0x41+d4);}
delayms(5);
}
}
/*void send_char_com(unsigned char dh)//发送一个字节
{
SBUF=dh;
while(TI!=1);
TI=0;
RI=0;
}
void send_string_com(unsigned char*str,unsigned int strlen)//发送字符串
{
unsigned int k=0;
do
{
send_char_com(*(str+k));
k++;
}
while(k<=strlen);
}*/
void init_serial()
{
SCON=0x50; //串行工作方式1,8位异步通信方式
TMOD=0x20; //定时器1,方式2,8位自动重装
PCON=0x00; //SMOD=0,表示数据传输率加倍
TH1=0xFA;
TL1=0xFA;
TR1=1; //启动定时器1 //数据传输率:4800 fosc=11.0592MHz
IE=0x90; //允许串行中断
EA=1; //开中断
ES=1;
}
void serial()interrupt 4 using 3
{
unsigned int Count;
RI=0;
ch[Count]=SBUF;
delayms(5);
LCD();
//send_char_com(ch);
// ch[Count]=SBUF;
Count++;
if(Count==3)
{
Count=0;
//read_flag=1;
}
//send_string_com(ch,4);
//read_flag=1;
//delayms(5);
}
void main()//主程序功能:实现接收数据并把接收到的数据原样显示
{
init();
init_serial();
while(1)
{
//if (read_flag)
//{
//if((ch[0]==0XFC)&&(ch[3]==0XFD))
//LCD(); //显示接收到的数据
//send_char_com(ch);
//delayms(10);
//}
}
} |