打印

24C04A,只能读,写不成功,奇怪了。高手指点一下。谢谢

[复制链接]
1546|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cpu51|  楼主 | 2010-7-9 00:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char;
#define uint  unsigned int;
   //-----定义I2总线端口, 可根据实际使用改变----
#define SCL_L PORTA&=~BIT(0) //I2总线时钟线
#define SCL_H PORTA|=BIT(0)
#define SDA_L PORTA&=~BIT(1) //I2总线数据线
#define SDA_H PORTA|=BIT(1)

#define SDA_TO_IN DDRA&=~BIT(1) _NOP() //设数据线位输入
#define SDA_TO_OUT DDRA|=BIT(1) _NOP() //设数据线位输出

#define SDA_IF_L (PINA&BIT(1))==0  //如果sda为低
#define SDA_IF_H (PINA&BIT(1))==BIT(1) //如果sda为高
    #define DELAY_us _NOP(); _NOP(); _NOP(); _NOP();_NOP();  
unsigned char temp1,temp2,writedevice_add=0xa0,readdevice_add=0xa1,word_address=0,word_address1=0,word_address2=0,word_data=0,dd;
//a:writedevice add
//a1:readdevice add
//b:wordbyte add
//c:byte_data
//d:write8bit_cs;
//*************************************************
void delay_for_eeprom()
{
  _NOP();_NOP();_NOP();_NOP();_NOP();_NOP(); _NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
  _NOP();_NOP();_NOP();_NOP();_NOP();_NOP();  
  _NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
}
//**********************EEPROM 启动 适用24c02**************************/
void start_cs2()
  
{   SDA_TO_OUT;   //置为输出状态
    delay_for_eeprom();
    SDA_H;
   delay_for_eeprom();
SCL_H;
    delay_for_eeprom();
SDA_L;
    delay_for_eeprom();
SCL_L;
    delay_for_eeprom();
}
//**********************EEPROM 停止 适用24c02 **************************/
void stop_cs2() {
    SDA_TO_OUT;   //置为输出状态
delay_for_eeprom();
    SCL_L;
    delay_for_eeprom();
SDA_L;
    delay_for_eeprom();
    SCL_H;
    delay_for_eeprom();
  SDA_H;
    delay_for_eeprom();
SCL_L;  
    delay_for_eeprom();
}
/***************************EEPROM 应答   24c02 ************************************************/
//内部函数
//********************************************************************************************
void read_ack_cs2()  //
{  SDA_TO_OUT;   //置为输出状态
    delay_for_eeprom();
SDA_L;
    delay_for_eeprom();
SCL_H;
    delay_for_eeprom();
SCL_L;
    delay_for_eeprom();
  }
/***************************EEPROM 应答   24c04 ************************************************/
void  write_ack_cs2(void)
{
   SDA_TO_OUT;  
     delay_for_eeprom();
  SDA_L;
     delay_for_eeprom();
  SCL_H;
  delay_for_eeprom();
  SCL_L;
  delay_for_eeprom();
  }
/***************************EEPROM 写数据 24c02 ************************************************/
void  write8bit_cs2(writedevice_address)
   {
unsigned char temp;
    SDA_TO_OUT;
       delay_for_eeprom();
    //置为输出状态
for(temp=8;temp!=0;temp--)
   {  
  if(0x80==(writedevice_address & 0x80) )
         {
           SDA_H;
            }
        else
         {
           SDA_L;
            }  
        delay_for_eeprom();
     SCL_H;
        delay_for_eeprom();
  SCL_L;
  delay_for_eeprom();
  writedevice_address=writedevice_address<<1;
  delay_for_eeprom();
     }
      }
//***************************EEPROM 读数据 24c02************************************************/
unsigned char read8bit_cs2()
{
    unsigned char output;
     delay_for_eeprom();
  SDA_TO_IN;   //置为输入状态
for(temp1=8;temp1!=0;temp1--)
{  
     SDA_H;
        delay_for_eeprom();
  SCL_H;
  delay_for_eeprom();
  
        if(SDA_IF_H)   
  {
    output=output<<1;
    output|=0x01;
    }
  else
        {output=output<<1;}
  
  delay_for_eeprom();
  SCL_L;
  delay_for_eeprom();
  }
      return(output);
      }
//***************************EEPROM 读数据 24c02*****************************************
//内部函数:带参数返回
////2004-4-10:增加写重试功能
//**************
char read_eeprom_byte(word_address)  //
{   
   start_cs2(); //start
   delay_for_eeprom();
   write8bit_cs2(0xa0);  //device address
   read_ack_cs2();
   write8bit_cs2(word_address);     //word address
   read_ack_cs2();
   start_cs2();          //start
   write8bit_cs2(0xa1);  //device address
   read_ack_cs2();
   word_data=read8bit_cs2();      //读出1  
   read_ack_cs2();
   stop_cs2();
   return(word_data);   
   }
//*************************************************************************
//写数据到EEPROM  
//入口:word_address,word_data
//2004-4-10:增加写重试功能
//************************************************************************
void write_eeprom_byte(word_address,word_data)  //b-byteadd ,c_data
{
      stop_cs2();
      delay_for_eeprom();
      start_cs2();
      write8bit_cs2(0xa0);  //device address
   write_ack_cs2;
      write8bit_cs2(word_address);  //byte address //b   
   write_ack_cs2;
      write8bit_cs2(word_data);  //byte_data      
   write_ack_cs2();
   stop_cs2();
  }

相关帖子

沙发
cpu51|  楼主 | 2010-7-9 00:49 | 只看该作者
main()
{init();
   
write_eeprom_byte(0x70,0x21)  
  data1=read_eeprom_byte(0x70);  
while(1){
......

.......}
}
能读出,但写不进去。奇怪呀。

使用特权

评论回复
板凳
duojinian| | 2010-7-9 09:59 | 只看该作者
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:承接工业远程控制系统及设计研发,DCS控制系统,无线采集控制系统,单片机设计,工业设备维修,仪器仪表、控制系统维护、检修。

97

主题

456

帖子

0

粉丝