注意:访问本站需要Cookie和JavaScript支持!请设置您的浏览器! 打开购物车 查看留言付款方式联系我们
初中电子 单片机教材一 单片机教材二
搜索上次看见的商品或文章:
商品名、介绍 文章名、内容
首页 电子入门 学单片机 免费资源 下载中心 商品列表 象棋在线 在线绘图 加盟五一 加入收藏 设为首页
本站推荐:
33.4×4键盘及8位数码管显示构成的电子密码锁
文章长度[] 加入时间[2008/5/30] 更新时间[2024/12/27 14:39:53] 级别[0] [评论] [收藏]
 
1.      实验任务
用4×4组成0-9数字键及确认键。
用8位数码管组成显示电路提示信息,当输入密码时,只显示“8.”,当密码位数输入完毕按下确认键时,对输入的密码与设定的密码进行比较,若密码正确,则门开,此处用LED发光二极管亮一秒钟做为提示,同时发出“叮咚”声;若密码不正确,禁止按键输入3秒,同时发出“嘀、嘀”报警声;若在3秒之内仍有按键按下,则禁止按键输入3秒被重新禁止。
2.      电路原理图
图4.33.1
3.      系统板上硬件连线
(1).            把“单片机系统”区域中的P0.0-P0.7用8芯排线连接到“动态数码显示”区域中的ABCDEFGH端子上。
(2).            把“单片机系统“区域中的P2.0-P2.7用8芯排线连接到“动态数码显示”区域中的S1S2S3S4S5S6S7S8端子上。
(3).            把“单片机系统”区域中的P3.0-P3.7用8芯排线连接到“4×4行列式键盘”区域中的R1R2R3R4C1C2C3C4端子上。
(4).            把“单片机系统”区域中的P1.0用导线连接到“八路发光二极管模块”区域中的L2端子上。
(5).            把“单片机系统”区域中的P1.7用导线连接到“音频放大模块”区域中的SPK IN端子上。
(6).            把“音频放大模块”区域中的SPK OUT接到喇叭上。
4.      程序设计内容
(1).            4×4行列式键盘识别技术:有关这方面内容前面已经讨论过,这里不再重复。
(2).            8位数码显示,初始化时,显示“P   ”,接着输入最大6位数的密码,当密码输入完后,按下确认键,进行密码比较,然后给出相应的信息。在输入密码过程中,显示器只显示“8.”。当数字输入超过6个时,给出报警信息。在密码输入过程中,若输入错误,可以利用“DEL”键删除刚才输入的错误的数字。
(3).            4×4行列式键盘的按键功能分布图如图4.33.2所示:

7
8
9
 
4
5
6
 
1
2
3
Del
0
 
 
Enter

图4.33.2
5.      C语言源程序
#include <AT89X52.H>
unsigned char ps[]={1,2,3,4,5};
unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,
                              0xef,0xdf,0xbf,0x7f};
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
                               0x6d,0x7d,0x07,0x7f,0x6f,
                               0x77,0x7c,0x39,0x5e,0x79,0x71,
                               0x00,0x40,0x73,0xff};
