//------------------------IObitsInput-----------#defineP1Input(n)P1IN&n?1:0#defineP2Input(n)P2IN&n?1:0#defineP3Input(n)P3IN&n?1:0#defineP4Input(n)P4IN&n?1:0#defineP5Input(n)P5IN&n?1:0#defineP6Input(n)P6IN&n?1:0//--------------------------------------------------------------------------------#ifndef_MLX90614_H_#define_MLX90614_H_//-----------------------------------#defineSMBus_SDABIT0#defineSMBus_SCLBIT1//#defineSDA_1P2OUT|=SMBus_SDA//#defineSDA_0P2OUT&=~SMBus_SDA#defineSDA_1P2DIR&=~SMBus_SDA#defineSDA_0P2DIR|=SMBus_SDA#defineSCL_1P2OUT|=SMBus_SCL#defineSCL_0P2OUT&=~SMBus_SCL#definemSDA_INP2DIR&=~SMBus_SDA#definemSDA_OUTP2DIR|=SMBus_SDA#definemSCL_OUTP2DIR|=SMBus_SCL#defineSDA_READP2IN&SMBus_SDA#defineACK0#defineNACK1//MLX90614constants#defineSA0x00//Slaveaddress#defineDEFAULT_SA0x5A//DefaultSlaveaddress#defineRAM_Access0x00//RAMaccesscommand#defineEEPROM_Access0x20//EEPROMaccesscommand#defineRAM_Tobj10x07//To1addressintheeeprom//*PROTOTYPES*******************************voidSTART_bit(void);voidSTOP_bit(void);unsignedcharTX_byte(unsignedcharTx_buffer);unsignedcharRX_byte(unsignedcharack_nack);voidsend_bit(unsignedcharbit_out);unsignedcharReceive_bit(void);voidMLX90614_init(void);unsignedintMemRead(unsignedcharSlaveAddress,unsignedcharcommand);voidSendRequest(void);voidDummyCommand(unsignedcharbyte);floatCalcTemp(unsignedintvalue);unsignedcharPEC_calculation(unsignedcharpec[]);#endif//----------------------------------------------------------------------voidSM_Wait(void){_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();}voidMLX90614_init(void){mSDA_OUT;//SetSDAasOutputP2OUT&=~SMBus_SDA;mSCL_OUT;//SetSCLasOutputSCL_1;SDA_1;SendRequest();}voidSTART_bit(void){//mSDA_OUT;SDA_1;SM_Wait();SCL_1;SM_Wait();SDA_0;SM_Wait();SCL_0;SM_Wait();}voidSTOP_bit(void){//mSDA_OUT;SCL_0;SM_Wait();//WaitafewmicrosecondsSDA_0;SM_Wait();SCL_1;SM_Wait();//Stopconditionsetuptime(Tsu:sto=4.0usmin)SDA_1;SM_Wait();//SetSDAline}//---------------------------------------------------------------------------------------------voidsend_bit(unsignedcharbit_out){//mSDA_OUT;if(bit_out)SDA_1;elseSDA_0;SM_Wait();SCL_1;SM_Wait();SCL_0;SM_Wait();//return;}//Endofsend_bit()//---------------------------------------------------------------------------------------------unsignedcharTX_byte(unsignedcharTx_buffer){unsignedcharBit_counter;unsignedcharAck_bit;unsignedcharbit_out;for(Bit_counter=8;Bit_counter;Bit_counter--){if(Tx_buffer&0x80)bit_out=1;//IfthecurrentbitofTx_bufferis1setbit_outelsebit_out=0;//elseclearbit_outsend_bit(bit_out);//SendthecurrentbitonSDATx_buffer=1;//Getnextbitforchecking}SM_Wait();Ack_bit=Receive_bit();//GetacknowledgmentbitreturnAck_bit;}//EndofTX_bite()//---------------------------------------------------------------------------------------------unsignedcharReceive_bit(void){unsignedcharAck_bit;mSDA_IN;//SDA-input_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();SCL_1;//SetSCLlineSM_Wait();//HighLevelofClockPulseif(SDA_READ)Ack_bit=1;//\Readacknowledgmentbit,saveitinAck_bitelseAck_bit=0;///SCL_0;//ClearSCLlineSM_Wait();//LowLevelofClockPulsereturnAck_bit;}//EndofReceive_bitunsignedcharRX_byte(unsignedcharack_nack){unsignedcharRX_buffer;unsignedcharBit_Counter;for(Bit_Counter=8;Bit_Counter;Bit_Counter--){if(Receive_bit())//GetabitfromtheSDAline{RX_buffer=1;//IfthebitisHIGHsave1inRX_bufferRX_buffer|=0x01;}else{RX_buffer=1;//IfthebitisLOWsave0inRX_bufferRX_buffer&=0xfe;}}send_bit(ack_nack);//SendsacknowledgmentbitreturnRX_buffer;}//----------------------------------------------------------------------------------------------unsignedintMemRead(unsignedcharSlaveAddress,unsignedcharcommand){unsignedintdata;//Datastorage(DataH:DataL)unsignedcharPec;//PECbytestorageunsignedcharDataL;//LowdatabytestorageunsignedcharDataH;//Highdatabytestorageunsignedchararr[6];//BufferforthesentbytesunsignedcharPecReg;//CalculatedPECbytestorageunsignedcharErrorCounter;//DefinesthenumberoftheattemptsforcommunicationwithMLX90614ErrorCounter=255;//InitialisingofErrorCounterdo{repeat:STOP_bit();//IfslavesendNACKstopcomunicationif(!ErrorCounter--){//ErrorCounter=0?break;//Yes,gooutfromdo-while{}}START_bit();//Startconditionif(TX_byte(SlaveAddress)){//SendSlaveAddressgotorepeat;//Repeatcomunicationagain}if(TX_byte(command)){//Sendcommandgotorepeat;//Repeatcomunicationagain}START_bit();//RepeatedStartconditionif(TX_byte(SlaveAddress)){//SendSlaveAddressgotorepeat;//Repeatcomunicationagain}DataL=RX_byte(ACK);//Readlowdata,mastermustsendACKDataH=RX_byte(ACK);//Readhighdata,mastermustsendACKPec=RX_byte(NACK);//ReadPECbyte,mastermustsendNACKSTOP_bit();//Stopconditionarr[5]=SlaveAddress;//arr[4]=command;//arr[3]=SlaveAddress;//Loadarrayarrarr[2]=DataL;//arr[1]=DataH;//arr[0]=0;//PecReg=PEC_calculation(arr);//CalculateCRC}while(PecReg!=Pec);//IfreceivedandcalculatedCRCar