#i nclude <REG932.h> #i nclude <Define.h> #i nclude <Stdio.h> #i nclude <Intrins.h> //起始信號 //延時等候 void IicWait(void) { _nop_();//01 _nop_();//02 _nop_();//03 _nop_();//04 _nop_();//05 _nop_();//06 _nop_();//07 _nop_();//08 _nop_();//09 _nop_();//10 _nop_();//11 _nop_();//12 _nop_();//13 _nop_();//14 _nop_();//15 _nop_();//16 _nop_();//17 _nop_();//18 _nop_();//19 _nop_();//20 _nop_();//21 _nop_();//22 _nop_();//23 _nop_();//24 _nop_();//25 } void IicStart(void) { IicSda=1; IicWait(); IicScl=1; IicWait(); IicSda=0; IicWait(); IicScl=0; IicWait(); } //停止信號 void IicStop(void) { IicSda=0; IicWait(); IicScl=1; IicWait(); IicSda=1; IicWait(); }//應答信號 void SendAcknowledge(bit ack) { IicSda=ack; IicScl=1; IicWait(); IicScl=0; IicWait(); } //從slave端讀取一個數據*/ unsigned char IicReceiveByte(void) { unsigned char data i; unsigned char data bytedata=0; IicSda=1; for(i=0;i<8;i++) { IicScl=1; IicWait(); bytedata<<=1; if (IicSda) bytedata |=0x01; IicScl=0; IicWait(); } return bytedata; } //傳送一個 Byte 數據到 slave */ bit IicSentByte(unsigned char DataByte) { unsigned char data i; bit ack; for(i=0;i<8;i++) { if(DataByte & 0x80) { IicSda=1; } else { IicSda=0; } DataByte <<=1; IicWait(); IicScl=1; IicWait(); IicScl=0; IicWait(); } IicSda=1; IicWait(); IicScl=1; IicWait(); ack=IicSda; IicScl=0; IicWait(); return ack; } unsigned char FgTimeout; //無扇區讀---單字節 //讀取指定器件無扇區指定地址單字節的數據 //Device=器件地址---單字節 //DataAdd=字節地址---單字節 unsigned char IicByteRead(unsigned char Device,unsigned char DataAdd) { unsigned char data bytedata; EA=0; IicStart(); IicSentByte(Device); IicSentByte(DataAdd); IicStart(); IicSentByte(Device|0x01); bytedata=IicReceiveByte(); SendAcknowledge(1); IicStop(); EA=1; return bytedata; } //無扇區讀---多字節 //讀取指定器件無扇區指定地址多字節數據到指定地址 //Device=器件地址---單字節 //DataAdd=字節地址---單字節 void IicMuchByteRead(unsigned char Device,unsigned char DataAdd,unsigned char *DataDptr,unsigned char DataLend) { unsigned char data i; EA=0; IicStart(); IicSentByte(Device); IicSentByte(DataAdd); IicStart(); IicSentByte(Device|0x01); for(i=0;i<DataLend-1;i++) { *DataDptr=IicReceiveByte(); DataDptr++; SendAcknowledge(0); } *DataDptr=IicReceiveByte(); SendAcknowledge(1); IicStop(); EA=1; } /* //有扇區讀---單字節 //讀取指定器件有扇區指定地址中單字節的數據 //Device=器件地址---單字節 //DataPage=扇區地址---單字節 //DataAdd=字節地址---雙字節) unsigned char IicPageByteRead(unsigned char Device,unsigned char DataPage,unsigned char DataAdd) { unsigned char data bytedata; ET1=0; IicStart(); IicSentByte(Device); IicSentByte(DataPage); IicSentByte(DataAdd); IicStart(); IicSentByte(Device|0x01); bytedata=IicReceiveByte(); SendAcknowledge(1); IicStop(); ET1=1; return bytedata; } //有扇區讀---多字節 //讀取指定器件有扇區指定地址中的多字節數據 //Device=器件地址---單字節 //DataPage=扇區地址---單字節 //DataAdd=字節地址---雙字節) void IicPageMuchByteRead(unsigned char Device,unsigned char DataPage,unsigned char DataAdd,unsigned char DataLend) //讀取指定器件指定地址中的數據 { unsigned char i; ET1=0; IicStart(); IicSentByte(Device); IicSentByte(DataPage); IicSentByte(DataAdd); IicStart(); IicSentByte(Device|0x01); for(i=0;i<DataLend-1;i++) { IIcBuf[i]=IicReceiveByte(); SendAcknowledge(0); } IIcBuf[i]=IicReceiveByte(); SendAcknowledge(1); IicStop(); ET1=1; } */ //無扇區寫---單字節 //寫入指定器件無扇區指定地址中的單字節數據 //Device=器件地址---單字節 //DataAdd=字節地址---雙字節 void IicByteWrite(unsigned char Device,unsigned char DataAdd,unsigned char DataByte) { unsigned char data i; bit ack; EA=0; IicWp=0; Delay(50); for(i=0;i<3;i++) { IicStart(); ack=IicSentByte(Device);if(ack==1){IicStop();continue;} ack=IicSentByte(DataAdd);if(ack==1){IicStop();continue;} ack=IicSentByte(DataByte);if(ack==1){IicStop();continue;} IicStop(); if(ack==0)break; } IicWp=1; EA=1; Delay(500); } //無扇區寫---多字節 //寫入指定器件無扇區指定地址中的多字節數據 //Device=器件地址---單字節 //DataAdd=字節地址---雙字節 //DataByte=被存放數據的地址 //DataLend=被存放數據的長度 void IicMuchByteWrite(unsigned char Device,unsigned char DataAdd,unsigned char *DataDptr,unsigned char DataLend) { unsigned char data i,j; bit ack; EA=0; IicWp=0; for(i=0;i<3;i++) { IicStart(); ack=IicSentByte(Device);if(ack==1){IicStop();continue;} ack=IicSentByte(DataAdd);if(ack==1){IicStop();continue;} for(j=0;j<DataLend;j++) { ack=IicSentByte(*DataDptr);if(ack==1){IicStop();continue;} DataDptr++; } IicStop(); if(ack==0)break; } IicWp=1; EA=1; Delay(500); } /* //有扇區寫---單字節 //寫入指定器件有扇區指定地址中的單字節數據 //Device=器件地址---單字節 //DataPage=扇區地址---單字節 //DataAdd=字節地址---雙字節 void IicPageByteWrite(unsigned char Device,unsigned char DataPage,unsigned char DataAdd,unsigned char DataByte) { unsigned char data i; bit ack; ET1=0; FgTimeout=1; IicWp=0; for(i=0;i<3;i++) { IicStart(); ack=IicSentByte(Device);if(ack==1){IicStop();continue;} ack=IicSentByte(DataPage);if(ack==1){IicStop();continue;} ack=IicSentByte(DataAdd);if(ack==1){IicStop();continue;} ack=IicSentByte(DataByte); if(ack==1){IicStop();continue;} IicStop(); FgTimeout=0; if(ack==0)break; } Delay(50); IicWp=1; ET1=1; } //有扇區寫---多字節 //寫入指定器件有扇區指定地址中的多字節數據 //Device=器件地址---單字節 //DataPage=扇區地址---單字節 //DataAdd=字節地址---雙字節 //DataByte=被存放數據的地址 //DataLend=被存放數據的長度 void IicPageMuchByteWrite(unsigned char Device,unsigned char DataPage,unsigned char DataAdd,unsigned int *DataDptr,unsigned char DataLend) { unsigned char data i,j; bit ack; ET1=0; FgTimeout=1; IicWp=0; for(i=0;i<3;i++) { IicStart(); ack=IicSentByte(Device);if(ack==1){IicStop();continue;} ack=IicSentByte(DataPage);if(ack==1){IicStop();continue;} ack=IicSentByte(DataAdd);if(ack==1){IicStop();continue;} for(j=0;j<DataLend;j++) { ack=IicSentByte(*DataDptr); if(ack==1){IicStop();continue;} DataDptr++; } IicStop(); FgTimeout=0; if(ack==0)break; } Delay(50); IicWp=1; ET1=1; } */ |