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

📄 ds1307.lst

📁 一个基于51单片机的定时报警设计电路及程序
💻 LST
字号:
C51 COMPILER V7.08   DS1307                                                                04/06/2008 21:07:25 PAGE 1   


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

line level    source

   1          #include <reg52.h>
   2          #include <intrins.h>
   3          
   4          sbit SCL=P1^0;
   5          sbit SDA=P1^1;
   6          #define ACK 0
   7          #define NACK 1
   8          #define WRITE 0xd0
   9          #define READ  0xd1
  10          unsigned char dispbuf[7];
  11          unsigned char code zimo[16]={0xc0,0xf9,0xa4,0xb0,       // 0, 1, 2, 3
  12                                          0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};// 4, 5, 6, 7, 8, 9
  13          unsigned char code disflag[6]={0x20,0x10,0x08,0x04,0x02,0x01};
  14          unsigned char led_buf[6];
  15          unsigned char n,flag;
  16          unsigned char Sec, Min, Hrs;
  17          
  18          void delayms(unsigned char ms)  
  19          // 延时子程序
  20          {
  21   1              unsigned char i;
  22   1              while(ms--)
  23   1              {
  24   2                      for(i = 0; i < 120; i++);
  25   2              }
  26   1      }
  27          //I2C启始标志
  28          void I2c_start(void)
  29          {
  30   1              SDA = 1;
  31   1              SCL = 1;
  32   1              _nop_();
  33   1              _nop_();
  34   1              SDA = 0;
  35   1              _nop_();
  36   1              _nop_();
  37   1              SCL = 0;
  38   1              _nop_();
  39   1      }
  40          //I2C停止标志
  41          void I2c_stop(void)
  42          {
  43   1              SDA = 0;
  44   1              _nop_();
  45   1              _nop_();
  46   1              SCL = 1;
  47   1              _nop_();
  48   1              _nop_();
  49   1              _nop_();
  50   1              _nop_();
  51   1              SDA = 1;
  52   1              _nop_();
  53   1      }
  54          
  55          unsigned char I2c_read(unsigned char ack)
