找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 11210|回复: 5
打印 上一主题 下一主题
收起左侧

单片机4*4矩阵键盘按键控制led灯的程序及仿真

[复制链接]
跳转到指定楼层
楼主
通过1-8按键显示逐个灯亮一个。9全灭。。10左移一位依次类推。。。这是一个简单的单片机例子,下面是仿真原理图:

所有资料打包下载: 4乘4按键控制灯.zip (52.31 KB, 下载次数: 77)

原理图:
  1. #include <reg52.h>
  2. #include<intrins.h>
  3. typedef  unsigned char   int8;
  4. typedef  unsigned int    int16;

  5. sbit KeyIn1  = P1^0;
  6. sbit KeyIn2  = P1^1;
  7. sbit KeyIn3  = P1^2;
  8. sbit KeyIn4  = P1^3;
  9. sbit KeyOut1 = P1^4;
  10. sbit KeyOut2 = P1^5;
  11. sbit KeyOut3 = P1^6;
  12. sbit KeyOut4 = P1^7;

  13. void delay(void);
  14. void delay1s(void)   
  15. {
  16.     unsigned char a,b,c;
  17.     for(c=167;c>0;c--)
  18.         for(b=171;b>0;b--)
  19.             for(a=16;a>0;a--);
  20.                         }

  21. main()
  22. {        int8 i;
  23.         P0 = 0xFF;
  24.         while (1)
  25.         {
  26.                 KeyOut1 = 0;
  27.                 KeyOut2 = 1;
  28.                 KeyOut3 = 1;
  29.                 KeyOut4 = 1;
  30.                 if(KeyIn1 == 0)
  31.                 {
  32.                         delay();
  33.                         if(KeyIn1 == 0)
  34.                          { i=0 ;P0 = 0xfe;  }
  35.                 }
  36.                 if(KeyIn2 == 0)
  37.                 {
  38.                         delay();
  39.                         if(KeyIn2 == 0)
  40.                           { i=0 ; P0 = 0xe0;}
  41.                 }
  42.                 if(KeyIn3 == 0)
  43.                 {
  44.                         delay();
  45.                         if(KeyIn3 == 0)
  46.                         { i=0 ; P0=0Xff; }
  47.                         
  48.                 }        
  49.                 if(KeyIn4 == 0)
  50.                 {
  51.                         delay();
  52.                         if(KeyIn4 == 0)
  53.                           {i=1;P0=0XF0;}
  54.                 }
  55.                 while((KeyIn1 == 0)||(KeyIn2 == 0)||
  56.                       (KeyIn3 == 0)||(KeyIn4 == 0));
  57.                
  58.                 KeyOut1 = 1;
  59.                 KeyOut2 = 0;
  60.                 KeyOut3 = 1;
  61.                 KeyOut4 = 1;
  62.                 if(KeyIn1 == 0)
  63.                 {
  64.                         delay();
  65.                         if(KeyIn1 == 0)
  66.                          { i=0 ;  P0 = 0xfc;   }
  67.                 }
  68.                 if(KeyIn2 == 0)
  69.                 {
  70.                         delay();
  71.                         if(KeyIn2 == 0)
  72.                          { i=0 ;  P0 = 0xc0; }
  73.                 }
  74.                 if(KeyIn3 == 0)
  75.                 {
  76.                         delay();
  77.                         if(KeyIn3 == 0)
  78.                          {  i=1;  P0=0XFE;}
  79.                 }        
  80.                 if(KeyIn4 == 0)
  81.                 {
  82.                         delay();
  83.                         if(KeyIn4 == 0)
  84.                            {i=1;P0=0XE0;}
  85.                 }
  86.                 while((KeyIn1 == 0)||(KeyIn2 == 0)||
  87.                       (KeyIn3 == 0)||(KeyIn4 == 0));

  88.                 KeyOut1 = 1;
  89.                 KeyOut2 = 1;
  90.                 KeyOut3 = 0;
  91.                 KeyOut4 = 1;
  92.                 if(KeyIn1 == 0)
  93.                 {
  94.                         delay();
  95.                         if(KeyIn1 == 0)
  96.                         { i=0 ;   P0 = 0xf8;  }
  97.                 }
  98.                 if(KeyIn2 == 0)
  99.                 {
  100.                         delay();
  101.                         if(KeyIn2 == 0)
  102.                         { i=0 ;   P0 = 0x80; }
  103.                 }
  104.                 if(KeyIn3 == 0)
  105.                 {
  106.                         delay();
  107.                         if(KeyIn3 == 0)
  108.                           {i=1;P0=0XFC;}
  109.                 }        
  110.                 if(KeyIn4 == 0)
  111.                 {
  112.                         delay();
  113.                         if(KeyIn4 == 0)
  114.                             {i=1;P0=0XC0;}
  115.                 }
  116.                 while((KeyIn1 == 0)||(KeyIn2 == 0)||
  117.                       (KeyIn3 == 0)||(KeyIn4 == 0));

  118.                 KeyOut1 = 1;
  119.                 KeyOut2 = 1;
  120.                 KeyOut3 = 1;
  121.                 KeyOut4 = 0;
  122.                 if(KeyIn1 == 0)
  123.                 {
  124.                         delay();
  125.                         if(KeyIn1 == 0)
  126.                          { i=0 ;   P0 = 0xf0; }
  127.                 }
  128.                 if(KeyIn2 == 0)
  129.                 {
  130.                         delay();
  131.                         if(KeyIn2 == 0)
  132.                           { i=0 ;  P0 = 0x00;}
  133.                 }
  134.                 if(KeyIn3 == 0)
  135.                 {
  136.                         delay();
  137.                         if(KeyIn3 == 0)
  138.                         {i=1;P0=0XF8;}
  139.                 }        
  140.                 if(KeyIn4 == 0)
  141.                 {
  142.                         delay();
  143.                         if(KeyIn4 == 0)
  144.                           {i=1;P0=0X80;}
  145.                 }
  146.                
  147.            while((KeyIn1 == 0)||(KeyIn2 == 0)||
  148.                       (KeyIn3 == 0)||(KeyIn4 == 0));
  149.                 switch(i)
  150.                 {        case 0: P0=P0;break;
  151.                         case 1: P0=_crol_(P0,1);delay1s(); break;
  152.                
  153.                         default:
  154.                           P0=0XFF;

  155.                
  156.                 }
  157.                
  158.         }
  159. }

  160. void delay(void)
  161. {
  162.         int16 i=500;

  163.         while (i--);

  164. }
复制代码






分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:110866 发表于 2016-12-1 23:19 | 只看该作者
感谢分享,学习到了
回复

使用道具 举报

板凳
ID:159790 发表于 2017-5-14 12:42 | 只看该作者
谢谢分享
回复

使用道具 举报

地板
ID:867353 发表于 2020-12-24 20:28 来自手机 | 只看该作者
有没有详细解释??
回复

使用道具 举报

5#
ID:867353 发表于 2020-12-24 20:31 来自手机 | 只看该作者
求大神解释代码
回复

使用道具 举报

6#
ID:896773 发表于 2021-4-19 14:45 | 只看该作者
代码有注释吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表