找回密码
 立即注册

QQ登录

只需一步,快速开始

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

提供一个自动窗帘的51单片机程序(蓝桥杯的题)

[复制链接]
跳转到指定楼层
楼主
ID:500662 发表于 2019-3-29 16:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提供一个自动窗帘的51单片机程序(蓝桥杯的题)

单片机源程序如下:
  1. #include<reg51.h>
  2. #include<time1302.h>

  3. #define        uchar unsigned char
  4. #define uint unsigned int

  5. sbit key1=P2^3;         //独立键盘的各个位置
  6. sbit key2=P2^4;
  7. sbit key3=P2^5;
  8. sbit key4=P2^6;
  9. sbit clock=P3^6;                //是单片机上IO的位置
  10. sbit dout=P3^7;                         //是out对应的位置


  11. uchar wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};                                   //位选
  12. uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};                 //段选         0到9
  13. uchar cun[8];          //每位数据存放的数组
  14. uchar a;

  15. void mode_change();
  16. void chushi();
  17. void dis_clock_time();
  18. uchar read();

  19. void delay(uint x)
  20. {
  21.       uint i,j;
  22.           for(i=x;i>0;i--)
  23.           for(j=110;j>0;j--);
  24. }

  25. void display()           //显示函数的固定格式          其中i和wei的取值根据自己的需要增加和减少
  26. {
  27.         uint i;
  28.         for(i=0;i<8;i++)          //两位数的循环 两取决于i
  29.         {
  30.                    P1=0xff;   //消影
  31.                 switch(i)
  32.                 {
  33.                         case 0:P0=wei[0];break;         //选择第几位
  34.                         case 1:P0=wei[1];break;
  35.                         case 2:P0=wei[2];break;
  36.                     case 3:P0=wei[3];break;
  37.                         case 4:P0=wei[4];break;
  38.                         case 5:P0=wei[5];break;
  39.                         case 6:P0=wei[6];break;
  40.                         case 7:P0=wei[7];break;

  41.                 }
  42.                 P1=cun[i];                          //存放第几位
  43.                 delay(1);       
  44.         }       
  45. }

  46. void main()
  47. {
  48.     Ds1302Init();
  49.         chushi();
  50.         while(1)
  51.         {       
  52.                 a=read();
  53.                 Ds1302ReadTime();
  54.                 mode_change();
  55.                 display();
  56.         }
  57. }


  58. void mode_change()
  59. {
  60.    if(key1 == 0)           //独立键盘的固定格式
  61.         {
  62.                 while(!key1)
  63.                   {
  64.                   cun[0]=~duan[TIME[5]%16];      //年       
  65.                   cun[1]=~duan[TIME[5]/16];       
  66.                   cun[2]=~duan[TIME[6]/16];       
  67.                   cun[3]=~duan[TIME[6]%16]&0x7f;          
  68.                   cun[4]=~duan[TIME[4]/16];            //月
  69.                   cun[5]=~duan[TIME[4]%16]&0x7f;               
  70.                   cun[6]=~duan[TIME[3]/16];
  71.                   cun[7]=~duan[TIME[3]%16];            //日
  72.                   display();          //这个可以根据自己的需要而定,即键盘的作用

  73.                 }
  74.         }
  75.        
  76. }

  77. void chushi()
  78. {
  79.                      cun[0]=~duan[10];
  80.                   cun[1]=~duan[10];       
  81.                   cun[2]=~duan[10];       
  82.                   cun[3]=~duan[10];          
  83.                   cun[4]=~duan[10];
  84.                   cun[5]=~duan[10];               
  85.                   cun[6]=~duan[10];
  86.                   cun[7]=~duan[10];            
  87. }

  88. void dis_clock_time()                                                //时钟显示         
  89. {
  90.           
  91.           Ds1302ReadTime();          
  92.       cun[0]=~duan[TIME[2]/16];                //小时
  93.           cun[1]=~duan[TIME[2]%16];
  94.           //闪烁  在中断里面                                 
  95.           cun[2]=~duan[TIME[1]/16];              //分钟
  96.           cun[3]=~duan[TIME[1]%16];          
  97.           cun[4]=~duan[10];                             //“-”
  98.           cun[5]=~duan[a/100];                    //ad显示
  99.           cun[6]=~duan[a/10%10];
  100.           cun[7]=~duan[a%10];
  101.           display();
  102. }


  103. uchar read()  //AD固定格式
  104. {
  105.         uint i;

  106.         uchar ad=0x00;

  107.         dout=1;
  108.         for(i=0;i<8;i++)
  109.         {
  110.                 ad=ad<<1;          //左移的数码管的 方便显示各个位置的
  111.                 if(dout)
  112.                 {
  113.                         ad++;
  114.                 }
  115.         clock=1;
  116.         clock=0;
  117.         _nop_();           //相当于延迟函数delay
  118.                 _nop_();
  119.         }
  120.         return ad;
  121. }
复制代码

所有资料51hei提供下载:
自动窗帘.rar (33.99 KB, 下载次数: 13)



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

使用道具 举报

沙发
ID:1 发表于 2019-3-31 02:36 | 只看该作者
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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