#include<reg52.h> sbit p30=P3^0; sbit p31=P3^1; sbit p32=P3^2; sbit p07=P0^7; const s=1; /***************** 延时函数 *****************/ delaynms(unsigned int m) { unsigned char i,j; for(;m!=0;m--) for(i=0;i<4;i++) for(j=0;j<32;j++) ; } /****************** 1602busy jump ******************/ busyjump() { bit result; p30=0; p31=1; p32=1; result=p07; p32=0; return result;
} /****************** 1602读指令 ******************/ readinstruction(unsigned char ins) { while(busyjump()) p30=0; p31=0; P0=ins; p32=1; p32=0; } /************** 1602读数据 **************/ readdata(unsigned char dat) { while(busyjump()); p30=1; p31=0; P0=dat; p32=1; p32=0; } /************* 1602初始化 *************/ chush() { delaynms(15);//模块热启动 readinstruction(0x36); readinstruction(0x36); readinstruction(0x0c);
readinstruction(0x01); readinstruction(0x06); } /**************** 显示地址转换 ***************/ showaddr(unsigned char addr) { readinstruction(addr|0x80); } /*************** 定位 ***************/ char dingwei (char n) { char i; n=~n; switch(n) { case 0xf0:i=-1; break; case 0xf1:i=0; break; case 0xf2:i=1; break; case 0xf4:i=2; break; case 0xf8:i=3; break; } return i; } /**************** 主函数 ***************/ main() { char h,l,j,k,flag; unsigned char temp; chush(); while(1) { temp=0xfe; flag=0; for(j=0;j<4;j++,temp=(temp<<1)+0x01) { P1=temp; h=P1/16; l=P1&0x0f; k=dingwei(l)*4+dingwei(h)+1; if(dingwei(h)+1) { delaynms(1); if(dingwei(h)+1)//去抖动 { readinstruction(0x01); showaddr(0x00); readdata(k/10+0x30); readdata(k%10+0x30); delaynms(2); delaynms(1500); flag=1; } } if(j==3&&flag==0) { readinstruction(0x01); showaddr(0x00); readdata('n');delaynms(2); readdata('o');delaynms(2); readdata(' ');delaynms(2); readdata('k');delaynms(2); readdata('e');delaynms(2); readdata('y');delaynms(2); readdata(' ');delaynms(2); readdata('p');delaynms(2); readdata('u');delaynms(2); readdata('t');delaynms(2); readdata(' ');delaynms(2); readdata('o');delaynms(2); readdata('n');delaynms(2); delaynms(150); } }
}
}
[此贴子已经被作者于2009-12-17 23:32:23编辑过]
|