📄 64.c
字号:
#include "stc89c516.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit I2C_SCK=P1^0; //24cxx的时钟线
sbit I2C_SDA=P1^1; //24CXX的数据线
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 read_Flash(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=30;
while(i--);
}
void Delay_N_mS( uint n_milisecond) /* n mS delay */
{
uchar i;
while(n_milisecond--)
{
i=37;
while(i--);
}
}
void I2C_Init(void)
{
I2C_SCK=0;
I2C_SDA=0;
}
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;
}
#if 0
unsigned char write_Flash(unsigned int byte_address,unsigned char *a,unsigned char len)
{
if (len==0)
return 1;
while(len--)
{
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( byte_address/256 );
I2C_Send_Byte( byte_address %256 );
I2C_Send_Byte( *a );
I2C_Stop();
Delay_N_mS(20); /* waiting for write cycle to be completed */
a++;
byte_address++;
}
return 0;
}
#else
unsigned char write_Flash(unsigned int byte_address,unsigned char *a,unsigned char len)
{
unsigned char i;
EA=0;
if (len==0)
return 1;
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( byte_address/256 );
I2C_Send_Byte( byte_address %256 );
//I2C_Send_Byte( *a );
//I2C_Stop();
for(i=0;i<len;i++)
{
I2C_Send_Byte(*a);
a++;
Delay_10_uS();
}
I2C_Stop();
Delay_N_mS(60);
//byte_address++;
EA=1;
return 0;
}
#endif
unsigned char write_byte_Flash(unsigned int byte_address,unsigned char a)
{
EA=0;
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( byte_address/256 );
I2C_Send_Byte( byte_address %256 );
I2C_Send_Byte( a );
I2C_Stop();
Delay_N_mS(40); /* waiting for write cycle to be completed */
EA=1;
return 0;
}
unsigned char read_Flash(unsigned int byte_address,unsigned char *a,unsigned char len)
{
// FeedDog();
unsigned char i=0;
EA=0;
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( byte_address/256 );
I2C_Send_Byte( byte_address % 256 );
I2C_Start();
/*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
I2C_Send_Byte( 0xa1 );
if (len==0)
{
EA=1;
return 0;
}
if (len>1)
while(i<(len-1))
{
*a = I2C_Receive_Byte();
I2C_Ack();
i++;
a++;
}
*a = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
EA=1;
return 0;
}
unsigned char read_byte_Flash(unsigned int byte_addr)
{
uchar read_data[2];
read_Flash(byte_addr,read_data,1);
return read_data[0];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -