⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24c64存储调试.c

📁 这是运用c51单片机测试24c64存储芯片存储能力
💻 C
字号:
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define AddWr 0xa0 					//器件地址选择及写标志
#define AddRd 0xa1 					//器件地址选择及读标志

/*****************************************************************************************************/
//注意:24C64和24C02的不同在于24C64要送两次地址,分别是高位地址和低位地址,24C02只要送一个,但是还有一个不同,
//可能在于在写数据的时候其写周期限制不同,不同厂家相同型号的存储器可能其写周期限制也不同,所以这个也可能要
//调整一下延时。
/***************************************************************************************************/
/*有关全局变量*/
sbit  I2C_SDA=P2^1;            //模拟I2C数据传送位
sbit I2C_SCK=P2^0;            //模拟I2C时钟控制位
bit bdata ack;	              //应答标志位

uchar idata  Number1[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//待存储的数据
uchar idata  Number2[16]={16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};//待存储的数据
uchar  idata Number3[16]={32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47};//待存储的数据
uchar idata  Number4[16]={48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63};//待存储的数据
uchar  idata Number5[16]={64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79};//待存储的数据
uchar idata  Number6[16]={80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95};//待存储的数据
uchar idata  nData1[16];//读出的数据
uchar  idata nData2[16];//读出的数据
uchar  idata nData3[16];//读出的数据
uchar idata  many=0;//数据组的个数
uint    idata   dizhi=0x10;//存入24c64的0x10为首地址
/*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)
{
	 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++;
	 }
}

void main()
{
	uchar  i;
	AT24C64_W(&many,10,1);//写入已存储数据流的个数
	Delay_N_mS(10);
	AT24C64_W(Number1,dizhi,16); 				//将初始化后的数值写入EEPROM
	Delay_N_mS(10);
	many++;
	AT24C64_W(&many,10,1);//写入已存储数据流的个数
	Delay_N_mS(10);
	dizhi=dizhi+16;
	AT24C64_W(Number2,dizhi,16); 				//将初始化后的数值写入EEPROM
	Delay_N_mS(10);
	many++;
	AT24C64_W(&many,10,1);//写入已存储数据流的个数
	Delay_N_mS(10);
	dizhi=dizhi+16;
	AT24C64_W(Number3,dizhi,16); 				//将初始化后的数值写入EEPROM
	Delay_N_mS(10);
	many++;
	AT24C64_W(&many,10,1);//写入已存储数据流的个数
	Delay_N_mS(10);


	AT24C64_R( &many,10,1);
	dizhi=many*16;
	AT24C64_R(nData1,dizhi,16);
	dizhi=dizhi-16;
	AT24C64_R(nData2,dizhi,16);
	dizhi=dizhi-16;
	AT24C64_R(nData3,dizhi,16);
	while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -