📄 iic20080107.c
字号:
/************************************************/
#include <REG52.H>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit I2C_SDA= P1^5;
sbit I2C_SCK= P1^4;
sbit I2C_WP= P1^3;
sbit GRE=P1^7;
//===============================================
//=============================
//延时10US
//内部调用函数
void Delay_10_uS(void)
{
char i=10;
while(i--);
}
//延时n_milisecond MS
//供内部调用函数
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();
}
//写单字节数据
//内部调用函数
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;
}
//写多字节数据
//供外部调用函数
void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
{
while(count--)
{
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );//写命令设备地址为10100000最后一位为0"写标志"
I2C_Send_Byte( AT24C64_address/256 );//写存储器页地址
I2C_Send_Byte( AT24C64_address %256 );//写存储器字节地址
I2C_Send_Byte( *(uchar*)mcu_address );//写MCU地址指针指数据到存储单元
I2C_Stop();
delay_n_ms(10); /* waiting for write cycle to be completed */
((uchar*)mcu_address)++;
AT24C64_address++;
}
}
//读多字节数据
//供外部调用函数
void write1()
{
uchar i,j;
bit kbit;
uchar xdata tab[96]=
{
0x8,0x40,0x2,0x0,0x8,0x0,0xc,0x40,0x1,0x0,0x1c,0x0,0x8,0x40,0x3f,0xfe,0x31,0xfc,0x17,0xfc,0x44,0x4,0x40,0x0,0x10,0x48,0x44,0x8,0x88,0x0,0x34,0x4c,0x7,0xf0,0xc,0x0,0x53,0x48,0xc,0x20,0x1b,0xfe,0x92,0x50,0x12,0x40,0x30,0x20,0x1f,0xfe,0x21,0x80,0x50,0x20,0x10,0xa0,0x6,0x70,0x90,0x20,0x10,0xa0,0x3f,0xfe,0x10,0x20,0x11,0x10,0xc8,0x14,0x10,0x20,0x11,0x18,0x8,0x10,0x10,0x20,0x12,0xe,0x8,0x10,0x10,0x20,0x14,0x4,0xf,0xf0,0x10,0xa0,0x0,0x0,0x8,0x10,0x10,0x40
};
AT24C64_W(tab,0x00,0x60);
for(j=1;j<8;j++)
{ kbit=(bit)(tab[0]&0x80);
for(i=0;i<96;i++)
{
tab[i]=tab[i]<<1;
tab[i+1]=(bit)(tab[i]&0x08);
}
tab[95]=tab[95]|(uchar)kbit;
AT24C64_W(tab,j*0x60,0x60);
}
}
//void write2()
//{
//uchar tab[2][16]={
// {0x10,0x40,0x1A,0x40,0x13,0x40,0x32,0x40,0x23,0xFC,0x64,0x40,0xA4,0x40,0x28,0x40},
// {0x2F,0xFE,0x20,0x40,0x20,0x40,0x20,0x40,0x20,0x40,0x20,0x40,0x20,0x40,0x20,0x40}/*"件",2*/
// };
// AT24C64_W(tab,0x0040,0x20);
//}
//=====================================
void main()
{
//uchar i,j;
//P1=0;
//P2=0;
P3=0;
GRE=0;
write1();
// write2();
GRE=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -