这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » hellok MCU DIY进程帖 增加矩阵键盘使用

共13条 2/2 1 2 跳转至
工程师
2011-11-28 22:07:11     打赏
11楼


板子收到两天了,收到当天就焊了个最小系统,没时间加怕焊出来用不了,多的没焊。回家发现居然用不了,烧了程序的单片机装上去也不工作,没办法等今天上班到公司查,发现居然晶振坏了。。板子的焊盘太小,一拆就掉铜皮,晶振只好焊在了正面。板子现在只焊了这个样子,需要什么再焊吧。
手里只有AT89S52,好像不能用串口下载,没办法用烧录器烧好装上去。





先上个最简单的流水灯程序吧,最简单的程序,谁都会。程序如下:
#include <AT89X51.H>
unsigned char code table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
       0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x81,
       0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3e,0x7c,
       0xf0,0xe1,0xc3,0x87,0x0f,0x1e,0x3c,0x78,
       0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,
       0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
       0x00,0xff,0x00,0xff,
       0xee};
unsigned char i=0;

void delay(void)
{
    unsigned char m,n;
    for(m=1500;m>0;m--)
        for(n=500;n>0;n--);
}

void main(void)
{
    while(1)
    {
        if(table[i]!=0xee)
        {
      P1=table[i];
   i++;
   delay();
  }
  else
  {
      i=0;
  }
 }
}


工程师
2011-12-03 22:04:25     打赏
12楼

不知道为什么不忙却总有些事,好久没更新了,先更新的1602的显示。

/********LCD1602显示程序********/
/********  2011-12-01   ********/

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^4;
sbit rw=P2^5;
sbit lcden=P2^6;
uchar table1[]="HELLO EEPW";
uchar table2[]="1602 DISPLAY";
void delay(uint x)
{
 uint a,b;
 for(a=x;a>0;a--)
  for(b=10;b>0;b--);
}

void write_command(uchar command)   //写指令子程序;
{
 P0=command;
 rs=0;
 delay(10);
 lcden=1;
 delay(10);
 lcden=0;
 
}

void write_date(uchar date)
{           //写数据子程序;
 P0=date;
 rs=1;
 delay(10);
 lcden=1;
 delay(10);
 lcden=0;
 
}

void main()
{
 uchar a;
 rw=0;
 write_command(0x38);   //8位数据线,两行,5X7点阵
 delay(20);
 write_command(0x0c);   //开显示,光标不显示,不闪烁
 delay(20);
 write_command(0x06);   //光标右移1位,显示不移
 delay(20);
 write_command(0x01);   //清显示;
 delay(20);

 write_command(0x80+19);//第1行字符;
 delay(20);
 for(a=0;a<10;a++)
 {
 write_date(table1[a]);
 delay(20);  
 }

 write_command(0xc0+18); //第2行字符;
 delay(50);
 for(a=0;a<12;a++)
 {
 write_date(table2[a]);
 delay(40);  
 }

 for(a=0;a<16;a++)  //字符往左移动;
 {
  write_command(0x18); //光标与显示一起向左移动,AC值不变;
  delay(2000);
 }

 while(1);
}


工程师
2011-12-21 22:50:29     打赏
13楼

好久没有更新了,都有点不好意思露面了。最近把该焊的都焊完,在1602程序里加入了矩阵键盘的使用




/*************LCD1602显示程序*****************/
/**************  2011-12-01   ****************/
/********2011-12-21 增加矩阵键盘的使用*******/

#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int

sbit rs = P2^4;            
sbit rw = P2^5;
sbit lcden = P2^6;
sbit key_lin1 = P3^4;
sbit key_lin2 = P3^5;
sbit key_lin3 = P3^6;
sbit key_lin4 = P3^7;
uchar temp;
uchar key;
uchar i, j;
uchar table1[] = "HELLO EEPW";
uchar table2[] = "KEY S";
uchar table[] = {" 1 2 3 4 5 6 7 8 910111213141516"};

void delay(uint x)
{
 uint a,b;
 for(a = x; a > 0; a--)
  for(b = 10; b > 0; b--)
  {
      ;
  }
}

void write_command(uchar command)   //写指令子程序;
{
 P0 = command;
 rs = 0;
 delay(10);
 lcden = 1;
 delay(10);
 lcden = 0;
}

void write_date(uchar date)
{           //写数据子程序;
 P0 = date;
 rs = 1;
 delay(10);
 lcden = 1;
 delay(10);
 lcden = 0;
}

