⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1302.lst

📁 零基础单片机C语言设计全套教程
💻 LST
📖 第 1 页 / 共 3 页
字号:
 145   3                      default:
 146   3                              printf("Please Input the Right Option");
 147   3              }
 148   2         }
 149   1      exit:   printf("Exit the program");       
 150   1      }
 151          
 152          
 153          
 154          
 155          
 156          void ResetDS1302()                                                      //复位子函数
 157          {
 158   1         SCLK = 0;
 159   1         RSTB = 0;                                                                    //复位
 160   1         RSTB = 1;
 161   1      }
 162          
 163          uchar ReadByteDS1302()                                                  //字节读取子函数
 164          {
 165   1      uchar i;
 166   1      uchar RByte;
 167   1      uchar TempByte;
 168   1      
 169   1              RByte = 0x00;                                                           //初始化
 170   1              I_O = 1;
 171   1              for(i=0; i<8; ++i)                                                      //逐位读取字节数据
 172   1      {
 173   2                      SCLK = 1;                                                               //构造时钟
 174   2                      SCLK = 0;
 175   2                      TempByte = (uchar)I_O;
 176   2                      TempByte = TempByte <<7;                                //移位
 177   2                      RByte = RByte >> 1;
 178   2                      RByte |= TempByte; 
 179   2              }
C51 COMPILER V8.08   DS1302                                                                08/29/2008 18:01:54 PAGE 4   

 180   1              return RByte;                                                           //返回结果
 181   1      }
 182          
 183          void WriteByteDS1302(uchar W_Byte)                              //字节写入子函数
 184          {
 185   1      uchar i;
 186   1              for(i = 0; i < 8; ++i)                                                  //循环逐位写入
 187   1      {
 188   2                      I_O = 0;
 189   2                      if(W_Byte & 0x01) 
 190   2                              I_O = 1;
 191   2                      SCLK = 0;                                                               //时钟操作
 192   2                      SCLK = 1;
 193   2                      W_Byte = W_Byte >>1;                                    //移位
 194   2          }
 195   1      }
 196          
 197          void SetYear()                                                                  //年设置子函数
 198          {
 199   1              uchar   year;
 200   1              printf("\nPlease Enter the year (0-99): ");                     //输入年
 201   1              scanf("%bx", &year);
 202   1              ResetDS1302();                                                  //复位DS1302
 203   1              WriteByteDS1302(0x06);                                          //年寄存器地址
 204   1              WriteByteDS1302(year);                                          //写入年
 205   1              ResetDS1302();                                                  //复位DS1302
 206   1      }
 207          
 208          void SetMonth()                                                                 //月设置子函数
 209          {
 210   1              uchar   month;
 211   1              printf("\n Please Enter the month (1-12): ");           //输入月
 212   1              scanf("%bx", &month);
 213   1              ResetDS1302();                                                  //复位DS1302
 214   1              WriteByteDS1302(0x04);                                          //月寄存器地址
 215   1              WriteByteDS1302(month);                                 //写入月
 216   1              ResetDS1302();                                                  //复位DS1302
 217   1      }
 218          
 219          void SetDate()                                                                  //日设置子函数
 220          {
 221   1              uchar   date;
 222   1              printf("\n Please Enter the date (1-31): ");                    //输入日
 223   1              scanf("%bx", &date);    
 224   1              ResetDS1302();                                                  //复位DS1302
 225   1              WriteByteDS1302(0x03);                                          //日寄存器地址
 226   1              WriteByteDS1302(date);                                          //写入日
 227   1              ResetDS1302();                                                  //复位DS1302
 228   1      }
 229          
 230          void SetDay()                                                                   //星期设置子函数
 231          {
 232   1              uchar   day;
 233   1              printf("\n Please Enter the day (1-7): ");                      //输入星期
 234   1              scanf("%bx", &day);
 235   1              ResetDS1302();                                                  //复位DS1302
 236   1              WriteByteDS1302(0x05);                                          //星期寄存器地址
 237   1              WriteByteDS1302(day);                                           //写入星期
 238   1              ResetDS1302();                                                  //复位DS1302
 239   1      }
 240          
 241          void SetHour()                                                                  //小时设置子函数
C51 COMPILER V8.08   DS1302                                                                08/29/2008 18:01:54 PAGE 5   

 242          {
 243   1              uchar   hour;
 244   1              printf("\n Please Enter the hour (1-24): ");                    //输入小时
 245   1              scanf("%bx", &hour);
 246   1              hour =hour & 0x3f;                                                      //设置时钟为24小时方式
 247   1              ResetDS1302();                                                  //复位DS1302
 248   1              WriteByteDS1302(0x02);                                          //小时寄存器地址
 249   1              WriteByteDS1302(hour);                                          //写入小时
 250   1              ResetDS1302();                                                  //复位DS1302
 251   1      }
 252          
 253          void SetMinute ()                                                               //分钟设置子函数
 254          {
 255   1              uchar   minute;
 256   1              printf("\n Please Enter the minute (0-59): ");          //输入分钟
 257   1              scanf("%bx", &minute);
 258   1              ResetDS1302();                                                  //复位DS1302
 259   1              WriteByteDS1302(0x01);                                          //分钟寄存器地址
 260   1              WriteByteDS1302(minute);                                        //写入分钟
 261   1              ResetDS1302();                                                  //复位DS1302
 262   1      }
 263          
 264          void SetSecond ()                                                               //秒设置子函数
 265          {
 266   1              uchar   second;
 267   1              printf("\n Please Enter the second (0-59): ");          //输入秒
 268   1              scanf("%bx", &second);
 269   1              ResetDS1302();                                                  //复位DS1302
 270   1              WriteByteDS1302(0x00);                                          //秒寄存器地址
 271   1              WriteByteDS1302(second);                                        //写入秒
 272   1              ResetDS1302();                                                  //复位DS1302
 273   1      }
 274          
 275          void DisableWrite ()                                                            //写保护子函数
 276          {
 277   1              ResetDS1302();                                                  //复位DS1302
 278   1              WriteByteDS1302(0x8e);                                          //写保护控制寄存器
 279   1              WriteByteDS1302(0x80);                                          //禁止写入
 280   1              ResetDS1302();                                                  //复位DS1302
 281   1      }
 282          
 283          void EnableWrite ()                                                             //写允许子函数
 284          {
 285   1              ResetDS1302();                                                  //复位DS1302
 286   1              WriteByteDS1302(0x8e);                                          //写保护控制寄存器
 287   1              WriteByteDS1302(0);                                             //允许写入
 288   1              ResetDS1302();                                                  //复位DS1302
 289   1      }
 290          
 291          void Charge ()                                                                  //充电控制子函数
 292          {
 293   1              ResetDS1302();                                                  //复位DS1302
 294   1              WriteByteDS1302(0x90);                                          //涓流充电寄存器
 295   1              WriteByteDS1302(0xab);                                          //允许充电
 296   1              ResetDS1302();                                                  //复位DS1302
 297   1      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -