#include #include #include #include <msp430x41x.h>//------------------------------------------#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 & BIT7bool 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---IicSdaDirInIIcDelay();IicSclHig;//IicScl=1;IIcDelay();IicSclLow;//IicScl=0;IIcDelay();}//-----***-無扇區(qū)讀---多字節(jié)-***-----////讀取指定器件無扇區(qū)指定地址多字節(jié)數據到指定地址//Device=器件地址---單字節(jié)//DataAdd=字節(jié)地址---單字節(jié)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();}//-----***-無扇區(qū)寫---多字節(jié)-***-----////寫入指定器件無扇區(qū)指定地址中的多字節(jié)數據//Device=器件地址---單字節(jié)//DataAdd=字節(jié)地址---雙字節(jié)//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
評論