找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2958|回复: 6
收起左侧

这个单片机DS1302程序如何做成定时开关?求思路

[复制链接]
ID:890865 发表于 2021-3-15 14:19 | 显示全部楼层 |阅读模式
1615788998(1).jpg

由于本论坛禁止直接求程序,禁止伸手党,所以向大家请教一下大致的实现方法与思路,理清头绪后我自己来写程序去实现

  1. #include<reg52.h>
  2. #include<intrins.h>
  3. #include<string.h>
  4. #include"DS1302.h"

  5. uchar Adjust_Index=-1;
  6. uchar MonthsDays[]={0,31,0,31,30,31,30,31,31,30,31,30,31};
  7. //****************************************************************************
  8. void Write_Byte(uchar x)
  9. {
  10.         uchar i;
  11.         for(i=0;i<8;i++)
  12.         {
  13.          IO=x&1;
  14.          SCLK=1;
  15.          _nop_();_nop_();_nop_();_nop_();_nop_();
  16.          SCLK=0;
  17.          x>>=1;
  18.         }
  19. }
  20. //***********************************************************************
  21. uchar Get_Byte()
  22. {
  23.   uchar i,b,t;
  24.   for(i=0;i<8;i++)
  25.          {
  26.           b>>=1;t=IO;b|=t<<7;
  27.           SCLK=1;
  28.           _nop_();_nop_();_nop_();_nop_();_nop_();
  29.           SCLK=0;
  30.          }
  31.          t=b/16*10+b%16;
  32.          return t;
  33. }
  34. //*************************************************************************
  35. uchar Read_Data(uchar addr)
  36. {
  37.          uchar t;
  38.          RST=0;SCLK=0;RST=1;
  39.          Write_Byte(addr);
  40.          _nop_();_nop_();_nop_();_nop_();_nop_();
  41.          t=Get_Byte();
  42.          SCLK=1;RST=0;
  43.          return t;
  44. }
  45. //*****************************************************************************
  46. void Write_DS1302(uchar addr,uchar dat)
  47. {
  48.          SCLK=0;RST=1;
  49.          Write_Byte(addr);
  50.          Write_Byte(dat);
  51.          SCLK=1;RST=0;
  52. }
  53. //****************************************************************************
  54. void Set_Time()
  55. {
  56.    uchar i;
  57.    Write_DS1302(0x8e,0x00);
  58.    for(i=0;i<7;i++)
  59.        Write_DS1302(0x80+2*i,(DateTime[i]/10<<4|DateTime[i]%10));
  60.            Write_DS1302(0x8e,0x80);
  61. }
  62. //*****************************************************************************
  63. void Get_Time()
  64. {
  65.    uchar i;
  66.    for(i=0;i<7;i++)
  67.     DateTime[i]=Read_Data(0x81+2*i);
  68. }
  69. //********************************************************************************
  70. void DateTime_Adjust(char x)
  71.   {
  72.    uchar y;
  73.    switch(Adjust_Index)
  74.      {
  75.            case 0://Ãëμ÷Õû
  76.            if(x==1&&DateTime[0]<59)DateTime[0]++;
  77.            if(x==-1&&DateTime[0]>0)DateTime[0]--;
  78.                  break;
  79.            case 1://·Ö0-59
  80.              if(x==1&&DateTime[1]<59)DateTime[1]++;
  81.                  if(x==-1&&DateTime[1]>0)DateTime[1]--;
  82.                  break;
  83.               case 2://ê±0-23
  84.              if(x==1&&DateTime[2]<23)DateTime[2]++;
  85.                  if(x==-1&&DateTime[2]>0)DateTime[2]--;
  86.                  break;
  87.            case 3: //èÕ
  88.                    y=2000+DateTime[6];
  89.                    if((y%4==0&&y%100!=0)||(y%400==0))
  90.                    MonthsDays[2]=29;
  91.                    else
  92.                    MonthsDays[2]=28;
  93.               if(x==1&&DateTime[3]<MonthsDays[DateTime[4]])DateTime[3]++;
  94.                   if(x==-1&&DateTime[3]>0)DateTime[3]--;
  95.                   break;
  96.            case 4: //ÔÂ
  97.                      if(x==1&&DateTime[4]<12)DateTime[4]++;
  98.                          if(x==-1&&DateTime[4]>0)DateTime[4]--;
  99.                          y=2000+DateTime[6];
  100.                    if((y%4==0&&y%100!=0)||(y%400==0))
  101.                    MonthsDays[2]=29;
  102.                    else
  103.                    MonthsDays[2]=28;
  104.               if(DateTime[3]<MonthsDays[DateTime[4]])
  105.                      DateTime[3]=MonthsDays[DateTime[4]];
  106.                          break;
  107.                 case 5://DÇÆú
  108.                   if(x==1&&DateTime[5]<7)DateTime[5]++;
  109.                   if(x==-1&&DateTime[5]>1)DateTime[5]--;
  110.                   break;
  111.                 case 6:        //Äê
  112.                   if(x==1&&DateTime[6]<99)DateTime[6]++;
  113.                   if(x==-1&&DateTime[6]>0)DateTime[6]--;
  114.                   y=2000+DateTime[6];
  115.                    if((y%4==0&&y%100!=0)||(y%400==0))
  116.                    MonthsDays[2]=29;
  117.                    else
  118.                    MonthsDays[2]=28;
  119.               if(DateTime[3]<MonthsDays[DateTime[4]])
  120.                      DateTime[3]=MonthsDays[DateTime[4]];
  121.                   break;                                                      
  122.          }
  123.   }
复制代码


回复

使用道具 举报

ID:890865 发表于 2021-3-15 14:20 | 显示全部楼层
想要用这个做成定时开关
回复

使用道具 举报

ID:844772 发表于 2021-3-15 16:07 | 显示全部楼层
1、设置成闹钟,闹钟到你就输出开关,也就是不断检查DateTime值是否等于闹钟值,如需要可以设置成某天的某个时刻,也可以天天到时开;2、设置成倒计时的定时器,那就直接算好DateTime值等于啥时开。
回复

使用道具 举报

ID:890865 发表于 2021-3-15 16:14 | 显示全部楼层
glinfei 发表于 2021-3-15 16:07
1、设置成闹钟,闹钟到你就输出开关,也就是不断检查DateTime值是否等于闹钟值,如需要可以设置成某天的某 ...

要怎么改才能让它动起来,显示当前时间
回复

使用道具 举报

ID:584814 发表于 2021-3-16 10:29 | 显示全部楼层
买个闹钟,设定闹钟时间,单片机上接个话筒,接收到闹钟声音就打开指定开关
或者在本坛里找带闹钟的DS1302+12864时钟程例伸手可得。
回复

使用道具 举报

ID:139866 发表于 2021-3-16 12:00 | 显示全部楼层
man1234567 发表于 2021-3-16 10:29
买个闹钟,设定闹钟时间,单片机上接个话筒,接收到闹钟声音就打开指定开关
或者在本坛里找带闹钟 ...

厉害,你这思路我想了一上午都想不出来
回复

使用道具 举报

ID:158375 发表于 2021-3-16 15:10 | 显示全部楼层
楼主 :你还是仔细看 DS1302 的技术手册,资料看明白了,程序怎么写自然就清楚了。
         尤其是寄存器。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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