Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630753
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:31:18

原文地址:LCD+矩阵按键值 作者:luozhiyong131

#include <reg51.h>
#include <intrins.h>
                
#define uchar unsigned char
#define uint unsigned int
uchar dis_buf; //显示缓存

uchar temp;
uchar key; //键顺序吗

void delay0(uchar x); //x*0.14MS

sbit 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); //第一位数显示

    }
  }
 


阅读(178) | 评论(0) | 转发(0) |
0

上一篇:I2C C51

下一篇:DS18B20

给主人留下些什么吧!~~