void main()
{
   
 /******LCD1602初始化******/
 uchar a;
 rw = 0;
 write_command(0x38);   //8位数据线,两行,5X7点阵
 delay(20);
 write_command(0x0c);   //开显示,光标不显示,不闪烁
 delay(20);
 write_command(0x06);   //光标右移1位,显示不移
 delay(20);
 write_command(0x01);   //清显示;
 delay(20);

 write_command(0x80+3);     //第1行字符;
 delay(20);
 for(a = 0; a < 10; a++)
 {
     write_date(table1[a]);
     delay(20);  
 }

 write_command(0xc0+4);   //第2行字符;
 delay(50);
 for(a = 0; a < 5; a++)
 {
     write_date(table2[a]);
     delay(40);  
 }

 while(1)
 {

 /*P3.4输出低电平,通过检测P3.0-P3.3的电平来判断第4竖排按键是否按下*/
        P3 = 0xff;
        key_lin1 = 0;
        temp = P3;
        temp = temp & 0x0f;
        if (temp != 0x0f)
        {
            for(i=50;i>0;i--)
                for(j=200;j>0;j--)
    {
        ;
    }
            temp = P3;
            temp = temp & 0x0f;
            if (temp != 0x0f)
            {
                temp = P3;
                temp = temp & 0x0f;
                switch(temp)
                {
                    case 0x0e:
                        key = 13;
                        break;
                    case 0x0d:
                        key = 14;
      break;
                    case 0x0b:
         key = 15;
      break;
                    case 0x07:
         key = 16;
      break;
    }
    temp = P3;
       write_command(0xc0 + 9);
             delay(50);
    a = key * 2 - 2;
             write_date(table[a]);
             delay(40);
             write_date(table[a+1]);
          delay(40);            
    temp = temp & 0x0f;
    while(temp != 0x0f)
    {
        temp = P3;
     temp = temp & 0x0f;
    }
   }
  }

  /*P3.5输出低电平,通过检测P3.0-P3.3的电平来判断第3竖排按键是否按下*/
        P3 = 0xff;
  key_lin2 = 0;
  temp = P3;
  temp = temp & 0x0f;
  if (temp != 0x0f)
  {
      for(i = 50; i > 0; i--)
       for(j = 200; j > 0; j--)
    {
        ;
    }
   temp = P3;
   temp = temp & 0x0f;
   if (temp != 0x0f)
   {
       temp = P3;
    temp = temp & 0x0f;
    switch(temp)
    {
        case 0x0e:
         key = 9;
      break;
     case 0x0d:
         key = 10;
      break;
     case 0x0b:
         key = 11;
      break;
     case 0x07:
         key = 12;
      break;
    }
    temp = P3;
       write_command(0xc0 + 9);
             delay(50);
    a = key * 2 - 2;
             write_date(table[a]);
             delay(40);
             write_date(table[a + 1]);
          delay(40);
    temp=temp & 0x0f;
    while(temp != 0x0f)
    {
        temp = P3;
     temp = temp & 0x0f;
    }
   }
  }

  /*P3.6输出低电平,通过检测P3.0-P3.3的电平来判断第2竖排按键是否按下*/
        P3 = 0xff;
  key_lin3 = 0;
  temp = P3;
  temp = temp & 0x0f;
  if (temp != 0x0f)
  {
      for(i = 50; i > 0; i--)
       for(j = 200; j > 0; j--)
       {
           ;
       }
   temp = P3;
   temp = temp & 0x0f;
   if (temp != 0x0f)
   {
       temp = P3;
    temp = temp & 0x0f;
    switch(temp)
    {
        case 0x0e:
         key = 5;
      break;
     case 0x0d:
         key = 6;
      break;
     case 0x0b:
         key = 7;
      break;
     case 0x07:
            key = 8;
      break;
    }
    temp = P3;
          write_command(0xc0 + 9);
             delay(50);
    a = key * 2 - 2;
             write_date(table[a]);
             delay(40);
             write_date(table[a + 1]);
          delay(40);
    temp = temp & 0x0f;
    while(temp != 0x0f)
    {
        temp = P3;
     temp = temp & 0x0f;
    }
   }
  }

  /*P3.7输出低电平,通过检测P3.0-P3.3的电平来判断第1竖排按键是否按下*/
        P3 = 0xff;
  key_lin4 = 0;
  temp = P3;
  temp = temp & 0x0f;
  if (temp != 0x0f)
  {
      for(i = 50; i > 0; i--)
   for(j = 200; j > 0; j--)
   {
       ;
   }
   temp = P3;
   temp = temp & 0x0f;
   if (temp != 0x0f)
   {
       temp = P3;
    temp = temp & 0x0f;
    switch(temp)
    {
        case 0x0e:
         key = 1;
      break;
     case 0x0d:
         key = 2;
      break;
     case 0x0b:
         key = 3;
      break;
     case 0x07:
         key = 4;
      break;
    }
    temp = P3;
    write_command(0xc0 + 9);
             delay(50);
    a = key * 2 - 2;
             write_date(table[a]);
             delay(40);
             write_date(table[a + 1]);
          delay(40);
    temp=temp & 0x0f;
    while(temp != 0x0f)
    {
        temp = P3;
     temp = temp & 0x0f;
    }
   }
  }
 }
}


共13条 2/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]