单片机:自己的4*4硬件连接和程序:(LCD)/************************************************文件名:1602.c***描述:本程序可以实现用***矩阵键盘输入1602显示的功能***创建者:wangkang2012年3月29日***********************************************/#includereg52.h#defineucharunsignedchar#defineuintunsignedintsbitLCD_RS=P2^0;sbitLCD_RW=P2^1;sbitLCD_EN=P2^2;ucharcodetable[]=wangkang;ucharcodetable1[]=15124325232;ucharnum,key,temp,dis_buf;uchara=0,b=0,flag=1;ucharLCD_busy();/*********************************************晶振电路复位电路*语法描述:voiddelay(uintz)*实现功能:延时*程序描述:延时大约1ms********************************************/voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}/**********************************************语法描述:voidwrite_com(ucharcom)*实现功能:写命令函数*程序描述:********************************************/voidwrite_com(ucharcom)//写命令{while(LCD_busy());LCD_RS=0;LCD_RW=0;P0=com;delay(5);LCD_EN=1;delay(5);LCD_EN=0;}/**********************************************语法描述:voidwrite_data(uchardate)*实现功能:写数据函数*程序描述:********************************************/voidwrite_data(uchardate)//写数据{while(LCD_busy());LCD_RS=1;LCD_RW=0;P0=date;delay(5);LCD_EN=1;delay(5);LCD_EN=0;}/**********************************************语法描述:ucharLCD_busy()*实现功能:检忙函数*程序描述:********************************************/ucharLCD_busy()//读状态,检忙{ucharbusyflag;LCD_RS=0;LCD_RW=1;LCD_EN=1;delay(5);busyflag=(P0&0x80);delay(3);LCD_EN=0;return(busyflag);}/**********************************************语法描述:voidinit()*实现功能:LCD_1602初始化*程序描述:设置1602的光标等********************************************/voidinit(){LCD_EN=0;write_com(0x38);//显示模式设置write_com(0x0c);//设置开显示write_com(0x06);//写一个字符后地址指针加1write_com(0x01);//清屏write_com(0x80+0x02);//指针指在01地址}/**********************************************语法描述:voidkeyscan(void)*实现功能:4*4矩形按键扫描*程序描述:********************************************/voidkeyscan(void){P1=0x0F;//低四位输入delay(1);temp=P1;//读P1口temp=temp&0x0F;//高4位置0,取出低4位temp=~(temp|0xF0);//低4位保持,高4位全置1,然后整个取反if(temp==1)key=0;elseif(temp==2)key=1;elseif(temp==4)key=2;elseif(temp==8)key=3;elsekey=16;P1=0xF0;//高四位输入delay(1);temp=P1;//读P1口temp=temp&0xF0;temp=~((temp4)|0xF0);if(temp==1)key=key+0;elseif(temp==2)key=key+4;elseif(temp==4)key=key+8;elseif(temp==8)key=key+12;elsekey=16;dis_buf=key;//键值入显示缓存dis_buf=dis_buf&0x0f;if(dis_buf9)//转换为ASCII码dis_buf=dis_buf+0x37;elsedis_buf=dis_buf+0x30;}/**********************************************语法描述:voidkeydown(void)*实现功能:判断按键是否按下*程序描述:有按键按下才进行按键扫描********************************************/voidkeydown(void){P1=0xf0;if(P1!=0xf0){delay(150);if(P1!=0xf0){keyscan();flag=0;}}}/**********************************************语法描述:voidkaiji(void)*实现功能:开机显示界面*程序描述:********************************************/voidkaiji(void)//开机界面{while(table[a]!='\0'){write_data(table[a++]);}write_com(0x80+0x42);while(table1[b]!='\0'){write_data(table1[b++]);}delay(2000);write_com(0x01);}/**********************************************语法描述:voidmain()*实现功能:主函数,调度整个系统功能*程序描述:********************************************/voidmain(){uintcounter=0;init();kaiji();//开机界面write_com(0x80+0x00);while(1){keydown();if(!flag){write_data(dis_buf);counter++;if(counter==17)//第一行显示完以后光标回到第一个位置{counter=0;write_com(0x01);write_data(dis_buf);}flag=1;}}老师的程序:4*4链接如下,程序如下:#includestdio.h#includemath.h#includereg51.h#includeabsacc.h#defineucharunsignedchar#defineuintunsignedint#defineCW_LCDXBYTE[0x8000]#defineCR_LCDXBYTE[0x8200]#defineDW_LCDXBYTE[0x8100]#defineDR_LCDXBYTE[0x8300]sbitP1_0=P1^0;sbitP1_1=P1^1;sbitP1_2=P1^2;sbitP1_3=P1^3;sbitP1_4=P1^4;sbitP1_5=P1^5;sbitP1_6=P1^6;sbitP1_7=P1^7;sbitLCD_WR=P3^6;sbitLCD_RS=P3^5;sbitLCD_EN=P3^4;sbitrs=P2^0;sbitrw=P2^1;sbitlcden=P2^2;ucharcodenum[]=YOUAREWELCOME!;ucharcodenum1[]=15144295190;ucharcodekey_num[]=0123456789ABCDEF;ucharkey_code=0;uintkey_index=0;ucharlcd_position=0;voiddelay(uintx){uchari;while(x--)for(i=0;i120;i++);}voidsend_com(ucharcom){rs=0;rw=0;lcden=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;delay(4);}voidsend_dat(uchardat){rs=1;rw=0;lcden=0;P0=dat;delay(5);lcden=1;delay(5);lcden=0;delay(4);}voidinit(){lcden=0;send_com(0x38);send_com(0x0C);send_com(0x06);send_com(0x01);}voidlcd_clear(void){lcden=0;send_com(0x01);}voiddelay_ms(uintx);ucharled[10]={0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};ucharkey_value1=0,key_value2=0;voiddisplay_led(void);ucharkey_table[16]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};voidkey_lookup(ucharkey_code);ucharkey_scan();voidmain(void){uchari;ucharflag_key=0;P1=0xf0;init();send_com(0x80+0);for(i=0;i16;i++){send_dat(num[i]);delay(5);}send_com(0x80+0x40+1);for(i=0;i16;i++){send_dat(num1[i]);delay(5);}for(i=0;i5;i++)delay(500);lcd_clear();while(1){display_led();key_code=key_scan();key_lookup(key_code);}}/*************判断是否有键按下*******************/ucharkey_scan(){ucharscode,recode;P1=0xf0;if((P1&0xf0)!=0xf0){delay_ms(5000);if((P1&0xf0)!=0xf0){scode=0xfe;while((scode&0x10)!=0){P1=scode;if((P1&0xf0)!=0xf0){recode=P1&0xf0;scode=scode&0x0f;lcd_position++;if(lcd_position15)lcd_position=0;while((P1&0xf0)!=0xf0);return