程序目的 0-9,传入eeprom后,读取eeprom里面的内容,显示到LCD1602里面,但是没办法显示0求解释:
- #include <reg52.h>
- #include "intrins.h"
- /*-----------------------宏定义----------------*/
- #define uint unsigned int
- #define uchar unsigned char
- #define RS_LOW RS=0
- #define RS_HIGH RS=1
- #define RW_LOW RW=0
- #define RW_HIGH RW=1
- #define EN_LOW EN=0
- #define EN_HIGH EN=1
- #define Read_ATadd 0xa1
- #define Write_ATadd 0xa0
- /***************设置特殊功能寄存器位变量**********/
- sbit SDA = P2^0;
- sbit SCL = P2^1;
- sbit RS = P1^0;
- sbit RW = P1^1;
- sbit EN = P2^5;
- sbit DU = P2^6;
- sbit WE = P2^7;
- /***************设置特殊功能寄存器位变量**********/
- Check_Busy();
- /*-----------------------宏定义----------------*/
- //unsigned char display[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};//读入的是ASCII码,{"0123456789abcdef"}差别是什么?
- unsigned char display[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39} ;
- /*-----------------------定时函数----------------*/
- void delay1ms()
- {
- uchar i,j;
- for(i=0;i<10;i++)
- for(j=0;j<33;j++);
- }
- void dalaynms(uchar n)
- {
- while(n--)
- {
- delay1ms();
- }
- }
- /*-----------------------定时函数----------------*/
- /*---------------写指令-----------------------*/
- void WrAdress(uchar a)
- {
- while(Check_Busy());
- RS_LOW;
- RW_LOW;
- EN_HIGH;
- dalaynms(1);
- P0=a;
- EN_LOW;
- }
- /*---------------写指令-----------------------*/
- /*---------------写数据-----------------------*/
- void WrData(uchar a)
- {
- while(Check_Busy());
- RS_HIGH;
- RW_LOW;
- EN_HIGH;
- dalaynms(1);
- P0=a;
- EN_LOW;
- }
- /*---------------写数据-----------------------*/
- /*---------------读忙指令-----------------------*/
- Check_Busy()
- {
- uchar a;
- P0 = 0xff;
-
- RS_LOW;
- RW_HIGH;
- EN_LOW;
- dalaynms(1);
- EN_HIGH;
- a = P0;
- return (bit)(a&0x80);
- }
- /*---------------读忙指令-----------------------*/
- /*---------------写字符数据-----------------------*/
- void Lcd1602_Wrchar(uchar x,uchar y,uchar z)//定义为行标志,y定义为是第几位,z定义为这个是字符串还是字符
- {
-
- if(x==0)
- {
- WrAdress(0x80 +y);
- }
- else
- WrAdress(0xC0 +y);
- WrData(z);
- WrAdress(0x0c);
-
- }
- /*---------------写字符数据-----------------------*/
- /*-----------------------start----------------*/
- void start()
- {
- SCL = 1;
- SDA = 1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SDA = 0;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SCL = 0;//允许SDA进行变化
- }
- /*-----------------------start----------------*/
- /*-----------------------stop----------------*/
- void stop()
- {
- SDA = 0;
- SCL = 1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- SDA = 1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- /*-----------------------stop----------------*/
- /*-----------------------应答----------------*/
- AT24C02_ack()
- {
- bit ack;
- SDA = 1;
- _nop_();
- _nop_();
- SCL =1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ack = SDA;
- SCL =0;
- // while(ack);
- return ack;
- }
- /*-----------------------应答----------------*/
- /*-----------------------读一个字节----------------*/
- At24c02_Read_bit()
- {
- uchar i,x;
- for(i =0;i<8;i++)
- {
- SCL =1;
- x = (x<<1)|(uchar)SDA;
- SCL =0;//允许SDA进行变化
- }
- return(x);
- }
- /*-----------------------读一个字节----------------*/
- /*-----------------------写一个字节----------------*/
- void At24c02_write_bit(uchar y)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- SDA = (bit)(y&0x80);
- _nop_();
- y<<=1;
- SCL =1;
- _nop_();
- _nop_();
- SCL = 0;
- }
- }
- /*-----------------------写一个字节----------------*/
- void init()
- {
- SDA = 1;
- SCL = 1;
- WE = 1;
- P0 = 0xff;
- WE = 0;
- WrAdress(0x01);//清屏同时,数据指针也会清零即变成0x80;
- WrAdress(0x38); //设置 16×2 显示,5×7 点阵,8 位数据接口
- WrAdress(0x0f); // 显示(1 有效)、C 光标显示(1 有效)、B 光标闪烁(1 有效)
- WrAdress(0x06);//读或写一个字符后地址指针加 1 &光标加 1
- }
- /*-----------------------在特定位置读字节----------------*/
- At24c02_Read(uchar add)
- {
- uchar date;
- start();
- At24c02_write_bit(Write_ATadd);
- AT24C02_ack();
- At24c02_write_bit(add);
- AT24C02_ack();
- start();
- At24c02_write_bit(Read_ATadd);
- AT24C02_ack();
- date = At24c02_Read_bit();
- stop();
- return(date);
- }
- /*-----------------------在特定位置读字节----------------*/
- /*-----------------------在特定位置写字节----------------*/
- void At24c02_write(uchar x,uchar addr )
- {
- start();
- At24c02_write_bit(Write_ATadd);
- AT24C02_ack();
- At24c02_write_bit(addr);
- AT24C02_ack();
- At24c02_write_bit(x);
- AT24C02_ack();
- stop();
- }
- void main()
- {
- uchar i,j;
- init();
- while(1)
- {
- for(i=0;i<10;i++)
- {
- At24c02_write(display[i],i);
- }
-
- for(i=0;i<10;i++)
- {
- j = At24c02_Read(i);
- Lcd1602_Wrchar(0,8,j);
- dalaynms(200);
- dalaynms(200);
- dalaynms(200);
-
- }
- }
- }
-
复制代码 |