#include#include #include //------------------------------------------ #define IicWpDirOut P6DIR |= BIT5 #define IicWpLow P6OUT &=~ BIT5 #define IicWpHig P6OUT |= BIT5 #define IicSclDirOut P6DIR |= BIT6 #define IicSclLow P6OUT &=~ BIT6 #define IicSclHig P6OUT |= BIT6 #define IicSdaDirOut P6OUT &= ~BIT7 #define IicSdaLow P6DIR |= BIT7 #define IicSdaHig P6DIR &=~ BIT7 #define IicSdaInSt P6IN & BIT7 bool AckErr; //------------------------------ *** - 时间延时 - *** ------------------------------// void Delay(unsigned int count) { unsigned int i; _NOP(); _NOP(); for(i=count; i>0; i--); } void IIcDelay(void) { _NOP(); } //-----***-IIC初始化-***-----// void IicInit(void) { IicWpDirOut; IicSdaDirOut; IicSclDirOut; IicWpHig; IicSdaHig; IicSclHig; } //-----***-IIC启示-***-----// void IicStart(void) { IicSdaHig;//IicSda=1; IIcDelay(); IicSclHig;//IicScl=1; IIcDelay(); IicSdaLow;//IicSda=0; IIcDelay(); IicSclLow;//IicScl=0; IIcDelay(); } //-----***-IIC停止-***-----// void IicStop(void) { IicSdaLow;//IicSda=0; IIcDelay(); IicSclHig;//IicScl=1; IIcDelay(); IicSdaHig;//IicSda=1; IIcDelay(); } //-----***-IIC应答-***-----// void SendAcknowledge(bool ack) { IicSdaDirOut; if(ack)//IicSda=ack; { IicSdaHig; } else { IicSdaLow; } IIcDelay(); IicSclHig;//IicScl=1; IIcDelay(); IicSclLow;//IicScl=0; IIcDelay(); } //-----***-从slave端读取一个数据-***-----// unsigned char IicReceiveByte(void) { unsigned char i; unsigned char bytedata=0; IicSdaDirOut; IicSdaHig;//IicSda=1; for(i=0;i<8;i++) { IicSclHig;//IicScl=1; IIcDelay(); bytedata<<=1; if(IicSdaInSt) { bytedata |=0x01; } else { bytedata &=0xfe; } IicSclLow;//IicScl=0; IIcDelay(); } IicSdaDirOut; return bytedata; } //-----***-传送一个 Byte 数据到 slave-***-----// void IicSentByte(unsigned char DataByte) { unsigned char i; for(i=0;i<8;i++) { if(DataByte & 0x80) { IicSdaHig;//IicSda=1; } else { IicSdaLow;//IicSda=0; } DataByte <<=1; IIcDelay(); IicSclHig;//IicScl=1; IIcDelay(); IicSclLow;//IicScl=0; IIcDelay(); } IicSdaHig;//IicSda=1;-----Y------D---IicSdaDirIn IIcDelay(); IicSclHig;//IicScl=1; IIcDelay(); IicSclLow;//IicScl=0; IIcDelay(); } //-----***-无扇区读---多字节-***-----// //读取指定器件无扇区指定地址多字节数据到指定地址 //Device=器件地址---单字节 //DataAdd=字节地址---单字节 void IicMuchByteRead(unsigned char DataAdd,unsigned char *DataDptr,unsigned char DataLend) { unsigned char i; _DINT(); WDT(); IicInit(); IicStart(); IicSentByte(0xa0); IicSentByte(DataAdd); IicStart(); IicSentByte(0xa1); for(i=DataLend-1;i>0;i--) { *DataDptr=IicReceiveByte(); DataDptr++; SendAcknowledge(0); } *DataDptr=IicReceiveByte(); SendAcknowledge(1); IicStop(); WDT(); Delay(500); _EINT(); } //-----***-无扇区写---多字节-***-----// //写入指定器件无扇区指定地址中的多字节数据 //Device=器件地址---单字节 //DataAdd=字节地址---双字节 //DataByte=被存放数据的地址 //DataLend=被存放数据的长度 void IicMuchByteWrite(unsigned char DataAdd,unsigned char *DataDptr,unsigned char DataLend) { unsigned char i; _DINT(); WDT(); IicInit(); IicWpLow; IicStart(); IicSentByte(0xa0); IicSentByte(DataAdd); for(i=DataLend;i>0;i--) { IicSentByte(*DataDptr); DataDptr++; } IicStop(); WDT(); Delay(3500); IicWpHig; _EINT(); } //---特别说明 //---利用430芯片来模拟IIC,是不能够把SDA设置为输出模式而是 //---设置为输入,然后利用方向控制器来输出高或者低 //---如下: #define IicSdaDirOut P6OUT &= ~BIT7 #define IicSdaLow P6DIR |= BIT7 #define IicSdaHig P6DIR &=~ BIT7 #define IicSdaInSt P6IN & BIT7
上一篇:矩阵键盘扫描
下一篇:at45db161驱动
推荐阅读最新更新时间:2024-03-16 14:24