avr單片機(jī)IIc總線編程
/*=========================================
IicWrite(uchar ad,uchar da):ad=寄存器地址,da=數(shù)據(jù);
IicRead(uchar ad): ad=寄存器地址
=========================================*/
void IicWrite(uchar ad,uchar da)
{
IIC_Start();
IIC_Wait();
TWDR=0xA2;//從機(jī)地址
IIC_Ack();
IIC_Wait();
TWDR=ad;
IIC_Ack();
IIC_Wait();
TWDR=da;
IIC_Ack();
IIC_Wait();
IIC_Stop();
}
uchar IicRead(uchar ad)
{
unsigned char data;
IIC_Start();
IIC_Wait();
TWDR = 0xA2;//從機(jī)地址
IIC_Ack();
IIC_Wait();
TWDR = ad;
IIC_Ack();
IIC_Wait();
IIC_Start();
IIC_Wait();
TWDR = 0xA3;
IIC_Ack();
IIC_Wait();
IIC_NoneAck();
IIC_Wait();
data=TWDR;
IIC_Stop();
return data;
}
單片機(jī)工作在IIC工作在主機(jī)方式下,對(duì)從機(jī)讀取數(shù)據(jù)及寫入數(shù)據(jù).已經(jīng)調(diào)試通過.希望大家能夠補(bǔ)充完善.
評(píng)論