//---------------------------------------------------------------------------------- char sht_write_byte(unsigned char value) //---------------------------------------------------------------------------------- // writes a byte on the Sensibus and checks the acknowledge { unsigned char i,error=0; for (i=0x80;i>0;i/=2) //shift bit for masking { if (i & value) { DATA(1); // TRISCbits.RC5 = 1; //masking value with i , write to SENSI-BUS } else { DATA(0); //PORTCbits.RC5 = 0; TRISCbits.RC5 = 0; } Delay10TCYx(2*sht_speed); SCK(1); //clk for SENSI-BUS Delay10TCYx(2*sht_speed); SCK(0); Delay10TCYx(2*sht_speed); } DATA(1); //TRISCbits.RC5 = 1; //release DATA-line Delay10TCYx(2*sht_speed); SCK(1); //clk #9 for ack Delay10TCYx(2*sht_speed); error=DATA_IN; //check ack (DATA will be pulled down by SHT11) SCK(0); Delay10TCYx(2*sht_speed); return error; //error=1 in case of no acknowledge }
//---------------------------------------------------------------------------------- char sht_read_byte(unsigned char ack) //---------------------------------------------------------------------------------- // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" { unsigned char i,val=0; DATA(1);//TRISCbits.RC5 = 1; //release DATA-line for (i=0x80;i>0;i/=2) //shift bit for masking { SCK(1); //clk for SENSI-BUS Delay10TCYx(2*sht_speed); if (DATA_IN) val=(val | i); //read bit SCK(0); Delay10TCYx(sht_speed); } if (ack){ DATA(0); //PORTCbits.RC5 = 0; TRISCbits.RC5 = 0; } else { DATA(1); //TRISCbits.RC5 = 1; } //in case of "ack==1" pull down DATA-Line SCK(1); //clk #9 for ack Delay10TCYx(2*sht_speed); SCK(0); //release DATA-line DATA(1);//TRISCbits.RC5 = 1; return val; }
//---------------------------------------------------------------------------------- void sht_connectionreset(void) //---------------------------------------------------------------------------------- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart // _____________________________________________________ ________ // DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ { unsigned char i; DATA(1);//TRISCbits.RC5 = 1; Delay10TCYx(2*sht_speed); SCK(0); //Initial state Delay10TCYx(2*sht_speed); for(i=0;i<9;i++) //9 SCK cycles { SCK(1); Delay10TCYx(2*sht_speed); SCK(0); Delay10TCYx(2*sht_speed); } sht_transstart(); //transmission start }
/************************************************** Function: SPI_RW(); Description: Writes one UINT8 to RFM73, and return the UINT8 read // **************************************************/ UINT8 SPI_RW(UINT8 inputValue) { UINT8 curBit; UINT8 outputValue = 0x00; //int i; for(curBit = 0; curBit < 8; curBit++) // output 8-bit { MOSI(inputValue & 0x80); inputValue *= 2; // shift next bit into MSB.. SCK(1); outputValue *= 2; outputValue += MISO; //LED(MISO); SCK(0); // ..then set SCK low again } return(outputValue); // return read UINT8 }
//---------------------------------------------------------------------------------- void sht_transstart(void) //---------------------------------------------------------------------------------- // generates a transmission start // _____ ________ // DATA: |_______| // ___ ___ // SCK : ___| |___| |______ { DATA(1);//TRISCbits.RC5 = 1; SCK(0); //Initial state Delay10TCYx(2*sht_speed); SCK(1); Delay10TCYx(2*sht_speed); DATA(0);//PORTCbits.RC5 = 0; TRISCbits.RC5 = 0; Delay10TCYx(2*sht_speed); SCK(0); Delay10TCYx(2*sht_speed); SCK(1); Delay10TCYx(2*sht_speed); DATA(1); //TRISCbits.RC5 = 1; Delay10TCYx(2*sht_speed); SCK(0); }