找回密码
 立即注册

QQ登录

只需一步,快速开始

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

DS1302红外4位时钟定时器

[复制链接]
跳转到指定楼层
楼主
ID:239605 发表于 2017-10-15 12:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
新手上传一个自用程序

/***************************************************************************
标题:        定时器中断 24小时时钟程序
效果:        数码管显示24小时制时钟
工作于:LY5A-L2A开发板
作者:        林洋电子吧
说明:        板使用12M晶振
******************************************************************************/

  1. //头文件:
  2. #include<reg51.h>
  3. #include<intrins.h>
  4. #include<math.h>

  5. #define uchar unsigned char
  6. #define uint unsigned int
  7. #define S_RST DS_RST=1
  8. #define C_RST DS_RST=0
  9. #define S_CLK DS_CLK=1
  10. #define C_CLK DS_CLK=0

  11. //引脚定义:               
  12. sbit SMG_q = P3^4;        //定义数码管阳级控制脚(千位)
  13. sbit SMG_b = P3^5;        //定义数码管阳级控制脚(百位)
  14. sbit SMG_s = P3^6;        //定义数码管阳级控制脚(十位)
  15. sbit SMG_g = P3^7;        //定义数码管阳级控制脚(个位)
  16. sbit IR=P3^3;                //红外接收数据引脚,
  17. sbit DS_RST  = P0^0;    //DS复位端
  18. sbit DS_IO   = P0^1;    //IO端
  19. sbit DS_CLK  = P0^2;    //SCLK端
  20. sbit SPK  = P0^7;                 //续电器
  21. sbit LED  = P0^6;         
  22. sbit LED1  = P0^5;                 

  23. //变量定义:
  24. code unsigned char table[]=
  25.                         {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF};
  26.                                         //表:共阳数码管 0-9

  27. unsigned char l_posit=0;        //显示位置
  28. unsigned char ly_tt=0;                //作为计数时间量
  29. unsigned char SET=0; //设置标识
  30. unsigned char dskg=0;
  31. unsigned char ddss=0;//掉电标识
  32. unsigned char ss=0;         //闪烁标识
  33. unsigned char ss1=0;        
  34. unsigned char ss2=0;         
  35. unsigned char ss3=0;
  36. unsigned char ts1=0;
  37. //////////////////////
  38. signed char time;
  39. unsigned char shi;
  40. unsigned char fen;
  41. unsigned char miao;
  42. unsigned char yue;
  43. unsigned char ri;
  44. unsigned char nian;
  45. unsigned char zhou;

  46. unsigned char qi;
  47. unsigned char ba;
  48. unsigned char sh;
  49. unsigned char ge;
  50. //函数声明:
  51. void display(unsigned char q,unsigned char b,unsigned char s,unsigned char g);//定义显示函数,参数为显示时分
  52. void delay(uint a);
  53. void init();                                        //初如化函数
  54. void gettime();
  55. uchar BCD_s(uchar BCDs);
  56. uchar s_BCD(uchar sBCD);
  57. void RW_DS1302(uchar reg, uchar byte);
  58. uchar Read_DS1302(uchar reg);//从DS1302寄存器读数据
  59. void DS1302_InputByte(uchar byte); //往DS1302写入1Byte数据
  60. uchar DS1302_OutputByte(void); //从DS1302读取1Byte数据
  61. void ts();
  62. /**************************红外接收处理***********************************
  63. **************************************************************************
  64. *************************************************************************/
  65. unsigned char  irtime;//红外用全局变量
  66. bit irpro_ok=0,irok=0;
  67. unsigned char IRcord[4];
  68. unsigned char irdata[33];
  69. unsigned char xs=0;
  70. unsigned char xstt=2;
  71. unsigned char xs_tt=0;
  72. signed char ds_time;
  73. unsigned char k_fen;
  74. unsigned char k_shi;
  75. unsigned char g_fen;
  76. unsigned char g_shi;
  77. signed char date;
  78. /*------------------------------------------------
  79.                   函数声明
  80. ------------------------------------------------*/
  81. void Ir_work(void);
  82. void Ircordpro(void);
  83. void EX0init(void);
  84. void TIM0init(void);//定时器0初始化
  85. /**************************红外接收处理***********************************
  86. **************************************************************************
  87. *************************************************************************/
  88. //初始化函数
  89. void init()
  90. {
  91.         P1=0XFF;
  92.         SMG_q=0;
  93.         SMG_b=0;        
  94.         SMG_s=0;               
  95.         SMG_g=0;
  96.         SPK=1;
  97.         delay(500);
  98. //        TMOD=0X02;                //设置定时器1为模式一,即16位计算模式
  99. //        TH0=0X00;
  100. //        TL0=0X00;
  101. //        EA=1;                //开启总中断
  102. //        ET0=1;                //开启定时器0中断
  103. //        TR0=1;                //启动定时器
  104. //        EX1=1;                                  //开启外部中断1,红外接收数据引脚
  105. //        IT1=1;                                //设置成下降沿触发方式
  106.         RW_DS1302(0x8e,0x80);delay(10);
  107.         delay(500);

  108. }

  109. //显示函数,参数为显示内容
  110. void display(unsigned char q,unsigned char b,unsigned char s,unsigned char g)
  111. {  
  112.         P1=0XFF;                        //        
  113.         switch(l_posit){
  114.         case 0:                //选择千位数码管,关闭其它位
  115.                 SMG_q=1;
  116.                 SMG_b=0;        
  117.                 SMG_s=0;               
  118.                 SMG_g=0;
  119.                 P1=table[q/10];        //输出显示内容
  120.                 break;
  121.         case 1:                //选择百位数码管,关闭其它位
  122.                 SMG_q=0;
  123.                 SMG_b=1;        
  124.                 SMG_s=0;               
  125.                 SMG_g=0;
  126.                 P1=table[b%10];
  127.                 break;
  128.         case 2:                //选择十位数码管,关闭其它位
  129.                 SMG_q=0;
  130.                 SMG_b=0;        
  131.                 SMG_s=1;               
  132.                 SMG_g=0;
  133.                 P1=table[s/10];
  134.                 break;
  135.         case 3:                //选择个位数码管,关闭其它位
  136.                 SMG_q=0;
  137.                 SMG_b=0;        
  138.                 SMG_s=0;        
  139.                 SMG_g=1;
  140.                 P1=table[g%10];
  141.                 break;        
  142.         }
  143.         l_posit++;                //每调用一次将轮流显示一位
  144.         if(l_posit>3)
  145.                 l_posit=0;        
  146. }
  147. //延时子函数,短暂延时
  148. void delay(uint a)
  149. {
  150.   uint k;
  151.   while(a--)
  152.   {
  153.     for(k=0;k<120;k++){;}
  154.   }
  155. }

  156. ///////////////////////////////////////////////////////////////////////////////////////
  157. /********************************************************
  158. *                                                      *
  159. *              DS1302时钟芯片程序段                    *
  160. *                                                      *
  161. ********************************************************/
  162. void DS1302_InputByte(uchar byte) //往DS1302写入1Byte数据
  163. {
  164.   uchar i;
  165.   uchar temp;
  166.   temp = byte;
  167. for(i=8; i>0; i--)
  168.      {
  169.      DS_IO = temp&0x01;
  170.      S_CLK;
  171.      C_CLK;
  172.      temp = temp >> 1;
  173.      }
  174. }

  175. uchar DS1302_OutputByte(void) //从DS1302读取1Byte数据
  176. {
  177.   uchar i;
  178.   uchar temp=0;

  179.   for(i=0; i<8;i++)
  180.   {
  181.     C_CLK;
  182.     if(DS_IO!=0) temp|=(1<<i);
  183.     S_CLK;
  184.   }
  185.   return(temp);
  186. }

  187. void RW_DS1302(uchar reg, uchar byte)//向DS1302寄存器写数据
  188. {
  189.   C_RST;
  190.   C_CLK;
  191.   S_RST;
  192.   DS1302_InputByte(reg);
  193.   DS1302_InputByte(byte);
  194.   S_CLK;
  195.   C_RST;
  196. }

  197. uchar Read_DS1302(uchar reg)//从DS1302寄存器读数据
  198. {
  199.   uchar byte;
  200.     C_RST;
  201.     C_CLK;
  202.     S_RST;
  203.   DS1302_InputByte(reg);
  204.   byte = DS1302_OutputByte();
  205.     S_CLK;
  206.     C_RST;
  207.   return(byte);
  208. }
  209. //////////////////////////////////////////////////////////////////
  210. //////////////////////////////////////////////转码
  211. uchar BCD_s(uchar BCDs)//十六进制转化为10进制
  212. {        
  213.         BCDs=(BCDs>>4)*10+(BCDs&0x0f);//将读出来的十六进制转化为10进制
  214.         delay(1);
  215.         return(BCDs);
  216. }

  217. uchar s_BCD(uchar sBCD)//10进制转化为十六进制
  218. {        
  219.         sBCD=((sBCD/10)<<4)+(sBCD%10);//将10进制转化为十六进制
  220.         delay(1);
  221.         return(sBCD);
  222. }
  223. /////////////////////////////////////////////////
  224. void gettime()
  225. {
  226.          time=Read_DS1302(0x85);//读小时
  227.          shi=BCD_s(time);

  228.          time=Read_DS1302(0x83);//读分
  229.          fen=BCD_s(time);

  230.          time=Read_DS1302(0x81);//读秒
  231.          time&=0x7f;//这句代码的意思是:因为秒的最高位第七位分为两种可能,一:它是0,如果是0的话,时钟会自动的计时,如果是1的话,时钟会停止走动,相当于中断
  232.          miao=BCD_s(time);//将分读出来的十六进制转化为10进制

  233.          time=Read_DS1302(0x89);//读月
  234.          yue=BCD_s(time);

  235.          time=Read_DS1302(0x87);//读日
  236.          ri=BCD_s(time);

  237.          time=Read_DS1302(0x8D);//读年
  238.          nian=BCD_s(time);

  239.          time=Read_DS1302(0x8b);//读zhou
  240.          zhou=BCD_s(time);

  241.          ds_time=Read_DS1302(0xc1);
  242.          k_fen=BCD_s(ds_time);        
  243.          ds_time=Read_DS1302(0xc9);
  244.          k_shi=BCD_s(ds_time);
  245.          ds_time=Read_DS1302(0xc5);
  246.          g_fen=BCD_s(ds_time);
  247.          ds_time=Read_DS1302(0xc7);
  248.          g_shi=BCD_s(ds_time);
  249. }

  250. /**************************红外接收处理***********************************
  251. **************************************************************************
  252. *************************************************************************/
  253. /*------------------------------------------------
  254.                   定时器0中断处理
  255. ------------------------------------------------*/
  256. void tim0_isr (void) interrupt 1 using 1
  257. {
  258.   irtime++;  //用于计数2个下降沿之间的时间

  259.   xs_tt++;
  260.   if(xs_tt==xstt){xs_tt=0;}        //亮度变量
  261.   if(xs_tt==1)
  262.           {        if(ss==1)
  263.                 {        if(ss2>=10)
  264.                         {        SMG_q=0;
  265.                                 SMG_b=0;
  266.                                 SMG_s=0;
  267.                                 SMG_g=0;
  268.                                 P0=0xFF;
  269.                         }
  270.                         else
  271.                         display(qi,ba,sh,ge);
  272.                 }
  273.                 else
  274.                 display(qi,ba,sh,ge);
  275.         }
  276.         else
  277.         P1=0xFF;

  278.   ss1++;
  279.           if(ss1==250)
  280.         {        ss1=0;
  281.                 ss2++;
  282.                 if(ss2==16)
  283.                 {        ss2=0;
  284.                 }
  285.         }
  286. }
  287. /*void tim1_isr (void) interrupt 3
  288. {
  289.   xs_tt++;
  290.   if(xs_tt==xstt){xs_tt=0;}

  291.   if(xs_tt==1)
  292.           {display();}
  293.         else
  294.         P0=0xFF;
  295. }*/
  296. /*------------------------------------------------
  297.                   外部中断0中断处理
  298. ------------------------------------------------*/
  299. void EX0_ISR (void) interrupt 2 //外部中断0服务函数
  300. {
  301.   static unsigned char  i;             //接收红外信号处理
  302.   static bit startflag;                //是否开始处理标志位

  303. if(startflag)                        
  304.    {
  305.     if(irtime<63&&irtime>=33)//引导码 TC9012的头码,9ms+4.5ms
  306.                         i=0;
  307.                     irdata[i]=irtime;//存储每个电平的持续时间,用于以后判断是0还是1
  308.                     irtime=0;
  309.                     i++;
  310.                             if(i==33)
  311.                               {
  312.                                    irok=1;
  313.                                  i=0;
  314.                                   }
  315.           }
  316.            else
  317.                 {
  318.                 irtime=0;
  319.                 startflag=1;
  320.                 }

  321. }

  322. /*------------------------------------------------
  323.                 定时器0初始化
  324. ------------------------------------------------*/
  325. void TIM0init(void)//定时器0初始化
  326. {

  327.   TMOD=0x02;//定时器0工作方式2,TH0是重装值,TL0是初值
  328.   TH0=0x00; //重载值
  329.   TL0=0x00; //初始化值
  330.   ET0=1;    //开中断
  331.   TR0=1;
  332. //  TH1=0x00; //重载值
  333. //  TL1=0x00; //初始化值
  334. //  TR1=1;
  335. //  ET1=1;    //开中断  
  336. }
  337. /*------------------------------------------------
  338.                   外部中断0初始化
  339. ------------------------------------------------*/
  340. void EX0init(void)
  341. {
  342. IT1 = 1;   //指定外部中断0下降沿触发,INT0 (P3.2)
  343. EX1 = 1;   //使能外部中断
  344. EA = 1;    //开总中断
  345. }
  346. /*------------------------------------------------
  347.                   键值处理
  348. ------------------------------------------------*/

  349. void Ir_work(void)//红外键值散转程序
  350. {                 
  351. //        uchar date;
  352.         if(SET==0){
  353.            switch(IRcord[2])//判断第三个数码值
  354.            {
  355.                         case 0x16:if(IRcord[3]==0xe9){
  356.                                 ddss=1;}
  357.                         break;//1 显示相应的按键值
  358.                          case 0x0c:if(IRcord[3]==0xf3){
  359.                                 xs=0;}
  360.                         break;//1 显示相应的按键值
  361.                         case 0x18:if(IRcord[3]==0xe7){
  362.                                 xs=1;}
  363.                         break;//1 显示相应的按键值
  364.                         case 0x5e:if(IRcord[3]==0xa1){
  365.                                 xs=2;}
  366.                         break;//1 显示相应的按键值
  367.                         case 0x0d:if(IRcord[3]==0xf2){
  368.                                 xstt=2;}
  369.                         break;//1 显示相应的按键值
  370.                         case 0x19:if(IRcord[3]==0xe6){
  371.                                 xstt=15;}
  372.                         break;//1 显示相应的按键值
  373. //                        case 0x0d:if(IRcord[3]==0xf2){
  374. //                                xstt=30;}
  375. //                        break;//1 显示相应的按键值
  376.                         case 0x09:if(IRcord[3]==0xf6){
  377.                                 SPK=~SPK;}
  378.                         break;//1 显示相应的按键值
  379.                         case 0x15:if(IRcord[3]==0xea){
  380.                                 dskg=1;}
  381.                         break;//1 显示相应的按键值
  382.                         case 0x07:if(IRcord[3]==0xf8){
  383.                                 dskg=0;}
  384.                         break;//1 显示相应的按键值

  385.             default:break;
  386. //                        IRcord[2]=0;
  387.                  }}
  388. /////////////////////////////////////////////////////////////////////////////////////////////////////
  389.         if(SET==0){
  390.                  if(IRcord[2]==0x42){
  391.                          if(IRcord[3]==0xbd){
  392.                                  RW_DS1302(0x8e,0x00);delay(10);//允许写操作
  393.                                 date=Read_DS1302(0x81);//先将秒读出来
  394.                                 date=0x80|date;//这句就是将秒置为最高位
  395.                                 RW_DS1302(0x80,date);delay(10);//再将秒写进去
  396.                                   RW_DS1302(0x8e,0x80);delay(10);//写完了关闭
  397.                                 SET=1;ss=1;}
  398.                                 IRcord[2]=0;
  399.                                 irpro_ok=0;}

  400.                   if(IRcord[2]==0x08){
  401.                         if(IRcord[3]==0xf7){
  402.                                 SET=4;}
  403.                                 IRcord[2]=0;
  404.                                 irpro_ok=0;}}

  405.         if(SET==1){
  406.                 if(IRcord[2]==0x42){
  407.                         if(IRcord[3]==0xbd){
  408.                                 SET=2;ss=1;
  409.                                 IRcord[2]=0;
  410.                                 irpro_ok=0;}}
  411.                                                 
  412.                 if(IRcord[2]==0x4a){
  413.                         if(IRcord[3]==0xb5){
  414.                                 RW_DS1302(0x8e,0x00);delay(10);
  415.                                 time=Read_DS1302(0x83);
  416.                                 time=BCD_s(time);//这是修改分的主要代码,好好研究就成了
  417.                                 time++;
  418.                                 if(time>59)
  419.                                 {
  420.                                         time=0;
  421.                                 }
  422.                                 time=s_BCD(time);
  423.                                 RW_DS1302(0x82,time);
  424.                                 RW_DS1302(0x8e,0x80);
  425.                                 IRcord[2]=0;
  426.                                 irpro_ok=0;}}                        

  427.                 if(IRcord[2]==0x52){               
  428.                          if(IRcord[3]==0xad){
  429.                                 RW_DS1302(0x8e,0x00);delay(10);
  430.                                 time=Read_DS1302(0x85);
  431.                                 time=BCD_s(time);
  432.                                 time++;
  433.                                 if(time>23)
  434.                                 {
  435.                                         time=0;
  436.                                 }
  437.                                 time=s_BCD(time);
  438.                                 RW_DS1302(0x84,time);
  439.                                 RW_DS1302(0x8e,0x80);
  440.                                 IRcord[2]=0;
  441.                                 irpro_ok=0;}}}

  442.         if(SET==2){
  443.                  if(IRcord[2]==0x42){
  444.                         if(IRcord[3]==0xbd){
  445.                                 SET=3;ss=1;
  446.                                 IRcord[2]=0;
  447.                                 irpro_ok=0;}}

  448.                  if(IRcord[2]==0x4a){
  449.                         if(IRcord[3]==0xb5){
  450.                                 RW_DS1302(0x8e,0x00);delay(10);
  451.                                 time=Read_DS1302(0x87);
  452.                                 time=BCD_s(time);//这是修改分的主要代码,好好研究就成了
  453.                                 time++;
  454.                                 if(time>31)
  455.                                 {
  456.                                         time=1;
  457.                                 }
  458.                                 time=s_BCD(time);
  459.                                 RW_DS1302(0x86,time);
  460.                                 RW_DS1302(0x8e,0x80);
  461.                                 IRcord[2]=0;
  462.                                 irpro_ok=0;}}

  463.                   if(IRcord[2]==0x52){
  464.                          if(IRcord[3]==0xad){
  465.                                 RW_DS1302(0x8e,0x00);delay(10);
  466.                                 time=Read_DS1302(0x89);
  467.                                 time=BCD_s(time);
  468.                                 time++;
  469.                                 if(time>12)
  470.                                 {
  471.                                         time=1;
  472.                                 }
  473.                                 time=s_BCD(time);
  474.                                 RW_DS1302(0x88,time);
  475.                                 RW_DS1302(0x8e,0x80);        
  476.                                 IRcord[2]=0;
  477.                                 irpro_ok=0;}}
  478.                  }
  479.         if(SET==3){
  480.                  if(IRcord[2]==0x42){
  481.                         if(IRcord[3]==0xbd){
  482.                                 RW_DS1302(0x8e,0x00);
  483.                                  date=Read_DS1302(0x81);
  484.                                  date&=0x7f;//将秒的最高位置零表示重新开始工作
  485.                                  RW_DS1302(0x80,date);
  486.                                  RW_DS1302(0x8e,0x80);
  487.                                 SET=0;
  488.                                 ss=0;}
  489.                                 IRcord[2]=0;
  490.                                 irpro_ok=0;}

  491.                  if(IRcord[2]==0x52){
  492.                         if(IRcord[3]==0xad){
  493.                                 RW_DS1302(0x8e,0x00);delay(10);
  494.                                 time=Read_DS1302(0x8D);
  495.                                 time=BCD_s(time);
  496.                                 time++;
  497.                                 if(time>99)
  498.                                 {
  499.                                          time=0;
  500.                                 }
  501.                                 time=s_BCD(time);
  502.                                 RW_DS1302(0x8C,time);
  503.                                 RW_DS1302(0x8e,0x80);
  504.                                 IRcord[2]=0;
  505.                                 irpro_ok=0;}}

  506.                   if(IRcord[2]==0x4a){
  507.                         if(IRcord[3]==0xb5){
  508.                                 RW_DS1302(0x8e,0x00);delay(10);
  509.                                 time=Read_DS1302(0x8B);
  510.                                 time=BCD_s(time);
  511.                                 time++;
  512.                                 if(time>7)
  513.                                 {
  514.                                         time=1;
  515.                                 }
  516.                                 time=s_BCD(time);
  517.                                 RW_DS1302(0x8A,time);
  518.                                 RW_DS1302(0x8e,0x80);
  519.                                 IRcord[2]=0;
  520.                                 irpro_ok=0;}}        
  521.                  }
  522. /*        if(SET==4){
  523.                   if(IRcord[2]==0x42){
  524.                         if(IRcord[3]==0xbd){
  525.                                 RW_DS1302(0x8e,0x00);
  526.                                  date=Read_DS1302(0x81);
  527.                                  date&=0x7f;//将秒的最高位置零表示重新开始工作
  528.                                  RW_DS1302(0x80,date);
  529.                                  RW_DS1302(0x8e,0x80);
  530.                                 SET=0;
  531.                                 ss=0;}
  532.                                 IRcord[2]=0;
  533.                                 irpro_ok=0;}} */

  534.         if(SET==4){
  535.                    if(IRcord[2]==0x08){
  536.                         if(IRcord[3]==0xf7){
  537.                                 SET=5;ss=1;
  538.                                 IRcord[2]=0;
  539.                                 irpro_ok=0;}}
  540.                
  541.                   if(IRcord[2]==0x5a){
  542.                         if(IRcord[3]==0xa5){
  543.                                 RW_DS1302(0x8e,0x00);delay(10);
  544.                                 ds_time=Read_DS1302(0xc1);
  545.                                 ds_time=BCD_s(ds_time);
  546.                                 ds_time++;
  547.                                 if(ds_time>59)
  548.                                 {
  549.                                          ds_time=0;
  550.                                 }
  551.                                 ds_time=s_BCD(ds_time);
  552.                                 RW_DS1302(0xc0,ds_time);
  553.                                 RW_DS1302(0x8e,0x80);
  554.                                 IRcord[2]=0;
  555.                                 irpro_ok=0;}}

  556.                   if(IRcord[2]==0x1c){
  557.                         if(IRcord[3]==0xe3){
  558.                                  RW_DS1302(0x8e,0x00);delay(10);
  559.                                  ds_time=Read_DS1302(0xc9);
  560.                                  ds_time=BCD_s(ds_time);
  561.                                  ds_time++;
  562.                                  if(ds_time>23)
  563.                                  {
  564.                                      ds_time=0;
  565.                                  }
  566.                                  ds_time=s_BCD(ds_time);
  567.                                  RW_DS1302(0xc8,ds_time);
  568.                                  RW_DS1302(0x8e,0x80);        
  569.                                 IRcord[2]=0;
  570.                                 irpro_ok=0;}}}

  571.         if(SET==5){
  572.                    if(IRcord[2]==0x08){
  573.                         if(IRcord[3]==0xf7){
  574.                                 SET=0;ss=0;}
  575.                                 IRcord[2]=0;
  576.                                 irpro_ok=0;}

  577.                   if(IRcord[2]==0x5a){
  578.                         if(IRcord[3]==0xa5){
  579.                                 RW_DS1302(0x8e,0x00);delay(10);
  580.                                 ds_time=Read_DS1302(0xc5);
  581.                                 ds_time=BCD_s(ds_time);
  582.                                 ds_time++;
  583.                                 if(ds_time>59)
  584.                                 {
  585.                                     ds_time=0;
  586.                                 }
  587.                                 ds_time=s_BCD(ds_time);
  588.                                 RW_DS1302(0xc4,ds_time);
  589.                                 RW_DS1302(0x8e,0x80);}
  590.                                 IRcord[2]=0;
  591.                                 irpro_ok=0;}

  592.                   if(IRcord[2]==0x1c){
  593.                         if(IRcord[3]==0xe3){
  594.                                 RW_DS1302(0x8e,0x00);delay(10);
  595.                                 ds_time=Read_DS1302(0xc7);
  596.                                 ds_time=BCD_s(ds_time);
  597.                                 ds_time++;
  598.                                 if(ds_time>23)
  599.                                 {
  600.                                     ds_time=0;
  601.                                 }
  602.                                 ds_time=s_BCD(ds_time);
  603.                                 RW_DS1302(0xc6,ds_time);
  604.                                 RW_DS1302(0x8e,0x80);        }
  605.                                 IRcord[2]=0;
  606.                                 irpro_ok=0;}}

  607.                    irpro_ok=0;//处理完成标志        
  608.   }
  609. /*------------------------------------------------
  610.                 红外码值处理
  611. ------------------------------------------------*/
  612. void Ircordpro(void)//红外码值处理函数
  613. {
  614.   unsigned char i, j, k;
  615.   unsigned char cord,value;

  616.   k=1;
  617.   for(i=0;i<4;i++)      //处理4个字节
  618.      {
  619.       for(j=1;j<=8;j++) //处理1个字节8位
  620.          {
  621.           cord=irdata[k];
  622.           if(cord>7)//大于某值为1,这个和晶振有绝对关系,这里使用12M计算,此值可以有一定误差
  623.              value|=0x80;
  624.           if(j<8)
  625.                     {
  626.                          value>>=1;
  627.                         }
  628.            k++;
  629.          }
  630.      IRcord[i]=value;
  631.      value=0;     
  632.      }
  633.          irpro_ok=1;//处理完毕标志位置1
  634. }


  635. /**************************红外接收处理***********************************
  636. **************************************************************************
  637. *************************************************************************/

  638. //主函数,C语言的入口函数:        
  639. void main()
  640. {
  641.         init();                //初始化中断控制寄存器

  642.         EX0init(); //初始化外部中断
  643.         TIM0init();//初始化定时器

  644.         while(1)
  645.         {
  646.           if(ss2==2)
  647.                 gettime();
  648. /////////////////////////////////////
  649.          if(irok)                        //如果接收好了进行红外处理
  650.           {   
  651.            Ircordpro();
  652.             irok=0;
  653.           }
  654.             if(irpro_ok)                   //如果处理好后进行工作处理,如按对应的按键后显示对应的数字等
  655.           {
  656.            Ir_work();
  657.             }
  658. //////////////////////////////////////////
  659.         if(dskg==1)
  660.         {        if(shi==k_shi)
  661.                 {        if(fen==k_fen)
  662.                         {        SPK=0;        }
  663.                 }
  664.                 if(shi==g_shi)
  665.                 {        if(fen==g_fen)
  666.                         {        SPK=1;        }
  667.                 }
  668.         }

  669. /*        if(SET==0)
  670.         {        qi=shi;
  671.                 ba=shi;
  672.                 sh=fen;
  673.                 ge=fen;
  674.                 if(ss2>=8)
  675.                 {        LED=0;}
  676.                 else
  677.                 LED=1;
  678.         }*/
  679.         if(SET==1)
  680.         {        qi=shi;
  681.                 ba=shi;
  682.                 sh=fen;
  683.                 ge=fen;
  684.                 LED=0;}
  685.         if(SET==2)
  686.         {        qi=yue;
  687.                 ba=yue;
  688.                 sh=ri;
  689.                 ge=ri;
  690.                 LED=1;}
  691.         if(SET==3)
  692.         {        qi=nian;
  693.                 ba=nian;
  694.                 sh=0;
  695.                 ge=zhou;
  696.                 LED=1;}
  697. /*        if(SET==4)
  698.         {        qi=shi;
  699.                 ba=shi;
  700.                 sh=fen;
  701.                 ge=fen;
  702.                 LED=1;}*/
  703.         if(SET==4)
  704.         {        ss=1;
  705.                 qi=k_shi;
  706.                 ba=k_shi;
  707.                 sh=k_fen;
  708.                 ge=k_fen;
  709.                 if(ss2>=8)
  710.                 {        LED=0;}
  711.                 else
  712.                 LED=1;
  713.         }
  714.         if(SET==5)
  715.         {        ss=1;        
  716.                 qi=g_shi;
  717.                 ba=g_shi;
  718.                 sh=g_fen;
  719.                 ge=g_fen;
  720.                 if(ss2>=8)
  721.                 {        LED=0;}
  722.                 else
  723.                 LED=1;
  724.         }
  725.         if(SET==0){
  726.         if(xs==0)
  727.         {        qi=shi;
  728.                 ba=shi;
  729.                 sh=fen;
  730.                 ge=fen;
  731.                 LED=1;
  732.                 if(ss2>=8)
  733.                 {        LED=0;}
  734.                 else
  735.                 LED=1;}
  736.         if(xs==1)
  737.         {        qi=yue;
  738.                 ba=yue;
  739.                 sh=ri;
  740.                 ge=ri;
  741.                 LED=1;}
  742.         if(xs==2)
  743.         {        qi=zhou;
  744.                 ba=zhou;
  745.                 sh=miao;
  746.                 ge=miao;
  747.                 LED=1;}        }

  748.         if(dskg==1)
  749.         {        LED1=0;}
  750.         if(dskg==0)
  751.         {        LED1=1;}
  752.         if(ddss==0)
  753.         {        if(ss2>=8)
  754.                 {        LED1=0;}
  755.                 else
  756.                 LED1=1;
  757.         }
  758.         if(shi==7)
  759.         {        xstt=2;}
  760.         if(shi==19)
  761.         {        xstt=15;}

  762. ////////////////////////////////////
  763.         if(zhou==6)
  764.         {        ts();}
  765. ////////////////////////////
  766.         }
  767. }
  768. //////////////////////////////////////////
  769. ////////////////////////////////////////////时间校准
  770. void ts()
  771. {
  772.         if(shi==0)
  773.         {        if(fen==0)
  774.                 {        if(miao==30)//0x30是30秒
  775.                         {        ts1=1;
  776.                         }
  777.                 }
  778.         }
  779. //////////////////////////////////////////////////
  780.         if(ts1==1)
  781.         {        if(shi==1)
  782.                 {        if(fen==1)
  783.                         {        if(miao==15)
  784.                                 {        ts1=0;
  785.                                         RW_DS1302(0x8e,0x00);delay(10);
  786.                                         RW_DS1302(0x80,0x31);delay(10);//0x80写秒0x30就是31秒
  787.                                     RW_DS1302(0x8e,0x80);delay(10);               
  788.                                 }
  789.                         }
  790.                 }
  791.         }
  792. }
复制代码


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

使用道具 举报

沙发
ID:112693 发表于 2018-1-9 18:44 | 只看该作者
膜拜了,向楼主学习!!!!!!!!!!!!!
回复

使用道具 举报

板凳
ID:97811 发表于 2018-6-8 09:02 | 只看该作者
谢谢分享!
回复

使用道具 举报

地板
ID:347571 发表于 2018-6-8 14:20 | 只看该作者

谢谢分享!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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