江苏理工学院毕业设计说明书(论文)
RX_buffer <<= 1;
// If the bit is HIGH save 1 in
RX_buffer
} else {
RX_buffer <<= 1;
// If the bit is LOW save 0 in
RX_buffer |=0x01;
RX_buffer }
//--------------------------------------------------------------------------------------------- void send_bit(unsigned char bit_out) {
mSCL_HIGH(); delay_Tbuf();
// Set SCL line
delay_Thd();
// Tsu:dat = 250ns minimum
mSDA_OUT; if(bit_out)
mSDA_HIGH();
}
send_bit(ack_nack);
return RX_buffer;
// Sends acknowledgment bit
}
RX_buffer &=0xfe;
else
mSDA_LOW();
// High Level of Clock Pulse------------------ 第 22 页 共 61 页
江苏理工学院毕业设计说明书(论文)
mSCL_LOW(); delay_Tbuf();
// Clear SCL line
// Low Level of Clock Pulse----------------------
// Master release SDA line ,
// mSDA_HIGH();
return;
}//End of send_bit()
//---------------------------------------------------------------------------------------------
unsigned char Receive_bit(void) {
unsigned char Ack_bit;
mSDA_IN;
// SDA-input
_NOP();_NOP();_NOP(); mSCL_HIGH(); delay_Tbuf();
// Set SCL line
// High Level of Clock Pulse
// if(P2Input(BIT2))
return Ack_bit; SDA=1; if(SDA)
Ack_bit=1;
// \\ Read acknowledgment bit, save it in Ack_bit
else
Ack_bit=0;
// /
// Clear SCL line
mSCL_LOW(); delay_Tbuf();
// Low Level of Clock Pulse
}//End of Receive_bit
unsigned int MemRead(unsigned char SlaveAddress,unsigned char command) {
第 23 页 共 61 页
江苏理工学院毕业设计说明书(论文)
unsigned int tdata; unsigned char Pec; unsigned char DataL; unsigned char DataH; unsigned char arr[6]; unsigned char PecReg;
// Data storage (DataH:DataL)
// PEC byte storage
// Low data byte storage // High data byte storage // Buffer for the sent bytes
// Calculated PEC byte storage
// Defines the number of the attempts for
unsigned char ErrorCounter;
communication with MLX90614
ErrorCounter=200; do{ repeat:
STOP_bit();
//Pre-decrement ErrorCounter //ErrorCounter=0?
//Yes,go out from do-while{}
//If
slave
send
NACK
stop
// Initialising of ErrorCounter
comunication
--ErrorCounter; if(!ErrorCounter){ }
START_bit();
break;
//Start condition
if(TX_byte(SlaveAddress)){ //Send SlaveAddress }
//Send command
//Repeat comunication again
goto
repeat;
//Repeat comunication again
if(TX_byte(command)){ }
START_bit();
goto
repeat;
//Repeated Start condition
第 24 页 共 61 页
江苏理工学院毕业设计说明书(论文)
if(TX_byte(SlaveAddress)){ //Send SlaveAddress-------------------??? }
DataL=RX_byte(ACK);
//Read low data,master must send
goto
repeat; //Repeat comunication again
ACK
DataH=RX_byte(ACK); Pec=RX_byte(NACK);
//Read high data,master must send ACK
//Read PEC byte, master must send
NACK
STOP_bit();
arr[5]=SlaveAddress; arr[4]=command;
//
//
//Stop condition
arr[3]=SlaveAddress; arr[2]=DataL; arr[1]=DataH; arr[0]=0;
//Load array arr
// // //
PecReg=PEC_calculation(arr);//Calculate CRC
//If received and calculated CRC are equal go
}while(PecReg != Pec);
out from do-while{} }
unsigned char PEC_calculation(unsigned char pec[])
第 25 页 共 61 页
*((unsigned char *)(&tdata))=DataH; //
*((unsigned char *)(&tdata)+1)=DataL; //data=DataH:DataL
return tdata;
江苏理工学院毕业设计说明书(论文)
{
do{
crc[5]=0; crc[4]=0; crc[3]=0; crc[2]=0; crc[1]=0x01; crc[0]=0x07; BitPosition=47; shift=0;
/* Set maximum bit position at 47 */
/* Load CRC value 0x000000000107 */
unsigned char crc[6]; unsigned char unsigned char unsigned char unsigned char unsigned char
BitPosition=47; shift; i; j; temp;
//Find first 1 in the transmited message i=5; j=0;
while((pec[i]&(0x80>>j))==0 && i>0){
BitPosition--; if(j<7){ } else{ }
第 26 页 共 61 页
j=0x00; i--; j++;
/* Set highest index */