/*24C64子程序 */
bit I2C_Start(void);
void I2C_Stop(void);
void I2C_Ack(void);
void I2C_Nack(void);
bit I2C_Send_Byte( uchar);
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count);
void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count);
void Delay_10_uS(void)//延时10uS
{
char i=10;
while(i--);
}
void Delay_N_mS( uint n_milisecond) // n mS delay 毫秒级延时
{
uchar i;
while(n_milisecond--)
{
i=37;
while(i--);
}
}
bit I2C_Start(void)//启动总线
{
Delay_10_uS();
I2C_SDA =1;
Delay_10_uS();
I2C_SCK =1;
Delay_10_uS();
if ( I2C_SDA == 0) return 0;
if ( I2C_SCK == 0) return 0;
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 0;
Delay_10_uS();
return 1;
}
void I2C_Stop(void)//停止总线
{
Delay_10_uS();
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
}
void I2C_Ack(void)//非应答信号
{
Delay_10_uS();
I2C_SDA=0;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
Delay_10_uS();
}
void I2C_Nack(void))//是应答信号
{
Delay_10_uS();
I2C_SDA=1;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
Delay_10_uS();
}
bit I2C_Send_Byte( uchar d)//字节发送
{
uchar i = 8;
bit bit_ack;
while( i-- )
{
Delay_10_uS();
if ( d &0x80 ) I2C_SDA =1;
else I2C_SDA =0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SCK = 0;
d = d << 1;
}
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
bit_ack = I2C_SDA;
I2C_SCK =0;
Delay_10_uS();
return bit_ack;
}
uchar I2C_Receive_Byte(void)//字节读
{
uchar i = 8, d;
Delay_10_uS();
I2C_SDA = 1;
while ( i--)
{
d = d << 1;
Delay_10_uS();
I2C_SCK =1;
if ( I2C_SDA ) d++;
Delay_10_uS();
I2C_SCK =0;
}
return d;
}
//*mcu_address:数据地址指针;uint AT24C64_address:器件存储地址;uint count:数据字节数
void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
{
DOG_WDI=!DOG_WDI;
DOGTIME=0;
while(count--)
{
I2C_Start();
I2C_Send_Byte( 0xa0 );//器件写
I2C_Send_Byte( AT24C64_address/256 );// 写入地址高8位分离
I2C_Send_Byte( AT24C64_address %256 );//写入地址低8位分离
I2C_Send_Byte( *(uchar*)mcu_address );//写入首位数据
I2C_Stop();
Delay_N_mS(10); /* waiting for write cycle to be completed */
((uchar*)mcu_address)++;//数据指针加一,指向下一个数据
AT24C64_address++;//存入24c64下一个存储位置
}
}
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
DOG_WDI=!DOG_WDI;
DOGTIME=0;
while(count--)
{
I2C_Start();
I2C_Send_Byte( 0xa0 );//器件写地址
I2C_Send_Byte( AT24C64_address/256 );//高8位地址
I2C_Send_Byte( AT24C64_address % 256 );//低8位地址
I2C_Start();
I2C_Send_Byte( 0xa1 );//器件读数据
*(uchar*)mcu_address = I2C_Receive_Byte();//读入首位数据存入
I2C_Nack();
I2C_Stop();
((uchar*)mcu_address)++;//指向下一个地址
AT24C64_address++;//准备读器件下一个地址内容
}
}
cunchu.rar_24c64
版权申诉
88 浏览量
2022-09-24
14:09:12
上传
评论
收藏 1KB RAR 举报
寒泊
- 粉丝: 75
- 资源: 1万+