|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#define __24C01_02_C__
#include "config.h"
#include "24C01_02.h"
/***********************************************************************
IIC函数模块
***********************************************************************/
/*开始信号*/
void start(void)
{
sdaon;
sclon;
delay_us(10);
sdaoff;
delay_us(10);
scloff;
}
/*停止信号*/
void stop(void)
{
sdaoff;
delay_us(10);
sclon;
delay_us(10);
sdaon;
}
/*读取一个字节数据*/
uchar shin(void)
{
uchar i,read_data,data;
DDRD &= 0xf7;
for(i=0; i<8; i++)
{
sclon;
read_data <<= 1;
read_data |= (uchar)((PIND & 0x08)>>3);
scloff;
delay_us(10);
}
DDRD|=0x08;
return(read_data);
}
/*写一个字节数据*/
uchar shout(uchar write_data)
{
uchar i,temp;
uchar ack_bit;
for ( i=0; i<8; i++ )
{
temp = (write_data & 0x80)>>7;
if(temp == 0)
{
sdaoff;
}
else
{
sdaon;
}
delay_us(10);
sclon;
delay_us(10);
scloff;
write_data <<= 1;
}
sdaon;
delay_us(10);
sclon;
delay_us(10);
ack_bit = (PIND & 0x08)>>3;
scloff;
return ack_bit;
}
/*向指定地址写数据*/
void write_byte(uchar addr,uchar write_data)
{
start();
shout(OP_WRITE);
shout(addr);
shout(write_data);
stop();
delay_ms(10);
}
/*读取当前地址数据*/
uchar read_current(void)
{
uchar read_data;
start();
shout(OP_READ);
read_data = shin();
stop();
return read_data;
}
/*向指定地址读数据*/
uchar read_random(uchar random_addr)
{
start();
shout(OP_WRITE);
shout(random_addr);
return(read_current());
}
/*从指定地址开始写入一个4字节数据*/
void MultiByte_write(uchar add ,long dat)
{
union data
{
long dat;
uchar tab[4];
}count;
count.dat = dat;
write_byte(add,count.tab[0]);
write_byte(add+1,count.tab[1]);
write_byte(add+2,count.tab[2]);
write_byte(add+3,count.tab[3]);
delay_ms(100);
}
/*从指定地址开始读取一个4字节数据*/
long MultiByte_read(uchar add)
{
union data
{
long dat;
uchar tab[4];
}count;
count.tab[0] = read_random(add);
count.tab[1] = read_random(add+1);
count.tab[2] = read_random(add+2);
count.tab[3] = read_random(add+3);
return count.dat;
} |
|