当前位置:首页 > 《单片机的C语言程序设计与运用(第2版)》期末复习题及答案2
.
#include\ #include\sbit RS=P3^5; sbit RW=P3^6; sbit E=P3^7; #define busy 0x80
#define uchar unsigned char #define uint unsigned int
uchar a[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',}; void delay_LCM(uchar k)//延时函数 { uint i,j;
for(i=0;i for(j=0;j<60;j++) } } void test_1602busy()//测忙函数 { P1=0xff; E=1; RS=0; RW=1; _nop_(); _nop_(); while(P1&busy) //检测LCD DB7 是否为1 { E=0; _nop_(); E=1; _nop_(); } E=0; {;} 可编辑 . } void write_1602Command(uchar co) //写命令函数 { test_1602busy(); RS=0; RW=0; E=0; _nop_(); P1=co; _nop_(); E=1; _nop_(); E=0; } void write_1602Data(uchar Data)//写数据函数 { test_1602busy(); P1=Data; RS=1; RW=0; E=1; _nop_(); E=0; } void init_1602(void)//初始化函数 { write_1602Command(0x38); //LCD功能设定,DL=1(8位),N=1(2行显示) delay_LCM(5); write_1602Command(0x01); //清除LCD的屏幕 delay_LCM(5); write_1602Command(0x06); // LCD模式设定,I/D=1(计数地址加1) //LCD的使能端 高电平有效 //检测LCD是否忙 可编辑 . delay_LCM(5); write_1602Command(0x0F); //显示屏幕 delay_LCM(5); } void DisplayOneChar(uchar X,uchar Y,uchar DData) { Y&=1; X&=15; if(Y)X|=0x40; //若y为1(显示第二行),地址码+0X40 X|=0x80; //指令码为地址码+0X80 write_1602Command(X); write_1602Data(DData); } void display_1602(uchar *DData,X,Y)//显示函数 { uchar ListLength=0; Y&=0x01; X&=0x0f; while(X<16) { DisplayOneChar(X,Y,DData[ListLength]); ListLength++; X++; } } void delay(uint i) //延时程序 {uint j; for (j=0;j uchar checkkey()// 检测有没有键按下 {uchar i ; uchar j ; j=0x0f; 可编辑 . P2=j; i=P2; i=i&0x0f; if (i==0x0f) return (0); else return (0xff); } uchar keyscan()//键盘扫描程序 { uchar scancode; uchar codevalue; uchar a; uchar m=0; uchar k; uchar i,j; if (checkkey() ==0) return (0xff); else {delay(100); if (checkkey()==0) return (0xff); else { scancode=0xf7;m=0x00; for (i=1;i<=4;i++) { k=0x10; P2=scancode; a=P2; for (j=0;j<4;j++) //J为行数 { { while (checkkey()!=0); return (codevalue); if ((a&k)==0) codevalue = m+j; //键盘行扫描初值,M为列数 可编辑
共分享92篇相关文档