?? 24c512的讀寫簡單程序.txt
字號:
24C512的讀寫簡單程序.絕對原創(chuàng)!
文章內(nèi)容:
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define False 0
#define True 1
#define device_wr 0xa0
#define device_rd 0xa1
#include<intrins.h> // _nop_
#include<absacc.h>
#include"reg52E.h"
//----------------------------------------------------------
void daly_10us( void )
{ uchar i;
for(i=5;i>0;i--)
{
_nop_();
_nop_();
} //11.3us
}
void iic_start( void )
{
escl = False;
esda = True;
escl = True;
_nop_();
_nop_();
_nop_();
_nop_(); //set-up time min=0.25us
esda = False;
_nop_();
_nop_();
_nop_();
_nop_(); //hold time min=0.25us
escl=False;
_nop_();
_nop_();
}
void iic_stop( void )
{
escl=False;
esda=False;
escl=True;
_nop_();
_nop_();
_nop_();
_nop_(); //min 0.25us
esda=True;
_nop_();
_nop_();
_nop_();
_nop_(); //min 0.5us befor new start
esda=False;
_nop_();
_nop_();
}
//--------------------------------------------------------------
void tx(uchar wr_data)
{
uchar i;
for(i=8;i>0;i--)
{
if((wr_data&0x80)==0x80)
esda=True;
else
esda=False;
daly_10us();
escl=True;
daly_10us(); //0.543*4=2us //Thigh = min 0.4us
escl=False;
daly_10us(); //Tlow = min 0.6us
wr_data<<=1;
}
}
//-----------------------------------------------------------
uchar rx( void )
{
uchar i,rd_data=0;
esda=1;
for(i=8;i>0;i--)
{
rd_data<<=1;
escl=True; //有些資料顯示UP為數(shù)據(jù)輸出????
_nop_();
//Taa max 0.55us Clock low to Data out
if(esda)
rd_data|=0x01;
else
rd_data&=0xfe;
escl=False; //Thigh = min 0.4us
daly_10us(); //Tlow = min 0.6us
}
return rd_data;
}
//------------------------------------------------------------------------------------------
// FunName: uchar read_iic(uint rd_data_addr)
// In : uint rd_data_addr
// Out: uchar rd_data
// description:
// 24C512讀數(shù)據(jù)函數(shù)
// Author: xuchuanfang
// Date: 2006-8-28
//-----------------------------------------------------------------------------------------------
uchar read_iic(uint rd_data_addr)
{
uchar rd_data=0,msb=0,lsb=0;
msb=rd_data_addr/256;
lsb=rd_data_addr%256;
iic_start();
tx(device_wr);
daly_10us();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
tx(msb); //高位地址
daly_10us(); //MSB
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
tx(lsb);
daly_10us(); //LSB
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
iic_start();
tx(device_rd);
daly_10us();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
rd_data=rx();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
iic_stop();
return rd_data;
}
//------------------------------------------------------------------------------------------
// FunName: uchar write_iic(uchar updata,uint wr_data_addr)
// In : uchar updata,uint wr_data_addr
// Out: uchar ACK
// description:
// OK=True NO=False
// Author: xuchuanfang
// Date:
//---------------------------------------------------------------------------------------
//I2C編程中的地址問題:/*
// 芯片型號 Device Adress 讀寫地址 備注 特征編碼
//24c01~02 1010 A2 A1 A0 R/W 單字節(jié)地址 常規(guī) 1
//24c04 1010 A2 A1 P0 R/W 單字節(jié)地址 *設(shè)備地址中或入高位地址 2
//24c08 1010 A2 P1 P0 R/W 單字節(jié)地址 *設(shè)備地址中或入高位地址 2
//24c16 1010 P2 P1 P0 R/W 單字節(jié)地址 *設(shè)備地址中或入高位地址 2
//24c32~64 1010 A2 A1 A0 R/W 2字節(jié)地址 常規(guī) 3
//24c128~512 1010 0 A1 A0 R/W 2字節(jié)地址 常規(guī) 3
//24c256B 1010 A2 A1 A0 R/W 2字節(jié)地址 常規(guī) 3
//24c1024 1010 0 A1 P0 R/W 2字節(jié)地址 *設(shè)備地址中或入高位地址 4*/
//
*/
//A0~A2均接地,所以就有通常使用的:
// 芯片型號 Device Adress 讀寫地址 備注
//24c01~02 0xA0 / 0xA1 單字節(jié)地址 常規(guī)
//24c04 0xA0 / 0xA1 | P0 R/W 單字節(jié)地址 *設(shè)備地址中或入高位地址
//24c08 0xA0 / 0xA1 | P1 P0 R/W 單字節(jié)地址 *設(shè)備地址中或入高位地址
//24c16 0xA0 / 0xA1 | P2 P1 P0 R/W 單字節(jié)地址 *設(shè)備地址中或入高位地址
//24c32~64 0xA0 / 0xA1 2字節(jié)地址 常規(guī)
//24c128~512 0xA0 / 0xA1 2字節(jié)地址 常規(guī)
//24c256B 0xA0 / 0xA1 2字節(jié)地址 常規(guī)
//24c1024 0xA0 / 0xA1 | P0 R/W 2字節(jié)地址 *設(shè)備地址中或入高位地址
// 曉齊大蝦的整理
//-----------------------------------------------------------------------------------------------
uchar write_iic(uchar updata,uint wr_data_addr)
{
uchar i,j,msb,lsb;
uchar ack=0;
msb=wr_data_addr/256;
lsb=wr_data_addr%256;
iic_start();
tx(device_wr);
daly_10us();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
tx(msb); //高位地址
daly_10us();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
tx(lsb);
daly_10us();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
tx(updata);
daly_10us();
escl=True;
_nop_();
_nop_();
_nop_();
_nop_();
escl=False;
_nop_();
_nop_();
_nop_();
_nop_(); // 9 CLK ACK
if(esda)
ack=False;
else
ack=True;
iic_stop();
for(i=200;i>0;i--)
{
for(j=30;j>0;j--)
{
_nop_();
_nop_();
}
} //13.348ms 5ms
return ack;
}
/*--------------------------------------------------------------------*/
//也是5分鐘的下載時間
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -