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

📄 clock.lst

📁 单片机用DS1302进行计时
💻 LST
字号:
C51 COMPILER V7.06   CLOCK                                                                 02/10/2009 21:12:43 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE CLOCK
OBJECT MODULE PLACED IN clock.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE clock.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          #include<reg52.h>
   2          #include<string.h>
   3          #define uchar unsigned char
   4          #define uint unsigned int 
   5          sbit rs=P1^0;
   6          sbit rw=P1^1;
   7          sbit lcden=P1^2;
   8          
   9          sbit sclk=P3^7;
  10          sbit io=P3^5;
  11          sbit rst=P3^6;
  12          void delay(uint z)
  13          {
  14   1              uint x;
  15   1              for(;z>0;z--)
  16   1                      for(x=10;x>0;x--)
  17   1                      ;
  18   1      }
  19          void write_com(uchar command)
  20          {
  21   1              lcden=0;
  22   1              rs=0;
  23   1              rw=0;
  24   1              P0=command;
  25   1              lcden=1;
  26   1              delay(1);
  27   1              lcden=0;
  28   1              
  29   1      }
  30          void write_data(uchar date)
  31          {
  32   1              lcden=0;
  33   1              rs=1;
  34   1              rw=0;
  35   1              P0=date;
  36   1              lcden=1;
  37   1              delay(1);
  38   1              lcden=0;
  39   1      }
  40          
  41          void init()
  42          {
  43   1              write_com(0x38);
  44   1              write_com(0x0c);
  45   1              write_com(0x06);
  46   1      }
  47          
  48          /*****************************************
  49          函数名称:write_byte
  50          功能:向DS1302写入一个字节数据(无RST操作)
  51          参数:byte 要写入数据
  52          返回值:无
  53          *****************************************/
  54          
  55          void write_byte(uchar byte)
