lchjczwlchjczw.blog.chinaunix.net
lchjczw
全部博文(1008)
sysfs(0)
procfs(6)
Java(0)
Linux C(1)
C++(0)
C语言(0)
wifi驱动开发(0)
视频驱动开发(0)
音频驱动开发(2)
网络设备驱动开发(1)
驱动调试(0)
驱动基础(4)
sd卡驱动开发(1)
总线驱动(9)
flash驱动开发(0)
USB驱动开发(1)
2012年(1008)
Lanny_li
zsy77
cynthia
Bsolar
浪花小雨
fengchar
yxl15098
wangfeng
CHLRX
728196
Katherin
分类:
2012-08-01 11:31:18
原文地址:LCD+矩阵按键值 作者:luozhiyong131
#include <reg51.h>#include <intrins.h> #define uchar unsigned char#define uint unsigned intuchar dis_buf; //显示缓存uchar temp;uchar key; //键顺序吗void delay0(uchar x); //x*0.14MSsbit LCD_RW = P2^5;sbit LCD_RS = P2^6; sbit LCD_EN = P2^7;uchar cdis1[16] = {" KEY NUMBER "};uchar cdis2[16] = {" KEY-CODE: H "};#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};void delay(uchar x){ uchar j; while((x--)!=0) { for(j=0;j<125;j++) {;} }}bit lcd_busy() { bit result; LCD_RS = 0; LCD_RW = 1; LCD_EN = 1; delayNOP(); result = (bit)(P0&0x80); LCD_EN = 0; return(result); }void lcd_wcmd(uchar cmd){ while(lcd_busy()); LCD_RS = 0; LCD_RW = 0; LCD_EN = 0; _nop_(); _nop_(); P0 = cmd; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0; }void lcd_wdat(uchar dat){ while(lcd_busy()); LCD_RS = 1; LCD_RW = 0; LCD_EN = 0; P0 = dat; delayNOP(); LCD_EN = 1; delayNOP(); LCD_EN = 0;}void lcd_init(){ delay(15); lcd_wcmd(0x38); //16*2显示,5*7点阵,8位数据 delay(5); lcd_wcmd(0x38); delay(5); lcd_wcmd(0x38); delay(5); lcd_wcmd(0x0c); //显示开,关光标 delay(5); lcd_wcmd(0x06); //移动光标 delay(5); lcd_wcmd(0x01); //清除LCD的显示内容 delay(5);}void lcd_pos(uchar pos){ lcd_wcmd(pos | 0x80); //数据指针=80+地址变量} void keyscan(void) { P1=0x0F; //低四位输入 delay(1); temp=P1; //读P1口 temp=temp&0x0F; temp=~(temp|0xF0); if(temp==1) key=0; else if(temp==2) key=1; else if(temp==4) key=2; else if(temp==8) key=3; else key=16; P1=0xF0; //高四位输入 delay(1); temp=P1; //读P1口 temp=temp&0xF0; temp=~((temp>>4)|0xF0); if(temp==1) key=key+0; else if(temp==2) key=key+4; else if(temp==4) key=key+8; else if(temp==8) key=key+12; else key=16; dis_buf = key; //键值入显示缓存 dis_buf = dis_buf & 0x0f; if(dis_buf>9) //转换为ASCII码 dis_buf = dis_buf+0x37; else dis_buf = dis_buf+0x30; } void keydown(void) { P1=0xF0; if(P1!=0xF0) { keyscan(); } }main() { uchar m; P0=0xFF; //置P0口 P1=0xFF; //置P1口 delay(10); //延时 lcd_init(); //初始化LCD lcd_pos(0); //设置显示位置为第一行的第1个字符 m = 0; while(cdis1[m] != '\0') { //显示字符 lcd_wdat(cdis1[m]); m++; } lcd_pos(0x40); //设置显示位置为第二行第1个字符 m = 0; while(cdis2[m] != '\0') { lcd_wdat(cdis2[m]); //显示字符 m++; } dis_buf = 0x2d; //显示字符"-" while(1) { keydown(); lcd_pos(0x4c); lcd_wdat(dis_buf); //第一位数显示 } }
上一篇:I2C C51
下一篇:DS18B20
登录 注册