//---------------------------------------------------------------------------------- char sht_read_statusreg(void) //---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) { unsigned char error=0; unsigned char tmp = 0; unsigned char bits = 8; sht_crc_init(); sht_transstart(); //transmission start error=sht_write_byte(STATUS_REG_R); //send command to sensor sht_crc_shuffle_byte(STATUS_REG_R); StatusReg =sht_read_byte(ACK); //read status register (8-bit) sht_crc_shuffle_byte(StatusReg); CheckSum =sht_read_byte(noACK); //read checksum (8-bit) while( bits--) { tmp >>=1; if( sht_crc & 0b10000000) tmp |= 0b10000000; sht_crc <<=1; } if (tmp != CheckSum) { SHTdevice_table[CurrentSensorIndex].crc++; error++; } return error; //error=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) //---------------------------------------------------------------------------------- char sht_write_statusreg(unsigned char *p_value) { unsigned char error=0; sht_transstart(); //transmission start error+=sht_write_byte(STATUS_REG_W);//send command to sensor error+=sht_write_byte(*p_value); //send value of status register return error; //error>=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) //---------------------------------------------------------------------------------- char sht_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) { unsigned char error=0; sht_transstart(); //transmission start error=sht_write_byte(STATUS_REG_R); //send command to sensor *p_value=sht_read_byte(ACK); //read status register (8-bit) *p_checksum=sht_read_byte(noACK); //read checksum (8-bit) return error; //error=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- // start measurement (humidity/temperature) //---------------------------------------------------------------------------------- char sht_measure_start(unsigned char mode) { sht_transstart(); // transmission start switch(mode) { // send command to sensor case TEMP: return(sht_write_byte(MEASURE_TEMP)); //break; case HUMI: return(sht_write_byte(MEASURE_HUMI)); //break; default: break; } return 1; // error }
//---------------------------------------------------------------------------------- unsigned char sht_write_statusreg(unsigned char *p_sht_value, unsigned char pin) //---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) { unsigned char error=0; sht_transstart(pin); //transmission start error+=sht_write_byte(STATUS_REG_W, pin);//send command to sensor error+=sht_write_byte(*p_sht_value, pin); //send value of status register return error; //error>=1 in case of no response form the sensor }
void sht_connectionreset(void) { unsigned char i; SHT_DATA_OUT(0); SHT_SCK(0); //Initial state //for(i=0;i<9;i++) //9 SCK cycles for(i=9;i!=0;i--) //9 SCK cycles (detecting 0 is easier - says TI) { SHT_SCK(1); delay_us(1); SHT_SCK(0); } sht_transstart(); //transmission start }
//---------------------------------------------------------------------------------- char sht_measure(unsigned char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned error=0; unsigned int i; unsigned char LSB; unsigned char MSB; unsigned char tmp = 0; unsigned char bits = 8; sht_crc_init(); sht_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error+=sht_write_byte(MEASURE_TEMP); sht_crc_shuffle_byte(MEASURE_TEMP);break; case HUMI : error+=sht_write_byte(MEASURE_HUMI); sht_crc_shuffle_byte(MEASURE_HUMI);break; default : break; } for (i=0;i<40000;i++) { if(DATA_IN==0) { break; //wait until sensor has finished the measurement } Delay100TCYx(4); //50 us } if(DATA_IN) { error+=1; // or timeout (~2 sec.) is reached } MSB = sht_read_byte(ACK); //read the first byte (MSB) sht_crc_shuffle_byte(MSB); LSB = sht_read_byte(ACK); //read the second byte (LSB) sht_crc_shuffle_byte(LSB); CurrentValue = MSB * 256 + LSB; CheckSum = sht_read_byte(noACK); //read checksum while( bits--) { tmp >>=1; if( sht_crc & 0b10000000) tmp |= 0b10000000; sht_crc <<=1; } if (tmp != CheckSum) { SHTdevice_table[CurrentSensorIndex].crc++; return 1; } return error; }
void sht_connectionreset( void ) { unsigned char i; // Make RC5 input TRISCbits.TRISC5 = 1; PORTCbits.RC5 = 1; // DATA = 1 PORTCbits.RC3 = 0; // CLK = 0 for( i = 0; i < 9; i++ ) { // 9 SCK cycles PORTCbits.RC3 = 1; // CLK = 1 PORTCbits.RC3 = 0; // CLK = 0 } sht_transstart(); // transmission start }
//---------------------------------------------------------------------------------- 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 }
char sht_measure( unsigned char *p_value, unsigned char *p_checksum, unsigned char mode ) { unsigned error=0; unsigned long i; sht_transstart(); // transmission start switch( mode ) { // send command to sensor case SHT_MODE_TEMP: error += sht_write_byte( MEASURE_TEMP ); break; case SHT_MODE_HUMI: error += sht_write_byte( MEASURE_HUMI ); break; default: break; } // wait until sensor has finished the measurement sht_measure_timer = 0; while ( sht_measure_timer < SHT_READ_TIMEOUT ) { if ( 0 == ( PORTCbits.RC5 ) ) { break; } } if ( PORTCbits.RC5 ) { // or timeout (~2 sec.) is reached error += 1; } *(p_value) = sht_read_byte( ACK ); // read the first byte (MSB) *(p_value+1)= sht_read_byte( ACK ); // read the second byte (LSB) *p_checksum = sht_read_byte( noACK ); // read checksum return error; }
//---------------------------------------------------------------------------------- void sht_connectionreset(unsigned char pin) //---------------------------------------------------------------------------------- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart { unsigned char i; MAKE_SHT_DATA_PIN_OUTPUT(pin); asm volatile ("nop"::); SET_SHT_DATA(pin); CLEAR_SHT_SCK; //Initial state for(i=0;i<9;i++) //9 SCK cycles { SET_SHT_SCK; asm volatile ("nop"::); asm volatile ("nop"::); CLEAR_SHT_SCK; asm volatile ("nop"::); asm volatile ("nop"::); } sht_transstart(pin); //transmission start }