C51 COMPILER V7.06   CLOCK                                                                 02/10/2009 21:12:43 PAGE 2   

  56          {
  57   1              uchar i;                //8位数据计数
  58   1      
  59   1              for(i=0;i<8;i++)
  60   1              {
  61   2                      sclk=0;                 //拉低时钟信号
  62   2                      io=byte&0x01;   //给数据最低位
  63   2                      delay(1);               //调整时钟和脉冲宽度
  64   2                      sclk=1;                 //提供时钟上升沿
  65   2                      byte=byte>>1;   //数据右移一位,为送出新数据做准备
  66   2              }
  67   1      }
  68          
  69          uchar read_byte(void)           //读一个字节
  70          {
  71   1              uchar i,byte=0;
  72   1              for(i=0;i<8;i++)
  73   1              {
  74   2                      byte>>=1;               //保存输出数据
  75   2                      sclk=1;                 //时钟是高电平
  76   2                      delay(1);               //延时,调整时钟脉冲宽度
  77   2                      sclk=0;                 //时钟下降沿,准备读数据位
  78   2                      delay(1);
  79   2                      if(io==1)               //读出数据位为1,给最高位置1
  80   2                      byte=byte|0x80;
  81   2                      if(io==0)               //读出数据位为0,给最高位置0
  82   2                      byte=byte&0x7f; 
  83   2              }
  84   1              return byte;            //返回读出数据
  85   1      }
  86          
  87          void write_D(uchar addr,uchar date)             //向某个地址写入一个字节的数据
  88          {
  89   1              rst=0;                  //拉低片选端
  90   1              sclk=0;                 //拉低时钟端
  91   1              delay(1);
  92   1              rst=1;                  //拉高片选端
  93   1              delay(1);
  94   1              write_byte(addr);               //写入操作命令(地址)
  95   1              delay(1);
  96   1              sclk=0;                 //拉低时钟端端
  97   1              delay(1);
  98   1              write_byte(date);               //写入数据
  99   1              sclk=0;                 //拉低时钟端
 100   1              delay(1);
 101   1              rst=0;          //拉低片选端
 102   1      }
 103          
 104          /*uchar read_D(uchar addr)              //在某地址读一个字节数据
 105          {
 106                  uchar date;
 107                  rst=0;                  //拉低片选端
 108                  sclk=0;                 //拉低时钟端
 109                  delay(1);
 110                  rst=1;                  //拉高片选端
 111                  delay(1);
 112                  write_byte(addr);       //写入操作命令(地址)
 113                  delay(1);
 114                  date=read_byte;         //读出数据
 115                  delay(1);
 116                  sclk=0;                         //拉低时钟端
 117                  rst=0;                          //拉低片选端
C51 COMPILER V7.06   CLOCK                                                                 02/10/2009 21:12:43 PAGE 3   

 118                  return date;            //返回读出数据
 119          
 120          }*/
 121          void set_time(uchar ptime[])            //设置时间
 122          {
 123   1              uchar i;
 124   1          uchar addr=0x80;
 125   1              write_D(0x8e|0x00,0x00);                //写入控制命令,WP为0,允许写操作
 126   1              delay(200);                                     //稍长延时
 127   1              for(i=0;i<7;i++)
 128   1              {
 129   2                      write_D(addr|0x00,ptime[i]);            //写入秒 分 时 日 月 星期 年
 130   2                      addr=addr+2;                                    //地址加2
 131   2                      delay(10);
 132   2              }
 133   1              write_D(0x8e|0x00,0x80);                        //写控制命令,WP为1
 134   1      }
 135          
 136          void get_time(uchar time[])             //读取当前时间
 137          {
 138   1              uchar i;
 139   1      /****************************************
 140   1      //单次读写      
 141   1              uchar addr=0x80;
 142   1              for(i=0;i<7;i++)
 143   1              {
 144   1                      time[i]=read_D(addr|0x00);              //秒 分 时 日 月 星期 年
 145   1                      addr=addr+2;
 146   1              }
 147   1      *******************************************/
 148   1      //多字节读取
 149   1              rst=0;          //拉低片选
 150   1              delay(1);
 151   1              rst=1;          //拉高片选
 152   1              delay(1);
 153   1              write_byte(0xbf);               //写入时钟多字节读取命令 0xbf
 154   1              for (i=0;i<8;i++)       //时间数据的存放格式是:
 155   1          {                        //秒,分,时,日,月,星期,年,控制
 156   2              time[i]=read_byte(); //【7个数据(BCD格式)+1个控制】 
 157   2          }
 158   1              rst=0;  //拉低片选
 159   1      //
 160   1              sclk=0;         //拉低时钟端
 161   1      
 162   1      }
 163          
 164          void DS_init()  //初始化DS1302
 165          {
 166   1              write_D(0x8e|0x00,0x00);                //写入写允许命令
 167   1              write_D(0x80|0x00,0x00);                //启动振荡器,DS1302开始工作
 168   1              write_D(0x8e|0x00,0x80);                //写控制命令,不允许写操作
 169   1      
 170   1      }
 171          
 172          void bcd_asc(uchar bcd,uchar pasc[])
 173          {
 174   1              pasc[0]=bcd/16|0x30;    //转换十位
 175   1              pasc[1]=bcd&0x0f|0x30;          //转换个位
 176   1      }
 177          
 178          void disp(uchar time[])
 179          {
C51 COMPILER V7.06   CLOCK                                                                 02/10/2009 21:12:43 PAGE 4   

 180   1              uchar i,asc[2];
 181   1              uchar line1[11]={0,0,'-',0,0,'-',0,0,' ',0,'\0'};//显示第1行的字符数组
 182   1              uchar line2[9]={0,0,':',0,0,':',0,0,'\0'};                //显示第2行的字符数组
 183   1              for(i=0;i<3;i++)                //
 184   1              {
 185   2                      bcd_asc(time[2-i],asc);
 186   2                      line2[i*3]=asc[0];
 187   2                      line2[i*3+1]=asc[1];
 188   2              }
 189   1              bcd_asc(time[6],asc);
 190   1              line1[0]=asc[0];
 191   1              line1[1]=asc[1];
 192   1              bcd_asc(time[4],asc);                                             //为第1行的月赋值
 193   1              line1[3]=asc[0];
 194   1              line1[4]=asc[1];
 195   1              bcd_asc(time[3],asc);                                             //为第1行的日赋值
 196   1              line1[6]=asc[0];
 197   1              line1[7]=asc[1];
 198   1              bcd_asc(time[5],asc);                                             //为第1行的星期赋值
 199   1              line1[9]=asc[1];
 200   1      
 201   1              write_com(0x80+0x01);
 202   1              for(i=0;i<strlen(line1);i++)
 203   1              {
 204   2                      write_data(line1[i]);
 205   2              }                                                                                       //显示第一行
 206   1              write_com(0x80+0x44);
 207   1              for(i=0;i<strlen(line2);i++)
 208   1              {
 209   2                      write_data(line2[i]);
 210   2              }                                                                                       //显示第二行
 211   1      
 212   1      
 213   1      }
 214          void main()
 215          {
 216   1      
 217   1      
 218   1              
 219   1      //      uchar setadd,setdat,shift;        //setadd指定将当前数值送入DS1302的哪个寄存器
 220   1                                                                        //setdat是当前设置的数值,即被送入DS1302指定寄存器的数
 221   1                                                                //shift来实现十位和各位的设置相互独立(因为十位和个位是在一个寄存器里的)
 222   1       //uchar dis_x,dis_y;                     //存储1602液晶当前光标的位置
 223   1       uchar settime[7]={0x50,0x27,0x19,0x05,0x02,0x06,0x09};//设置的秒,分,时,日,月,星期,年
 224   1       uchar gettime[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00};//保存当前时间的数组
 225   1      // LCD1602_initial();           //初始化1602液晶
 226   1       //DS1302_portinit();                   //初始化DS1302的三根数据线
 227   1      init(); 
 228   1       DS_init();                             //启动振荡器,DS1302开始工作 
 229   1       set_time(settime);             //设置初始时间
 230   1       while(1)                                       //以下程序完成显示和设置时间
 231   1       {
 232   2         get_time(gettime);   //获得当前时间
 233   2         
 234   2         disp(gettime);               //显示当前时间
 235   2       }
 236   1      
 237   1      
 238   1      
 239   1              
 240   1      //      while(1);
 241   1      }
C51 COMPILER V7.06   CLOCK                                                                 02/10/2009 21:12:43 PAGE 5   

 242          
 243          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    685    ----
   CONSTANT SIZE    =     34    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      49
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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