1、一、要求:液晶显示器第一行显示“Hello World!”;第二行显示键盘的键值。二、程序代码:#include #include #define uchar unsigned char#define uint unsigned intunsigned char code dis1=“Hello World!“;uchar key_val=0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G;sbit U3_DS=P15;sbit U3_STCP=P14;sbit U3_SHCP=P13;sbit U4_DS=P12;sbit U4_STCP=P11;sbit U4_SHCP=P
2、10;void delay(unsigned int n);/74HC595void U3_595(unsigned char num)unsigned char count1;for (count1=0;count10;n-)for(i=0;i125;i+)_nop_();/写指令到 LCDvoid wcmd(unsigned char cmd)U4_595(0x00);U3_595(cmd);U4_595(0x40);U4_595(0x00);/写要显示的数据到 LCDvoid wdat(unsigned char dat)U4_595(0x10);U3_595(dat);U4_595(0
3、x50);U4_595(0x10);/初始化 LCD 子程序void init()wcmd(0x38);/设置 8 位总线双行显示, 5*7 点阵delay(20);wcmd(0x0C);/开显示,开光标,不闪烁delay(20);wcmd(0x06);/读写字符时地址加 1delay(20);wcmd(0x01);/清屏delay(20);/键盘扫描子程序void keyscan(void)unsigned char n;/扫描第一行P0=0xEF;n=P0;nif(n!=0x0F)delay(1);/去抖动P0=0xEF;n=P0;nif(n!=0x0F)switch(n)case (0x
4、0E):wdat(key_val0x01);break;case (0x0D):wdat(key_val0x02);break;case (0x0B): wdat(key_val0x03);break;case (0x07):wdat(key_val0x04);break;P0=0xDF;/扫描第二行n=P0;nif(n!=0x0F)delay(1);P0=0xDF;n=P0;nif(n!=0x0F)switch(n)case(0x0E):wdat(key_val0x05);break;case(0x0D):wdat(key_val0x06);break;case(0x0B):wdat(key
5、_val0x07);break;case(0x07):wdat(key_val0x08);break;P0=0xBF;/扫描第三行n=P0;nif(n!=0x0F)delay(1);P0=0xBF;n=P0;nif(n!=0x0F)switch(n)case(0x0E):wdat(key_val0x09);break;case(0x0D):wdat(key_val0x0A);break;case(0x0B):wdat(key_val0x0B);break;case(0x07):wdat(key_val0x0C);break;P0=0x7F;/扫描第四行n=P0;nif(n!=0x0F)dela
6、y(1);P0=0x7F;n=P0;nif(n!=0x0F)switch(n)case(0x0E):wdat(key_val0x0D);break;case(0x0D):wdat(key_val0x0E);break;case(0x0B):wdat(key_val0x0F);break;case(0x07):wdat(key_val0x10);break;void main(void)unsigned char i;init();wcmd(0x82);for (i=0;i12;i+)wdat(dis1i);while(1)delay(15);wcmd(0x38);/设置 8 位总线双行显示,5*7 点阵delay(20);wcmd(0x0C);/开显示,开光标,不闪烁delay(20);wcmd(0x06);/读写字符时地址加 1delay(20);wcmd(0xC2);keyscan();/*-http:/