问题:此模拟IIC,在413上可以运行,读取和写入数据正常,但是在123上就不可以!
望各位高人指教!
#include <GLOBAL.h> #include <stdlib.h> #include <stdbool.h> #include <msp430x12x.h>
#define BIT0 (0x0001) #define BIT1 (0x0002) #define BIT2 (0x0004)
#define IicWpDirOut P1DIR |= BIT2 #define IicWpDirIn P1DIR &= ~BIT2 #define IicWpHig P1OUT |= BIT2 #define IicWpLow P1OUT &= ~BIT2
#define IicSclDirOut P1DIR |= BIT1 #define IicSclDirIn P1DIR &= ~BIT1 #define IicSclHig P1OUT |= BIT1 #define IicSclLow P1OUT &= ~BIT1
#define IicSdaDirOut P1DIR |= BIT0 #define IicSdaDirIn P1DIR &= ~BIT0 #define IicSdaHig P1OUT |= BIT0 #define IicSdaLow P1OUT &= ~BIT0 #define IicSdaInSt P1IN & BIT0
bool AckErr; unsigned char TimeDelay=10; //-----***-IIC延时(一个nop是纳秒 10个大概是1.67微秒大概总线速度在300K符合IIC在写状态下400K的要求)-***-----// void IicInit(void) { IicWpDirOut; IicSdaDirOut; IicSclDirOut; IicWpHig; IicSdaHig; IicSclHig; } //-----***-IIC启示-***-----// void IicStart(void) { IicSdaHig;//IicSda=1; Delay(TimeDelay); IicSclHig;//IicScl=1; Delay(TimeDelay); IicSdaLow;//IicSda=0; Delay(TimeDelay); IicSclLow;//IicScl=0; Delay(TimeDelay); } //-----***-IIC停止-***-----// void IicStop(void) { IicSdaLow;//IicSda=0; Delay(TimeDelay); IicSclHig;//IicScl=1; Delay(TimeDelay); IicSdaHig;//IicSda=1; Delay(TimeDelay); } //-----***-IIC错误-***-----// void IicErr(void) { AckErr=1; IicStop(); } //-----***-IIC应答-***-----// void SendAcknowledge(bool ack) { IicSdaDirOut; if(ack)//IicSda=ack; { IicSdaHig; } else { IicSdaLow; } Delay(TimeDelay); IicSclHig;//IicScl=1; Delay(TimeDelay); IicSclLow;//IicScl=0; Delay(TimeDelay); } //-----***-从slave端读取一个数据-***-----// unsigned char IicReceiveByte(void) { unsigned char i; unsigned char bytedata=0; IicSdaDirOut; IicSdaHig;//IicSda=1; IicSdaDirIn; for(i=0;i<8;i++) { IicSclHig;//IicScl=1; Delay(TimeDelay); bytedata<<=1; if(IicSdaInSt&BIT0) { bytedata |=0x01; } else { bytedata &=0xfe; } IicSclLow;//IicScl=0; Delay(TimeDelay); } IicSdaDirOut; return bytedata; } //-----***-传送一个 Byte 数据到 slave-***-----// bool IicSentByte(unsigned char DataByte) { unsigned char i; bool ack; ack=1; IicSdaDirOut; for(i=0;i<8;i++) { if(DataByte & 0x80) { IicSdaHig;//IicSda=1; } else { IicSdaLow;//IicSda=0; } DataByte <<=1; Delay(TimeDelay); IicSclHig;//IicScl=1; Delay(TimeDelay); IicSclLow;//IicScl=0; Delay(TimeDelay); } IicSdaHig;//IicSda=1; Delay(TimeDelay); IicSclHig;//IicScl=1; Delay(TimeDelay); IicSdaDirIn; if(IicSdaInSt&BIT0) { ack=0; } else { ack=1; } IicSdaDirOut; IicSclLow;//IicScl=0; Delay(TimeDelay); return ack; } //-----***-无扇区读---多字节-***-----// //读取指定器件无扇区指定地址多字节数据到指定地址 //Device=器件地址---单字节 //DataAdd=字节地址---单字节 void IicMuchByteRead(unsigned char Device,unsigned char DataAdd,unsigned char *DataDptr,unsigned char DataLend) { unsigned char i; IicWpHig; 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(); } //-----***-无扇区写---多字节-***-----// //写入指定器件无扇区指定地址中的多字节数据 //Device=器件地址---单字节 //DataAdd=字节地址---双字节 //DataByte=被存放数据的地址 //DataLend=被存放数据的长度 void IicMuchByteWrite(unsigned char Device,unsigned char DataAdd,unsigned char *DataDptr,unsigned char DataLend) { unsigned char i,j; for(i=0;i<3;i++) { AckErr=0; IicWpLow; IicStart(); if(IicSentByte(Device)){IicErr();continue;} if(IicSentByte(DataAdd)){IicErr();continue;} for(j=0;j<DataLend;j++) { if(IicSentByte(*DataDptr)){IicErr();continue;} DataDptr++; } IicStop(); if(!AckErr) { break; } } IicWpHig; Delay(50000); }
|