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

📄 at03.lst

📁 基于AT24C02的LCD显示
💻 LST
字号:
C51 COMPILER V8.08   AT03                                                                  05/07/2009 15:03:40 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE AT03
OBJECT MODULE PLACED IN at03.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE at03.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /* 这里举出一个实例(读写串行EEPROM芯片at2402) */ 
   2          /************************************************************************/ 
   3          /* Name:AT24C02存储器的读写程序,用到I2C总线,含相对独立的I2C总线读写函数 */ 
   4          /************************************************************************/ 
   5          #include<string.h> 
   6          #include<reg52.h> 
   7          #include<intrins.h> 
   8          #define uchar unsigned char
   9          #define uint unsigned int
  10          //#define DELAY_TIME 60 /*经实验,不要小于50!否则可能造成时序混乱*/ 
  11          //#define TRUE 1 
  12          //#define FALSE 0 
  13          sbit SCL=P2^6;/*假设由P3.4和P3.5控制*/ 
  14          sbit SDA=P2^7;
  15           
  16          sbit LCD_RS=P3^5;
  17          sbit LCD_RW=P3^6;
  18          sbit LCD_E=P3^7;
  19          
  20          /********** Function Definition 函数定义 ************/ 
  21          void DELAY(unsigned int t) /*延时函数*/ 
  22          { 
  23   1      while(t!=0) 
  24   1      t--; 
  25   1      } 
  26          void I2C_Start(void) 
  27          { 
  28   1      /*启动I2C总线的函数,当SCL为高电平时使SDA产生一个负跳变*/ 
  29   1      SDA=1; 
  30   1      SCL=1; 
  31   1      DELAY(60); 
  32   1      SDA=0; 
  33   1      DELAY(60); 
  34   1      SCL=0; 
  35   1      DELAY(60); 
  36   1      } 
  37          void I2C_Stop(void) 
  38          { 
  39   1      /*终止I2C总线,当SCL为高电平时使SDA产生一个正跳变*/ 
  40   1      SDA=0; 
  41   1      SCL=1; 
  42   1      DELAY(60); 
  43   1      SDA=1; 
  44   1      DELAY(60); 
  45   1      SCL=0; 
  46   1      DELAY(60); 
  47   1      } 
  48          void SEND_0(void) /* SEND ACK */ 
  49          { 
  50   1      /*发送0,在SCL为高电平时使SDA信号为低*/ 
  51   1      SDA=0; 
  52   1      SCL=1; 
  53   1      DELAY(60); 
  54   1      SCL=0; 
  55   1      DELAY(60); 
C51 COMPILER V8.08   AT03                                                                  05/07/2009 15:03:40 PAGE 2   

  56   1      } 
  57          void SEND_1(void) 
  58          { 
  59   1      /*发送1,在SCL为高电平时使SDA信号为高*/ 
  60   1      SDA=1; 
  61   1      SCL=1; 
  62   1      DELAY(60); 
  63   1      SCL=0; 
  64   1      DELAY(60); 
  65   1      } 
  66          bit Check_Acknowledge(void) 
  67          { 
  68   1      /*发送完一个字节后检验设备的应答信号*/ 
  69   1      SDA=1; 
  70   1      SCL=1; 
  71   1      DELAY(30); 
  72   1      F0=SDA; 
  73   1      DELAY(30); 
  74   1      SCL=0; 
  75   1      DELAY(60); 
  76   1      if(F0==1) 
  77   1      return 0; 
  78   1      return 1; 
  79   1      } 
  80          void WriteI2CByte(char b)reentrant 
  81          { 
  82   1      /*向I2C总线写一个字节*/ 
  83   1      char i; 
  84   1      for(i=0;i<8;i++) 
  85   1      if((b<<i)&0x80) 
  86   1      SEND_1(); 
  87   1      else 
  88   1      SEND_0(); 
  89   1      } 
  90          char ReadI2CByte(void)reentrant 
  91          { 
  92   1      /*从I2C总线读一个字节*/ 
  93   1      char b=0,i; 
  94   1      for(i=0;i<8;i++) 
  95   1      { 
  96   2      SDA=1; /*释放总线*/ 
  97   2      SCL=1; /*接受数据*/ 
  98   2      DELAY(10); 
  99   2      F0=SDA; 
 100   2      DELAY(10); 
 101   2      SCL=0; 
 102   2      if(F0==1) 
 103   2      { 
 104   3      b=b<<1; 
 105   3      b=b|0x01; 
 106   3      } 
 107   2      else 
 108   2      b=b<<1; 
 109   2      } 
 110   1      return b; 
 111   1      } 
 112          /**********以下为读写24c02的函数**********/ 
 113          void Write_One_Byte(char addr,char thedata) 
 114          { 
 115   1      bit acktemp=1; 
 116   1      /*write a byte to mem*/ 
 117   1      I2C_Start(); 
C51 COMPILER V8.08   AT03                                                                  05/07/2009 15:03:40 PAGE 3   

 118   1      WriteI2CByte(0xa0); 
 119   1      acktemp=Check_Acknowledge(); 
 120   1      WriteI2CByte(addr); /*address*/ 
 121   1      acktemp=Check_Acknowledge(); 
 122   1      WriteI2CByte(thedata); /*thedata*/ 
 123   1      acktemp=Check_Acknowledge(); 
 124   1      I2C_Stop();
 125   1       DELAY(60); 
 126   1      } 
 127          
 128          void Write_A_Page(char addr,char *buffer,char num) 
 129          { 
 130   1      bit acktemp=1; 
 131   1      //bit wrtmp; 
 132   1      int i; 
 133   1      /*write a page to at24c02*/ 
 134   1      I2C_Start(); 
 135   1      WriteI2CByte(0xa0); 
 136   1      acktemp=Check_Acknowledge(); 
 137   1      WriteI2CByte(addr);/*address*/ 
 138   1      acktemp=Check_Acknowledge(); 
 139   1      for(i=0;i<num;i++) 
 140   1      { 
 141   2      WriteI2CByte(buffer[i]); 
 142   2      if(!Check_Acknowledge()) 
 143   2      { 
 144   3      I2C_Stop(); 
 145   3      } 
 146   2      } 
 147   1      I2C_Stop(); 
 148   1      DELAY(60); 
 149   1      
 150   1      } 
 151          
 152          char Read_One_Byte(char addr) 
 153          { 
 154   1      bit acktemp=1; 
 155   1      char mydata; 
 156   1      /*read a byte from mem*/ 
 157   1      I2C_Start(); 
 158   1      WriteI2CByte(0xa0); 
 159   1      acktemp=Check_Acknowledge(); 
 160   1      WriteI2CByte(addr);/*address*/ 
 161   1      acktemp=Check_Acknowledge(); 
 162   1      I2C_Start(); 
 163   1      WriteI2CByte(0xa1); 
 164   1      acktemp=Check_Acknowledge(); 
 165   1      mydata=ReadI2CByte(); 
 166   1      acktemp=Check_Acknowledge(); 
 167   1      return mydata; 
 168   1      I2C_Stop(); 
 169   1      } 
 170          
 171          void Read_N_Bytes(char addr,char *buffer,char n) 
 172          { 
 173   1      bit acktemp=1; 
 174   1      int i=0; 
 175   1      /*read 8 bytes from mem*/ 
 176   1      I2C_Start(); 
 177   1      WriteI2CByte(0xa0); 
 178   1      acktemp=Check_Acknowledge(); 
 179   1      WriteI2CByte(addr);/*address*/ 
C51 COMPILER V8.08   AT03                                                                  05/07/2009 15:03:40 PAGE 4   

 180   1      acktemp=Check_Acknowledge(); 
 181   1      I2C_Start(); 
 182   1      WriteI2CByte(0xa1); 
 183   1      acktemp=Check_Acknowledge(); 
 184   1      for(i=0;i<n;i++) 
 185   1      { 
 186   2      buffer[i]=ReadI2CByte(); 
 187   2      if(i!=n-1) 
 188   2      SEND_0(); /*发送应答*/ 
 189   2      else 
 190   2      SEND_1(); /*发送非应答*/ 
 191   2      } 
 192   1      I2C_Stop(); 
 193   1      } 
 194          /***********************************/
 195          
 196          /*-------------------------------------- 
 197          ;模块名称:delay_n10us(); 
 198          ;功    能:延时函数,延时约n个10us
 199          ;-------------------------------------*/
 200          void delay_n10us(uint n)  //延时n个10us@12M晶振
 201          {       
 202   1              uint i;           
 203   1              for(i=n;i>0;i--)    
 204   1              {
 205   2              _nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); 
 206   2                      }
 207   1      } 
 208          
 209          /*-------------------------------------- 
 210          ;模块名称:LCD_write_command(); 
 211          ;功    能:LCD1602写指令函数 
 212          ;-------------------------------------*/ 
 213          void LCD_write_command(uchar dat)
 214          {
 215   1      delay_n10us(10);
 216   1      LCD_RS=0;         //指令
 217   1      LCD_RW=0;         //写入
 218   1      LCD_E=1;          //允许
 219   1      P0=dat;
 220   1      delay_n10us(10);  //实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
 221   1      LCD_E=0;
 222   1      delay_n10us(10);  //实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
 223   1      }
 224          
 225          /*-------------------------------------- 
 226          ;模块名称:LCD_init(); 
 227          ;功    能:初始化LCD1602 
 228          ;-------------------------------------*/ 
 229          void LCD_init(void)
 230          {
 231   1      delay_n10us(10);
 232   1      LCD_write_command(0x38);//设置8位格式,2行,5x7
 233   1      delay_n10us(10);
 234   1      LCD_write_command(0x0c);//整体显示,关光标,不闪烁
 235   1      delay_n10us(10);
 236   1      LCD_write_command(0x06);//设定输入方式,增量不移位
 237   1      delay_n10us(10);
 238   1      LCD_write_command(0x01);//清除屏幕显示
 239   1      delay_n10us(100);       //延时清屏,延时函数,延时约n个10us
 240   1      LCD_write_command(0x0d);////开显示,并显示光标
 241   1      delay_n10us(100);       //延时清屏,延时函数,延时约n个10us
C51 COMPILER V8.08   AT03                                                                  05/07/2009 15:03:40 PAGE 5   

 242   1      }
 243          
 244          
 245          /*-------------------------------------- 
 246          ;模块名称:LCD_write_data(); 
 247          ;功    能:LCD1602写数据函数 
 248          ;-------------------------------------*/
 249          void LCD_write_data(uchar dat)
 250          {
 251   1      delay_n10us(10);
 252   1      LCD_RS=1;          //数据
 253   1      LCD_RW=0;          //写入
 254   1      LCD_E=1;           //允许
 255   1      P0=dat;
 256   1      delay_n10us(10);
 257   1      LCD_E=0;
 258   1      delay_n10us(10);
 259   1      }
 260          
 261          /*-------------------------------------- 
 262          ;模块名称:LCD_disp_char(); 
 263          ;功    能:LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符,X(0-15),y(1-2)。
 264          ;-------------------------------------*/
 265          void LCD_disp_char(uchar x,uchar y,uchar dat)
 266          {
 267   1        uchar address;
 268   1        if(y==1)
 269   1               address=0x80+x;
 270   1        else
 271   1               address=0xc0+x;
 272   1        LCD_write_command(address);
 273   1        LCD_write_data(dat);
 274   1      }
 275          
 276          /*-------------------------------------- 
 277          ;模块名称:LCD_disp_str(); 
 278          ;功    能:LCD1602显示字符串函数,在某个屏幕起始位置{X(0-15),y(1-2)}上显示一个字符串。
 279          ;-------------------------------------*/
 280          void LCD_disp_str(uchar x,uchar y,uchar *str)
 281          {
 282   1        uchar address;
 283   1        if(y==1)
 284   1               address=0x80+x;
 285   1        else
 286   1               address=0xc0+x;
 287   1        LCD_write_command(address);
 288   1        while(*str!='\0')
 289   1        { 
 290   2          LCD_write_data(*str);   
 291   2          str++;
 292   2        }
 293   1      }
 294          
 295          void main() 
 296          { 
 297   1      //int i;
 298   1      
 299   1      char mybyte; 
 300   1      char code myarray[15]={0x46,0x4C,0x59,0x3A,0x31,0x33,0x36,0x34,0x30,0x33,0x30,0x38,0x30,0x34,0x31}; 
 301   1      char code myarray2[9]={0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49};
 302   1      char rdarray[20]; 
 303   1      
C51 COMPILER V8.08   AT03                                                                  05/07/2009 15:03:40 PAGE 6   

 304   1      //Write_One_Byte(0x20,0x34); 
 305   1      Write_A_Page(0x00,myarray2,9); 
 306   1      //Write_A_Page(0x18,myarray2,9); 
 307   1      mybyte=Read_One_Byte(0x00); 
 308   1      Read_N_Bytes(0x00,rdarray,9); 
 309   1      P1=rdarray[8];
 310   1      LCD_init();
 311   1      LCD_disp_str(0,1,myarray2);
 312   1      LCD_disp_str(0,2,rdarray);
 313   1      
 314   1      } 
*** WARNING C280 IN LINE 300 OF AT03.C: 'myarray': unreferenced local variable


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    839    ----
   CONSTANT SIZE    =     24    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      33
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       4
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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