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

📄 t.c

📁 at89c51 读取写入24c64的C源代码
💻 C
字号:
#include <at89x51.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int 
#define byte unsigned char
#if 0
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);
#else
void I2C_ReceiveFromSubAddress(byte SlaveAddress,uint SubAddress,byte *pRData,uchar N);
void I2C_SendToSubAddress(byte SlaveAddress,uint SubAddress,byte *pSData,uchar N);

#endif

//RXD TXD
#define KEY_LINE1 P3_0
#define KEY_LINE2 P3_1
#define KEY_LINE3 P3_7

//T0 T1
#define SCL P3_4
#define SDA P3_5
void delay(unsigned int n)
{
	unsigned int i,j;
	for(j=0;j<100;j++)
	for(i=0;i<n;i++);
}
unsigned char wdat[10]={0xF0,0x0F,0xA0,0x0A,0xAA,0x99,0x3C,0xC3,0x81,0x18};
unsigned char rdat[10]={0};
void main()
{
	EX0=0;//enable int 1
	EX1=1;//
	ET0  = 0;//disable 2 timer
	ET1  = 0;//
	IT0=0;//
	IT1=1;//edge trigger

	EA=1;
	P1=~0x00;

	for(;;);

}
unsigned char func,pw,pr;
void interrupt_func1() interrupt 2
{
	unsigned char k1,k2,k3;
	delay(1);
	k1=KEY_LINE1;
	k2=KEY_LINE2;
	k3=KEY_LINE3;


	if(k1==0)
	{
		func=(func+1)%4;

		if(func==0) pw=0;
		else if(func==1) pr=0;

		P1=~func;
	}


	if(func==0)//pw display
	{
		if(k2==0)	{unsigned int i;for(i=0;i<10;i++)rdat[i]=0; P1=~0xA0;}//clear rdat to 0
		else if(k3==0)	{unsigned int i;for(i=0;i<10;i++)wdat[i]=0xAA; P1=~0xAA;}//set wdat to 0xAA
	}
	else if(func==1)//pr display
	{
		if(k2==0)	{pw=(pw+9)%10;P1=~wdat[pw];}//pw--
		else if(k3==0)	{pw=(pw+1)%10;P1=~wdat[pw];}//pw++
	}
	else if(func==2)//pr display
	{
		if(k2==0)	{pr=(pr+9)%10;P1=~rdat[pr];}//pr--
		else if(k3==0)	{pr=(pr+1)%10;P1=~rdat[pr];}//pr++
	}
#if 0
	else if(func==3)//write and read
	{
		if(k2==0)	{AT24C64_W(wdat,0x100,10); P1=~0xF0; }// write
		else if(k3==0)	{AT24C64_R(rdat,0x100,10); P1=~0xF1;}// read
	}
#else
	else if(func==3)//write and read
	{
		if(k2==0)	{I2C_SendToSubAddress(0xa0,0x100,wdat,10); P1=~0xF3; }// write
		else if(k3==0)	{I2C_ReceiveFromSubAddress(0xa0,0x100,rdat,10); P1=~0xF1;}// read
	}
#endif

}

#if 0
#define I2C_SCK SCL
#define I2C_SDA SDA


/*24C64子程序 */
void  Delay_10_uS(void)
{
 //uchar i=10;
 //while(i--);
}
void Delay_N_mS( uint n_milisecond)  /* n mS delay */
{
 uint i;
 while(n_milisecond--)
 {
  i=10;//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++;
 }
}

#else

void delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
#if 0
void delayms (uint x)
{
 byte j;
 while(x--){
    for(j=0;j<125;j++){;}
      }
}
#endif
void I2C_Start()                      //I2C START
{
 SDA=1;
 _nop_();
  SCL=1;
  delay();
   SDA=0;
   delay();
  SCL=0;
  _nop_();
}

void I2C_Stop()             //I2C STOP
{
SDA=0;
_nop_();
SCL=1;
delay();
SDA=1;
delay();
}

bit  I2C_ReceiveAck() 
{
bit ck;
SDA=1;                  //THE HOST RELEASE THE BUS
_nop_();
_nop_();
SCL=1;           
_nop_();
_nop_();
ck=SDA;
_nop_();
_nop_();
SCL=0;
_nop_();
return(ck);
}


void I2C_SendAck()

{
SDA=0;
_nop_();
_nop_();
SCL=1;
delay();          //remain
SCL=0;
_nop_();
_nop_();
}

void I2C_SendNoAck()

{
SDA=1;
_nop_();
_nop_();
SCL=1;
delay();   //remain
SCL=0;
_nop_();
_nop_();
}

void I2C_SendByte(byte SData)
{uchar i;
for(i=0;i<8;i++)
{  SDA=(bit)(SData&0x80);
   SData=SData<<1;
   SCL=1;
   delay();
   SCL=0;
}
}

byte I2C_ReceiveByte()
{uchar i;
 byte RData;
 RData=0;

for(i=0;i<8;i++)
{  SDA=1;
  _nop_();
  SCL=1;
  _nop_();
  _nop_();
  RData=RData<<1;
  RData=RData|SDA;
  _nop_();
  _nop_();
  SCL=0;
}
return(RData);
}
//#define p1(x) P1=~x
#define p1(x) delay()
void I2C_SendToSubAddress(byte SlaveAddress,uint SubAddress,byte *pSData,uchar N)
{
uchar i;
I2C_Start();p1(0x1F);
I2C_SendByte(SlaveAddress);p1(0x2F);    //send slave address(write device)
I2C_ReceiveAck();p1(0x3F);

I2C_SendByte(SubAddress/256);p1(0x4F);    //send sub address
I2C_ReceiveAck();p1(0x5F);

I2C_SendByte(SubAddress%256);p1(0x6F);    //send sub address
I2C_ReceiveAck();p1(0x7F);

/*
I2C_SendByte(SubAddress);    
I2C_ReceiveAck();

*/
for(i=0;i<N;i++)
{ do{
  I2C_SendByte(pSData[i]);
  }while(I2C_ReceiveAck()==1); 
}
p1(0x8F);
I2C_Stop();p1(0x9F);
}
void I2C_ReceiveFromSubAddress(byte SlaveAddress,uint SubAddress,byte *pRData,uchar N)
{uchar i;
I2C_Start();
I2C_SendByte(SlaveAddress);    //send slave address(write device)
I2C_ReceiveAck();
I2C_SendByte(SubAddress/256);    //send sub address
I2C_ReceiveAck();
I2C_SendByte(SubAddress%256);    //send sub address
I2C_ReceiveAck();

/*
I2C_SendByte(SubAddress);    //send sub address
I2C_ReceiveAck();
*/
I2C_Start();                              //I2C Start Again!
I2C_SendByte(SlaveAddress+1);    //send slave address(read device)
I2C_ReceiveAck();
for(i=0;i<N-1;i++)
{
   pRData[i]=I2C_ReceiveByte();
   I2C_SendAck();
}
pRData[i]=I2C_ReceiveByte();
I2C_SendNoAck();
I2C_Stop();
}
/*
main()
{byte *pData;
 uint addr;
  byte a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  pData=a;
   do
   {
      I2C_SendToSubAddress(0xA0,addr,pData,16);
      addr+=16;
   delayms (1);
   }while(addr!=0x2000);
while(1);
}
*/
#endif

⌨️ 快捷键说明

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