每次设定好,结果再复位出现的数值总是大了些,但确定它是能写入的。
应该不是延时固化数据的问题,是WrToROM或者RdFromROM 这两个子程序出问题了吗
- #include <reg52.H>
- #include <intrins.h>
- #include "24c02.h"
- #define AddWr 0xa0 //写数据地址
- #define AddRd 0xa1 //读数据地址
-
- sbit WP=P1^6;
- /*------------------------------------------------
- 延时程序
- ------------------------------------------------*/
- void mDelay(unsigned char j)
- {
- unsigned int i;
- for(;j>0;j--)
- {
- for(i=0;i<100;i++)
- {;}
- }
- }
-
- /*--------------------------------------------------
- 初始化
- --------------------------------------------------*/
- void init(void)
- {
- Sda = 1;
- mDelay(3);
- Scl=1;
- mDelay(3);
- }
- /*------------------------------------------------
- 启动IIC总线
- ------------------------------------------------*/
- void Start(void)
- {
- Sda=1;
- mDelay(3);
- Scl=1;
- mDelay(3);
- Sda=0;
- mDelay(3);
- }
- /*------------------------------------------------
- 停止IIC总线
- ------------------------------------------------*/
- void Stop(void)
- {
- Sda=0;
- mDelay(3);
- Scl=1;
- mDelay(3);
- Sda=1;
- mDelay(3);
- }
- /*------------------------------------------------
- 应答IIC总线
- ------------------------------------------------*/
- void Ack(void)
- {
- Sda=0;
- mDelay(3);
- Scl=1;
- mDelay(3);
- Scl=0;
- mDelay(3);
- }
-
- /*------------------------------------------------
- 非应答IIc
- ------------------------------------------------*/
- void NoAck(void)
- {
- Sda=1;
- mDelay(3);
- Scl=1;
- mDelay(3);
- Scl=0;
- mDelay(3);
- }
-
-
- /*------------------------------------------------
- 发送一个字节
- ------------------------------------------------*/
- void Send(unsigned char Data)
- {
- unsigned char BitCounter=8;
- unsigned char temp;
- do
- {
- temp=Data;
- Scl=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- if((temp&0x80)==0x80)
- Sda=1;
- else
- Sda=0;
- Scl=1;
- temp=Data<<1;
- Data=temp;
- BitCounter--;
- }
- while(BitCounter);
- Scl=0;
- }
-
- /*------------------------------------------------
- 读入一个字节
- ------------------------------------------------*/
- unsigned char Read(void)
- {
- unsigned char temp=0;
- unsigned char temp1=0;
- unsigned char BitCounter=8;
- WP = 0;
- Sda=1;
- do
- {
- Scl=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- Scl=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- if(Sda)
- temp=temp|0x01;
- else
- temp=temp&0xfe;
- if(BitCounter-1)
- {
- temp1=temp<<1;
- temp=temp1;
- }
- BitCounter--;
- }
- while(BitCounter);
- return(temp);
- }
-
- /*------------------------------------------------
- 写入数据
- ------------------------------------------------*/
- void WrToROM(unsigned char Data[],unsigned char Address,unsigned char Num)
- {
- unsigned char i;
- unsigned char *PData;
- PData=Data;
- init();
- WP = 0;
- for(i=0;i<Num;i++) //数组长度
- {
- Start();
- Send(AddWr); //写入芯片地址
- Ack(); //应答
- Send(Address+i); //写入芯片地址
- Ack(); //应答
- Send(*(PData+i)); //写数据
- Ack(); //应答
- Stop(); //停止总线
- mDelay(20);
- }
- }
-
- /*------------------------------------------------
- 读出数据
- ------------------------------------------------*/
- void RdFromROM(unsigned char Data[],unsigned char Address,unsigned char Num)
- {
- unsigned char i;
- unsigned char *PData;
- PData=Data;
- for(i=0;i<Num;i++)
- {
- Start();
- Send(AddWr); //写入芯片地址
- Ack();
- Send(Address+i);//写入存储地址
- Ack();
- Start();
- Send(AddRd); //读入地址
- Ack();
- *(PData+i)=Read();//读数据
- Scl=0;
- NoAck();
- Stop();
- }
- }
- void U16_Save(unsigned int *tmp,unsigned char add)
- {
- unsigned char buf[2]=0;
- buf[0]= *tmp/256;
- buf[1]= *tmp%256;
- WrToROM(buf,add,2);
- mDelay(50);
- }
- void U16_Read(unsigned int *tmp,unsigned char add)
- {
- unsigned char buf[2]=0;
- RdFromROM(buf,add,2);
- *tmp=256*(buf[0])+buf[1];
- mDelay(50);
- }
复制代码
/*------------------------------------------------
发送一个字节
------------------------------------------------*/
void Send(unsigned char Data)
{
unsigned char BitCounter=8;
unsigned char temp;
do
{
temp=Data;
Scl=0;
if((temp&0x80)==0x80)
Sda=1;
else
Sda=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
Scl=1;
temp=Data<<1;
Data=temp;
BitCounter--;
}
while(BitCounter);
Scl=0;
}
/*------------------------------------------------
应答IIC总线
------------------------------------------------*/
void Ack(void)
{
Sda=0;
mDelay(3);
Scl=1;
mDelay(3);
Scl=0;
mDelay(3);
Sda=1;
}
/*------------------------------------------------
非应答IIc
------------------------------------------------*/
void NoAck(void)
{
Sda=1;
mDelay(3);
Scl=1;
mDelay(3);
Scl=0;
mDelay(3);
Sda=0;
}
一周热门 更多>