cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 1405|回复: 0

[转帖]24C64 EEPROM的读写的C语言程序

[复制链接]
发表于 2005-8-4 21:20:25 | 显示全部楼层 |阅读模式
/*24C64子程序 */
bit   I2C_Start(void);
void  I2C_Stop(void);
void  I2C_Ack(void);
void  I2C_Nack(void);
bit   I2C_Send_Byte( uchar);
uchar I2C_Receive_Byte(void);
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)
{
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;
}
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 + AT24C64_address /256 *2);*/  /* 24C16  USE */
  I2C_Send_Byte( 0xa0 );
  I2C_Send_Byte(  AT24C64_address/256 );
  I2C_Send_Byte( AT24C64_address %256 );
  I2C_Send_Byte( *(uchar*)mcu_address );
  I2C_Stop();
  Delay_N_mS(10);       /* waiting for write cycle to be completed */
  ((uchar*)mcu_address)++;
  AT24C64_address++;
}
}
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 + AT24C64_address / 256 *2 );*/   /* 24C16 USE */
  I2C_Send_Byte( 0xa0 );
  I2C_Send_Byte( AT24C64_address/256 );
  I2C_Send_Byte( AT24C64_address % 256 );
  I2C_Start();
  /*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
  I2C_Send_Byte( 0xa1 );
  *(uchar*)mcu_address = I2C_Receive_Byte();
  I2C_Nack();
  I2C_Stop();
  ((uchar*)mcu_address)++;
  AT24C64_address++;
}
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2025-5-4 12:53 , Processed in 1.073111 second(s), 7 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表