#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();
} |