avr單片機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;//從機地址
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;//從機地址
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;
}
單片機工作在IIC工作在主機方式下,對從機讀取數(shù)據(jù)及寫入數(shù)據(jù).已經(jīng)調(diào)試通過.希望大家能夠補充完善.
評論