原创 DS1302的测试程序

2010-3-22 23:02 1953 2 2 分类: MCU/ 嵌入式

 /**************************************************
                 // DS1302的测试程序。


                  year="01";
                  DS1302_WriteYear(year);
                  month="02";
                  DS1302_WriteMonth(month);
                  day="03";
                  DS1302_WriteDay(day);
                  hour="04";
                  DS1302_WriteHour(hour);
                  minute="05";
                  DS1302_WriteMinute(minute);
                  second="06";
                  DS1302_WriteSecond(second);
                 /**************从ds1302中 取出数据
                  //写允许
                  year="DS1302"_ReadYear();
                  month="DS1302"_ReadMonth();
                  day="DS1302"_ReadDay();
                  hour="DS1302"_ReadHour();
                  minute="DS1302"_ReadMinute();
                  second="DS1302"_ReadSecond();
                  DS1302_WriteDisable();        //写禁止
  ************************************************************/


 /****************  时钟芯片 DS1302 子程序库 **************/
 //初次上电,要打开振荡器工作,CH=0.
 // 每次写入数据,要先打开写保护,wp=0;
 //            写完后,关死写保护,wp=1;


 


 /**************发送一字节,**********************/
  void DSSend_byte(uchar Command,uchar DSdata)
         { uchar i,j;
            DS_RST=0;
            _nop_();
            _nop_();
            _nop_();
            _nop_();
            DS_SCLK=0;
            _nop_();
            DS_RST=1;
            _nop_();
            i="8";
           do
                   { j="Command";
                     if ((j&0x01)==0x01)
                        { DS_IO=1;
                        }
                     else
                        {DS_IO=0;
                        }
                     Command="Command">>1;


                    DS_SCLK=1;
                    _nop_();
                    DS_SCLK=0;
                    i--;
                    }while(i!=0);
           i="8";
           do
                    { j="DSdata";         //判断数据的最低位。
                      if ((j&0x01)==1)
                         { DS_IO=1;
                         }
                       else
                         {DS_IO=0;
                         }
                       DSdata="DSdata">>1;


                       DS_SCLK=1;
                       _nop_();
                       DS_SCLK=0;
                       i--;
                    }while(i!=0);
           DS_RST=0;


         }


/***************接收一字节 *******************/
  uchar DSRecive_byte(uchar Command)
          {uchar i,DSdata,j;
           DS_RST=0;
           _nop_();
           DS_SCLK=0;
           _nop_();
           DS_RST=1;
           i="8";
           do
                   {
                    j="Command";
                    if ((j&0x01)==1)    //发送先发送低位。
                       {DS_IO=1;
                       }
                    else
                       {DS_IO=0;
                       }
                    Command="Command">>1;


                    DS_SCLK=1;
                    _nop_();
                    DS_SCLK=0;
                    i--;
                   }while(i!=0);
           i="8";
           do
                   {
                    if (DS_IO==1)
                            {DSdata=DSdata>>1;
                             DSdata="DSdata|0x80";  // 最高位加1
                            }
                    else
                            {DSdata>>=1;
                            }
                    DS_SCLK=1;
                    _nop_();
                    DS_SCLK=0;
                    i--;
                   }while(i!=0);
           return(DSdata);
          }


  /************** DS1302 Write disable (WP=1)  ***/
  void DS1302_WriteDisable(void)
            {uchar i,j;
             i="0x8e";j=0x80;
             DSSend_byte(i,j);
            }
  /************** DS1302 Write enble (WP=0) ******/
  void DS1302_WriteEnble(void)
            {uchar i,j;
             i="0x8e";j=0x0;
             DSSend_byte(i,j);
            }
  /************** DS1302 Charge Disable **********/
  void DS1302_ChargeDisable(void)
            {uchar i,j;
             i="0x90";j=0x50;
             DSSend_byte(i,j);
            }


  /************** DS1302 Charge enble *** 一个二极管,2kn *********/
  void DS1302_ChargeEnble(void)
             {uchar i,j;
              i="0x90";j=0xa5;
              DSSend_byte(i,j);
             }


  /************** DS1302 Single byte Mode*********/
  // 当命令字为be 或bf时,ds1302工作在多字节传送模式。
  /************** DS1302 Burst byte Mode *********/



  /************** DS1302 Timer enble ****振荡器工作允许*********/
   void DS1302_OscillatorEnble(void)
             {uchar i,j;
              i="0x80";j=0;
              DSSend_byte(i,j);
             }


  /************** DS1302 Timer disable ***禁止振荡器工作********/
  void DS1302_OscillatorDisable(void)
             {uchar i,j;
              i="0x80";j=0x80;
              DSSend_byte(i,j);
             }


  /************** DS1302 write year **************/
  void DS1302_WriteYear(uchar value)
          { uchar i;
            i="0x8c";
            DSSend_byte(i,value);
          }
  /************** DS1302 read year ***************/
 uchar DS1302_ReadYear()
          {uchar i,j;
           i="0x8d";
           j="DSRecive"_byte(i);
           return(j);
          }
  /************** DS1302 write month *************/
  void DS1302_WriteMonth(uchar value)
           {uchar i;
            i="0x88";
            DSSend_byte(i,value);
           }
  /************** DS1302 read month **************/
  uchar DS1302_ReadMonth(void)
           {uchar i;
            i="0x89";
            i="DSRecive"_byte(i);
            return(i);
           }
  /************** DS1302 write day ***************/
  void DS1302_WriteDay(uchar value)
           {uchar i;
            i="0x86";
            DSSend_byte(i,value);
            }
  /************** DS1302 read day ****************/
  uchar DS1302_ReadDay(void)
           {uchar i;
            i="0x87";
            i="DSRecive"_byte(i);
            return(i);
           }
  /************** DS1302 write hour **************/
  void DS1302_WriteHour(uchar value)
           {uchar i;
            i="0x84";
            DSSend_byte(i,value);
           }
  /************** DS1302 read hour ***************/
  uchar DS1302_ReadHour(void)
           {uchar i;
            i="0x85";
            i="DSRecive"_byte(i);
            return(i);
           }
  /************** DS1302 write minute ************/
  void DS1302_WriteMinute(uchar value)
           {uchar i;
            i="0x82";
            DSSend_byte(i,value);
           }
  /************** DS1302 read minute *************/
 uchar DS1302_ReadMinute(void)
          {uchar i;
           i="0x83";
           i="DSRecive"_byte(i);
           return(i);
          }
  /************** DS1302 write second ************/
  void DS1302_WriteSecond(uchar value)
          {uchar i;
           i="0x80";
           DSSend_byte(i,value);


          }
  /************** DS1302 read second *************/
 uchar DS1302_ReadSecond(void)
         {uchar i;
          i="0x81";
          i="DSRecive"_byte(i);
          return(i);
         }

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
2
关闭 站长推荐上一条 /3 下一条