?? iic20080107.c
字號(hào):
/************************************************/
#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;
//===============================================
//=============================
//延時(shí)10US
//內(nèi)部調(diào)用函數(shù)
void Delay_10_uS(void)
{
char i=10;
while(i--);
}
//延時(shí)n_milisecond MS
//供內(nèi)部調(diào)用函數(shù)
void delay_n_ms( uint n_milisecond) /* n mS delay */
{
uchar i;
while(n_milisecond--)
{
i=37;
while(i--);
}
}
//開(kāi)始位
//內(nèi)部調(diào)用函數(shù)
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;
}
//停止位
//內(nèi)部調(diào)用函數(shù)
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();
}
//寫(xiě)單字節(jié)數(shù)據(jù)
//內(nèi)部調(diào)用函數(shù)
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;
}
//寫(xiě)多字節(jié)數(shù)據(jù)
//供外部調(diào)用函數(shù)
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 );//寫(xiě)命令設(shè)備地址為10100000最后一位為0"寫(xiě)標(biāo)志"
I2C_Send_Byte( AT24C64_address/256 );//寫(xiě)存儲(chǔ)器頁(yè)地址
I2C_Send_Byte( AT24C64_address %256 );//寫(xiě)存儲(chǔ)器字節(jié)地址
I2C_Send_Byte( *(uchar*)mcu_address );//寫(xiě)MCU地址指針指數(shù)據(jù)到存儲(chǔ)單元
I2C_Stop();
delay_n_ms(10); /* waiting for write cycle to be completed */
((uchar*)mcu_address)++;
AT24C64_address++;
}
}
//讀多字節(jié)數(shù)據(jù)
//供外部調(diào)用函數(shù)
void write1()
{
//uchar i,j;
//bit kbit;
uchar xdata tab[96]=
{
0x8,0x0,0x7,0xf0,0x0,0x0,0xf,0xfc,0x4,0x10,0x1f,0xf0,0x10,0x80,0x4,0x10,0x10,0x10,0x10,0x80,0x4,0x10,0x10,0x10,0x20,0x80,0x7,0xf0,0x10,0x10,0x4f,0xf8,0x4,0x10,0x10,0x10,0x88,0x80,0x4,0x10,0x1f,0xf0,0x8,0x80,0x4,0x10,0x10,0x10,0x8,0x80,0x7,0xf0,0x10,0x10,0x8,0x80,0x4,0x10,0x10,0x10,0xff,0xfe,0x8,0x10,0x10,0x10,0x0,0x80,0x8,0x10,0x10,0x10,0x0,0x80,0x10,0x10,0x1f,0xf0,0x0,0x80,0x20,0x50,0x10,0x10,0x0,0x80,0x40,0x20,0x0,0x0,0x0,0x80,0x0,0x0,0x0,0x0
};
AT24C64_W(tab,0x120,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;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -