pic16f877a對24c01的讀寫程序
#i nclude
#i nclude
#define uchar unsigned char
#define uint unsigned int
static volatile unsigned char aCC@ 0x20; //發(fā)收數(shù)據(jù)暫存器
static volatile bit acc0 @ (unsigned)&acc*8+0; //接收
static volatile bit acc7 @ (unsigned)&acc*8+7; //發(fā)送
static volatile unsigned char err @ 0x21; //
static volatile bit err0 @ (unsigned)&err*8+0;
static volatile bit err3 @ (unsigned)&err*8+3;
static volatile bit err6 @ (unsigned)&err*8+6;
static volatile bit err7 @ (unsigned)&err*8+7; //應(yīng)答位暫存位
void Cack(); //應(yīng)答位檢查
void Mnack(); //發(fā)送非應(yīng)答位
void Mack(); //發(fā)送應(yīng)答位
void Stop(); //停止IC總線
void Sta(); //啟動IC總線
void Wrbyt (uchar x) ; //寫一字節(jié)
uchar Rdbyt( ); //讀一字節(jié)
void Rdnbyt (); //讀n個數(shù)據(jù)
void Wrnbyt(uchar SLAwtemp ); //寫n個數(shù)據(jù)
void initial(); //I/O初始化子程序
voidLED();//
void delay(uint a);
void delay1();
#define SCL RB4 //4 ;24C01-6
#define SDA RB5 //5 ;24C01-5
#define SCLIO TRISB4 //4
#define SDAIO TRISB5 //5
#define SLAW 0x0A0//尋址字節(jié)寫
#define SLAR 0x0A1//尋址字節(jié)讀
uchar numer=1; //數(shù)碼管數(shù)據(jù)暫存區(qū)
unsigned char sun[10] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//////////////24C01/////////////////
//--------應(yīng)答位檢查---------
void Cack()
{
SCL=1;
SDAIO=1;//輸入1
SCLIO=0;
delay1();
err7=SDA;//c=1錯誤ERR
SCL=0;
}
///--------發(fā)送非應(yīng)答位----------
void Mnack()
{
SDA=1;
SCLIO=0;
SDAIO=0;//輸出
SCL=1;
delay1();
SCL=0;
SDA=0;
}
//-------發(fā)送應(yīng)答位---------------
void Mack()
{
SDA=0;
SCLIO=0;
SDAIO=0;//輸出
SCL=1;
delay1();
SCL=0;
SDA=1;
}
//--------停止IC總線---------------
void Stop()
{
SDAIO=0;//輸出
SCLIO=0;
SDA=0;
delay1();
SCL=1;
delay1();
SDA=1;
delay1();
}
//--------;啟動IC總線-------------
void Sta()
{
SDAIO=0;//輸出
SCLIO=0;
SDA=1;
delay1();
SCL=1;
delay1();
SDA=0;
delay1();
SCL=0;
}
//--------寫一字節(jié)------------------------
void Wrbyt(uchar x)
{
uchar i;
acc=x;
SCLIO=0;
SDAIO=0;
for(i=8;i>0;i--)
{
SDA=acc7;
SCL=1;
delay1();
SCL=0;
SDA=0;
acc=acc<<1;
}
}
//---------讀一字節(jié)-----------------
uchar Rdbyt( )
{
uchar i;
SCLIO=0;
SDAIO=1;//;輸入
for(i=8;i>0;i--)
{
acc=acc<<1;//接收數(shù)據(jù)左移寄存器
SCL=1;
delay1();
acc0=SDA;
SCL=0;
delay1();
}
return(acc);
}
////---------讀n個數(shù)據(jù)----------------
void Rdnbyt () //傳送數(shù)據(jù)個
{
Sta(); //啟動IC總線
delay1();
numer = Rdbyt();///讀一字節(jié)
delay1();
Stop(); //停止IC總線
}
//---------寫n個數(shù)據(jù)---------------
void Wrnbyt(uchar SLAwtemp )
{
err7=1;
if(err7==1)///c=1E0,ERR
{
Sta(); //啟動IC總線
delay1();
Wrbyt(SLAwtemp); //寫一字節(jié)
delay1();
Cack();//應(yīng)答位檢查-
}
delay1();
while(err7==1);
Stop(); //停止IC總線
}
//*************************************//
//系統(tǒng) 各I/O初始化子程序
void initial()
{
TRISC=0;
TRISA=0;
TRISB1=1;
TRISB2=1;
}
//LED
void LED ()
{
PORTA=0X1f;
PORTC=sun[numer];
}
void delay(uint a)
{
uint b=100;
while(a--)
{
while(b--);
}
}
void delay1()
{
NOP();
NOP();
NOP();
NOP();
}
void main()
{
initial();//系統(tǒng) 各I/O初始化子程序
LED();
delay(100);
Wrnbyt (SLAW);//尋址字節(jié)寫#0A0
delay1();
Wrnbyt (0x05);//寫地址0x05
delay1();
Wrnbyt (0x06);//寫數(shù)據(jù)0x06
delay1();
Wrnbyt (SLAR);//尋址字節(jié)讀#0A1
delay1();
Wrnbyt (0x05);//讀地址0x05
delay1();
Rdnbyt (); //讀數(shù)據(jù)
delay(5);
while(1)
{LED();}
}