| 
 
| #include "REG51.H" #include "INTRINS.H"
 #define uint unsigned int
 #define uchar unsigned char
 typedef unsigned char BYTE;
 typedef unsigned short WORD;
 sbit SCL = P2^0;                //AT24C04的时钟
 sbit SDA = P2^1;                //AT24C04的数据
 BYTE BUF[16];                   //数据缓存区
 BYTE code res[6] _at_ 0x23;
 BYTE code TESTDATA[] =
 {
 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
 0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF
 };
 #define lcddata      P0     //lcd1602数据口
 #define ton          30     //按键灵敏度调节,越小越灵敏
 sbit RS   =P2^6;        //lcd液晶接口
 sbit RW   =P2^5;
 sbit E    =P2^7;
 
 void Delay5us();
 void Delay5ms();
 void AT24C04_Start();
 void AT24C04_Stop();
 void AT24C04_SendACK(bit ack);
 bit AT24C04_RecvACK();
 void AT24C04_SendByte(BYTE dat);
 BYTE AT24C04_RecvByte();
 void AT24C04_ReadPage();
 void AT24C04_WritePage();
 
 /**********************************
 lcd1602读写函数
 **********************************/
 void write_com(uchar com)
 {
 RS=0;
 RW=0;
 E=0;
 lcddata=com;
 Delay5ms();
 E =1;
 Delay5ms();
 E =0;
 }
 void write_date(uchar date)
 {
 RS=1;
 RW=0;
 E =0;
 lcddata=date;
 Delay5ms();
 E =1;
 Delay5ms();
 E =0;
 }
 /****************************************
 lcd1602初始化函数
 ***************************************/
 void init()
 {
 E=0;
 write_com(0x38);
 write_com(0x0c);
 write_com(0x06);
 write_com(0x01);
 write_com(0x80);
 Delay5ms();
 write_com(0x80);
 }
 
 /**************************************
 向AT24C04写1页(16字节)数据
 将TESTDATA开始的16个测试数据写如设备的00~0F地址中
 **************************************/
 void AT24C04_WritePage()
 {
 BYTE i;
 AT24C04_Start();            //起始信号
 AT24C04_SendByte(0xa0);     //发送设备地址+写信号
 AT24C04_SendByte(0x00);     //发送存储单元地址
 for (i=0; i<16; i++)
 {
 AT24C04_SendByte(TESTDATA);
 }
 AT24C04_Stop();             //停止信号
 }
 /**************************************
 从AT24C04读取1页(16字节)数据
 将设备的00~0F地址中的数据读出存放在DATA区的BUF中
 **************************************/
 void AT24C04_ReadPage()
 {
 BYTE i;
 AT24C04_Start();            //起始信号
 AT24C04_SendByte(0xa0);     //发送设备地址+写信号
 AT24C04_SendByte(0x00);     //发送存储单元地址
 AT24C04_Start();            //起始信号
 AT24C04_SendByte(0xa1);     //发送设备地址+读信号
 for (i=0; i<16; i++)
 {
 BUF = AT24C04_RecvByte();
 if (i == 15)
 {
 AT24C04_SendACK(1); //最后一个数据需要会NAK
 }
 else
 {
 AT24C04_SendACK(0); //回应ACK
 }
 }
 AT24C04_Stop();             //停止信号
 }
 /**************************************
 延时5微秒([email=STC90C52RC@12M]STC90C52RC@12M[/email])
 不同的工作环境,需要调整此函数
 当改用1T的MCU时,请调整此延时函数
 **************************************/
 void Delay5us()
 {
 _nop_();
 _nop_();
 }
 /**************************************
 延时5毫秒([email=STC90C52RC@12M]STC90C52RC@12M[/email])
 不同的工作环境,需要调整此函数
 当改用1T的MCU时,请调整此延时函数
 **************************************/
 void Delay5ms()
 {
 WORD n = 560;
 while (n--);
 }
 /**************************************
 起始信号
 **************************************/
 void AT24C04_Start()
 {
 SDA = 1;                    //拉高数据线
 SCL = 1;                    //拉高时钟线
 Delay5us();                 //延时
 SDA = 0;                    //产生下降沿
 Delay5us();                 //延时
 SCL = 0;                    //拉低时钟线
 }
 /**************************************
 停止信号
 **************************************/
 void AT24C04_Stop()
 {
 SDA = 0;                    //拉低数据线
 SCL = 1;                    //拉高时钟线
 Delay5us();                 //延时
 SDA = 1;                    //产生上升沿
 Delay5us();                 //延时
 }
 /**************************************
 发送应答信号
 入口参数:ack (0:ACK 1:NAK)
 **************************************/
 void AT24C04_SendACK(bit ack)
 {
 SDA = ack;                  //写应答信号
 SCL = 1;                    //拉高时钟线
 Delay5us();                 //延时
 SCL = 0;                    //拉低时钟线
 Delay5us();                 //延时
 }
 /**************************************
 接收应答信号
 **************************************/
 bit AT24C04_RecvACK()
 {
 SCL = 1;                    //拉高时钟线
 Delay5us();                 //延时
 CY = SDA;                   //读应答信号
 SCL = 0;                    //拉低时钟线
 Delay5us();                 //延时
 return CY;
 }
 /**************************************
 向IIC总线发送一个字节数据
 **************************************/
 void AT24C04_SendByte(BYTE dat)
 {
 BYTE i;
 for (i=0; i<8; i++)         //8位计数器
 {
 dat <<= 1;              //移出数据的最高位
 SDA = CY;               //送数据口
 SCL = 1;                //拉高时钟线
 Delay5us();             //延时
 SCL = 0;                //拉低时钟线
 Delay5us();             //延时
 }
 AT24C04_RecvACK();
 }
 /**************************************
 从IIC总线接收一个字节数据
 **************************************/
 BYTE AT24C04_RecvByte()
 {
 BYTE i;
 BYTE dat = 0;
 SDA = 1;                    //使能内部上拉,准备读取数据
 for (i=0; i<8; i++)         //8位计数器
 {
 dat <<= 1;
 SCL = 1;                //拉高时钟线
 Delay5us();             //延时
 dat |= SDA;             //读数据
 SCL = 0;                //拉低时钟线
 Delay5us();             //延时
 }
 return dat;
 }
 
 
 
 void main()
 {
 BYTE i;
 init();
 
 AT24C04_WritePage();
 Delay5ms();
 AT24C04_ReadPage();
 Delay5ms();
 while (1)
 {
 for (i=0; i<8; i++)
 {
 write_com(0x82);
 write_date(0x30+BUF[0]);
 write_com(0x83);
 write_date(0x30+BUF[1]);
 write_com(0x84);
 write_date(0x30+BUF[2]);
 write_com(0x85);
 write_date(0x30+BUF[3]);
 Delay5ms();
 }
 }
 }
 
 
 我的的是STC89C52单片机,调试后,没有错误,但是在1602显示上出现问题,不是我要的数字,4个都显示“/”这个符号,
 
 请求大家,我的问题在哪里?应该怎么改?
 
 先多谢了
 | 
 |