unsigned char dispbuf[8]={18,16,16,16,16,16,16,16};
unsigned char dispcount;
unsigned char flashcount;
unsigned char temp;
unsigned char key;
unsigned char keycount;
unsigned char pslen=5;
unsigned char getps[6];
bit keyoverflag;
bit errorflag;
bit rightflag;
unsigned int second3;
unsigned int aa,bb;
unsigned int cc;
bit okflag;
bit alarmflag;
bit hibitflag;
unsigned char oka,okb;
void main(void)
{
  unsigned char i,j;
  TMOD=0x01;
  TH0=(65536-500)/256;
  TL0=(65536-500)%256;
  TR0=1;
  ET0=1;
  EA=1;
  while(1)
    {
      P3=0xff;
      P3_4=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=7;
                    break;
                  case 0x0d:
                    key=8;
                    break;
                  case 0x0b:
                    key=9;
                    break;
                  case 0x07:
                    key=10;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i<keycount;i++)
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }
        }
      P3=0xff;
      P3_5=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=4;
                    break;
                  case 0x0d:
                    key=5;
                    break;
                  case 0x0b:
                    key=6;
                    break;
                  case 0x07:
                    key=11;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i<keycount;i++)
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a4;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a4:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }
        }
      P3=0xff;
      P3_6=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;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=12;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i<keycount;i++)
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a3;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a3:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }
        }
      P3=0xff;
      P3_7=0;
      temp=P3;
      temp=temp & 0x0f;
      if (temp!=0x0f)
        {
          for(i=10;i>0;i--)
          for(j=248;j>0;j--);
          temp=P3;
          temp=temp & 0x0f;
          if (temp!=0x0f)
            {
              temp=P3;
              temp=temp & 0x0f;             
              switch(temp)
                {
                  case 0x0e:
                    key=0;
                    break;
                  case 0x0d:
                    key=13;
                    break;
                  case 0x0b:
                    key=14;
                    break;
                  case 0x07:
                    key=15;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i<keycount;i++)
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a2;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a2:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }                         
        }
     
    }
}
void t0(void) interrupt 1 using 0
{
  TH0=(65536-500)/256;
  TL0=(65536-500)%256;
  flashcount++;
  if(flashcount==8)
    {
      flashcount=0;
      P0=dispcode[dispbuf[dispcount]];
      P2=dispbit[dispcount];
      dispcount++;
      if(dispcount==8)
        {
          dispcount=0;
        }
    }
  if((errorflag==1) && (rightflag==0))
    {
      bb++;
      if(bb==800)
        {
          bb=0;
          alarmflag=~alarmflag;
        }
      if(alarmflag==1)//sound alarm signal
        {
          P1_7=~P1_7;
        }
      aa++;
      if(aa==800)//light alarm signal
        {
          aa=0;
          P1_0=~P1_0;
        }
      second3++;
      if(second3==6400)
        {
          second3=0;
          errorflag=0;
          rightflag=0;
          alarmflag=0;
          bb=0;
          aa=0;
        }     
    }
    else if((errorflag==0) && (rightflag==1))
      {
        P1_0=0;
        cc++;
        if(cc<1000)
          {
            okflag=1;
          }
          else if(cc<2000)
            {
              okflag=0;
            }
            else
              {
                errorflag=0;
                rightflag=0;
                P1_7=1;
                cc=0;
                oka=0;
                okb=0;
                okflag=0;
                P1_0=1;
              }
        if(okflag==1)
          {
            oka++;
            if(oka==2)
              {
                oka=0;
                P1_7=~P1_7;
              }
          }
          else
            {
              okb++;
              if(okb==3)
                {
                  okb=0;
                  P1_7=~P1_7;
                }
            }
      }
  if(keyoverflag==1)
    {
      P1_7=~P1_7;
    }
}
nbsp;          case 0x0d:
                    key=13;
                    break;
                  case 0x0b:
                    key=14;
                    break;
                  case 0x07:
                    key=15;
                    break;
                }
              temp=P3;
              P1_1=~P1_1;
              if((key>=0) && (key<10))
                {
                  if(keycount<6)
                    {
                      getps[keycount]=key;
                      dispbuf[keycount+2]=19;
                    }
                  keycount++;
                  if(keycount==6)
                    {
                      keycount=6;
                    }
                    else if(keycount>6)
                      {
                        keycount=6;
                        keyoverflag=1;//key overflow                       
                      }
                }
                else if(key==12)//delete key
                  {
                    if(keycount>0)
                      {
                        keycount--;
                        getps[keycount]=0;
                        dispbuf[keycount+2]=16;
                      }
                      else
                        {
                          keyoverflag=1;
                        }
                  }
                  else if(key==15)//enter key
                    {
                      if(keycount!=pslen)
                        {
                          errorflag=1;
                          rightflag=0;
                          second3=0;
                        }
                        else
                          {
                            for(i=0;i<keycount;i++)
                              {
                                if(getps[i]!=ps[i])
                                  {
                                    i=keycount;
                                    errorflag=1;
                                    rightflag=0;
                                    second3=0;
                                    goto a2;
                                  }
                              }
                            errorflag=0;
                            rightflag=1;
a2:                          i=keycount;
                          }
                    }
              temp=temp & 0x0f;             
              while(temp!=0x0f)
                {
                  temp=P3;
                  temp=temp & 0x0f;             
                }
              keyoverflag=0;//?????????
            }                         
        }
     
    }
}
void t0(void) interrupt 1 using 0
{
  TH0=(65536-500)/256;
  TL0=(65536-500)%256;
  flashcount++;
  if(flashcount==8)
    {
      flashcount=0;
      P0=dispcode[dispbuf[dispcount]];
      P2=dispbit[dispcount];
      dispcount++;
      if(dispcount==8)
        {
          dispcount=0;
        }
    }
  if((errorflag==1) && (rightflag==0))
    {
      bb++;
      if(bb==800)
        {
          bb=0;
          alarmflag=~alarmflag;
        }
      if(alarmflag==1)//sound alarm signal
        {
          P1_7=~P1_7;
        }
      aa++;
      if(aa==800)//light alarm signal
        {
          aa=0;
          P1_0=~P1_0;
        }
      second3++;
      if(second3==6400)
        {
          second3=0;
          errorflag=0;
          rightflag=0;
          alarmflag=0;
          bb=0;
          aa=0;
        }     
    }
    else if((errorflag==0) && (rightflag==1))
      {
        P1_0=0;
        cc++;
        if(cc<1000)
          {
            okflag=1;
          }
          else if(cc<2000)
            {
              okflag=0;
            }
            else
              {
                errorflag=0;
                rightflag=0;
                P1_7=1;
                cc=0;
                oka=0;
                okb=0;
                okflag=0;
                P1_0=1;
              }
        if(okflag==1)
          {
            oka++;
            if(oka==2)
              {
                oka=0;
                P1_7=~P1_7;
              }
          }
          else
            {
              okb++;
              if(okb==3)
                {
                  okb=0;
                  P1_7=~P1_7;
                }
            }
      }
  if(keyoverflag==1)
    {
      P1_7=~P1_7;
    }
}
1、 本站不保证以上观点正确,就算是本站原创作品,本站也不保证内容正确。
2、如果您拥有本文版权,并且不想在本站转载,请书面通知本站立即删除并且向您公开道歉! 以上可能是本站收集或者转载的文章,本站可能没有文章中的元件或产品,如果您需要类似的商品请 点这里查看商品列表!
本站协议 | 版权信息 |  关于我们 |  本站地图 |  营业执照 |  发票说明 |  付款方式 |  联系方式
深圳市宝安区西乡五壹电子商行——粤ICP备16073394号-1;地址:深圳西乡河西四坊183号;邮编:518102
E-mail:51dz$163.com($改为@);Tel:(0755)27947428
工作时间:9:30-12:00和13:30-17:30和18:30-20:30,无人接听时可以再打手机13537585389