C51 COMPILER V7.08   DS1307                                                                04/06/2008 21:07:25 PAGE 2   

  56          {
  57   1              unsigned char i,read_data;
  58   1              for(i = 0; i < 8; i++)
  59   1              {
  60   2                      SCL = 1;
  61   2                      _nop_();
  62   2                      read_data <<= 1;
  63   2                      read_data |= (unsigned char)SDA;
  64   2                      SCL = 0;
  65   2                      _nop_();
  66   2              }
  67   1              _nop_();
  68   1              SCL = 0;
  69   1              if(ack == NACK) SDA = 1; /* sda = 1 if next cycle is reset */
  70   1              else
  71   1                      SDA=0;
  72   1              _nop_();
  73   1              SCL = 1;
  74   1              _nop_();
  75   1              SCL = 0;
  76   1              _nop_();
  77   1              SDA = 1;
  78   1              _nop_();
  79   1              return(read_data);
  80   1      }
  81          
  82          void I2c_write(unsigned char write_data)
  83          {
  84   1              unsigned char i;
  85   1              bit ack_bit;
  86   1              for(i = 0; i < 8; i++)          // 循环移入8个位
  87   1              {
  88   2                      SDA = (bit)(write_data & 0x80);
  89   2                      SCL = 1;
  90   2                      _nop_();
  91   2                      _nop_();
  92   2                      SCL = 0;
  93   2                      _nop_();
  94   2                      _nop_();
  95   2                      write_data <<= 1;
  96   2              }
  97   1              SDA = 1;                        // 读取应答
  98   1              _nop_();
  99   1              _nop_();
 100   1              SCL = 1;
 101   1              _nop_();
 102   1              _nop_();
 103   1              SCL = 0;
 104   1              _nop_();
 105   1              _nop_();
 106   1              ack_bit = SDA;
 107   1      }
 108          
 109          void Init_Rtc(unsigned char sec,unsigned char min,unsigned char hour)
 110          {
 111   1              I2c_start(); 
 112   1              I2c_write(WRITE); 
 113   1              I2c_write(0x00); 
 114   1              I2c_write(sec); 
 115   1              I2c_write(min);
 116   1              I2c_write(hour);
 117   1              I2c_stop();
C51 COMPILER V7.08   DS1307                                                                04/06/2008 21:07:25 PAGE 3   

 118   1              delayms(10);
 119   1      }
 120          void Read_time(void)
 121          {
 122   1              I2c_start();
 123   1              I2c_write(WRITE); 
 124   1              I2c_write(0x00); 
 125   1              I2c_start();
 126   1              I2c_write(READ);
 127   1              Sec = I2c_read(ACK);
 128   1              Min = I2c_read(ACK);
 129   1              Hrs = I2c_read(NACK);
 130   1              I2c_stop();     
 131   1              delayms(10);    
 132   1      }
 133          void disp_time(void)
 134          {
 135   1              unsigned char disp_temp[3];
 136   1                      unsigned char temp_h,temp_l,i,j;
 137   1                      Read_time();
 138   1                      disp_temp[0]=Sec;
 139   1                      disp_temp[1]=Min;
 140   1                      disp_temp[2]=Hrs;
 141   1                              do
 142   1                              {
 143   2                                      temp_h=(disp_temp[j]&0xf0)>>4;
 144   2                                      temp_l=disp_temp[j]&0x0f;
 145   2                                      led_buf[i]=zimo[temp_l];
 146   2                                      i++;
 147   2                                      led_buf[i]=zimo[temp_h];
 148   2                                      i++;
 149   2                                      j++;
 150   2                              }while(j<3);
 151   1                              j=0;
 152   1                              i=0;
 153   1      }
 154          int main()
 155          {
 156   1      //      unsigned char disp_temp[3];
 157   1      //      unsigned char temp_h,temp_l,i,j;
 158   1              P0 = 0xff;
 159   1              P2 = 0xff;
 160   1              TMOD = 0x01;
 161   1              TH0 = 0xfe;
 162   1              TL0 = 0x0c;
 163   1              IE = 0x82;
 164   1              flag=0;
 165   1              n=0;
 166   1      /*      I2c_start(); 
 167   1              I2c_write(WRITE); 
 168   1              I2c_write(0x00); 
 169   1              I2c_write(0x00);
 170   1              I2c_stop();*/
 171   1              Init_Rtc(0x00,0x50,0x03);
 172   1              TR0=1;
 173   1              while(1)
 174   1              {
 175   2      /*              Read_time();
 176   2                      disp_temp[0]=Sec;
 177   2                      disp_temp[1]=Min;
 178   2                      disp_temp[2]=Hrs;
 179   2                              do
C51 COMPILER V7.08   DS1307                                                                04/06/2008 21:07:25 PAGE 4   

 180   2                              {
 181   2                                      temp_h=(disp_temp[j]&0xf0)>>4;
 182   2                                      temp_l=disp_temp[j]&0x0f;
 183   2                                      led_buf[i]=zimo[temp_l];
 184   2                                      i++;
 185   2                                      led_buf[i]=zimo[temp_h];
 186   2                                      i++;
 187   2                                      j++;
 188   2                              }while(j<3);
 189   2                              j=0;
 190   2                              i=0;*/
 191   2                       disp_time();
 192   2              }
 193   1      }
 194          
 195          void timer0(void) interrupt 1
 196          {
 197   1              TH0=0xfe;
 198   1              TL0=0x0c;
 199   1      //      KeyDelaytime++;
 200   1              if(flag<7)
 201   1              {
 202   2                      P0=led_buf[n];
 203   2                      P2=disflag[n];
 204   2               }
 205   1              else
 206   1              {
 207   2                      P2=0x00;
 208   2                      P0=0;
 209   2               }
 210   1               flag++;
 211   1               if(flag==8)
 212   1               {
 213   2                      n++;
 214   2                      if(n==6)
 215   2                      {
 216   3                              n=0;
 217   3                      }
 218   2                      flag=0;
 219   2              }
 220   1      }                       
 221          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    409    ----
   CONSTANT SIZE    =     22    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     18       5
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       1
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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