//---------------------------------------------------------------------------------- u8 s_measure(u16 *p_value, u16 *p_checksum, u8 mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned short error=0; // unsigned int i; u16 sht11_msb, sht11_lsb; s_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } if(error != 0){return error;} /* Enable External_Interrupt1 of SHT11. */ MAKE_I2CDATA_INPUT(); //SHT11_INTERRUPT_ENABLE(); while(1){ sht11_delay(500); if(READ_I2CDATA_PIN() == 0) break; } MAKE_I2CDATA_INPUT(); sht11_msb = s_read_byte(ACK); //read the first byte (MSB) sht11_lsb = s_read_byte(ACK); //read the second byte (LSB) *p_value = (sht11_msb * 256) + sht11_lsb; *p_checksum =s_read_byte(noACK); //read checksum acq_complete = false; return error; }
bool sht11_write(SHT11_t *structure, unsigned char cmd, unsigned char *status_reg) { /* * If flag busy is true, signify that a command has been already send * and now we will wait to read data. * If flag busy is true will return true, to return signal that the command is send * and now we will wait for response. */ if(structure->busy) return true; /* * This is the single case that this function run forward. * This will go on only if a is called for first time, response is read * nack is received or timeout is occurred. * Now will set the timeout in millisecond and send the command. */ timer_interval(&structure->Timeout_Timer, 500); structure->busy = true; gpio_function_set(structure->Scl, GPIO_OUT_OPEN_DRAIN); gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); /* Send transmission start. * ___ ___ * data = \_____/ * ___ ___ * clk = _/ \_/ \__ */ gpio_out(structure->Scl, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Sda, 0); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Sda, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); /* * Send command. */ unsigned char data_cnt = 0; unsigned char _cmd = cmd; for(; data_cnt < 8; data_cnt++) { if(_cmd & 0x80) gpio_out(structure->Sda, 1); else gpio_out(structure->Sda, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_out(structure->Sda, 1); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); } sht11_delay(structure); _cmd = _cmd << 1; } gpio_out(structure->Scl, 1); sht11_delay(structure); signed int ack = gpio_in(structure->Sda); gpio_out(structure->Scl, 0); sht11_delay(structure); if(ack) { structure->busy = false; return false; } structure->reg_inst = cmd; if(cmd == SHT11_WRITE_STATUS_REG) { /* * Write status register */ gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); unsigned char st_reg = *status_reg; for(data_cnt = 0; data_cnt < 8; data_cnt++) { if(st_reg & 0x80) gpio_out(structure->Sda, 1); else gpio_out(structure->Sda, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_out(structure->Sda, 1); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); } sht11_delay(structure); st_reg = st_reg << 1; } gpio_out(structure->Scl, 1); sht11_delay(structure); signed int ack = gpio_in(structure->Sda); gpio_out(structure->Scl, 0); sht11_delay(structure); if(ack) { structure->busy = false; return false; } structure->busy = false; } else if(cmd == SHT11_READ_STATUS_REG) { /* * Read status register. */ unsigned char st_reg = 0; for(data_cnt = 0; data_cnt < 8; data_cnt++) { st_reg = st_reg << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) st_reg |= 1; gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); gpio_out(structure->Sda, 0); } sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); sht11_delay(structure); *status_reg = st_reg; /* * Read checksum. */ unsigned char chk = 0; for(data_cnt = 0; data_cnt < 8; data_cnt++) { chk = chk << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) chk |= 1; gpio_out(structure->Scl, 0); sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); structure->busy = false; } return true; }
bool sht11_read(SHT11_t *structure) { /* * Check if sht11 is not responding. * If sht11 will not respond until timeout will occurred, * the busy flag will be set as false to permit to sht11_write function * to send another command. */ if(timer_tick(&structure->Timeout_Timer)) { structure->busy = false; return false; } /* * If busy flag is not busy this function will don't have anything to read from sh11. */ if(!structure->busy) return false; /* * If flag busy is true w will wait to read data. * Now will wait the sda pin to be '0', that signify data ready to be read. * If sda pin is '1' will return false, this signify that the sht11 is busy. */ if(gpio_in(structure->Sda)) return false; /* * Now the data is available on the buss. * Will start to read data and return true, this is the single case that * this function return true. */ unsigned char data_cnt; sht11_delay(structure); unsigned short data = 0; /* * Read MSB data */ for(data_cnt = 0; data_cnt < 8; data_cnt++) { data = data << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) data |= 1; gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); gpio_out(structure->Sda, 0); } sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); sht11_delay(structure); /* * Read LSB data. */ for(data_cnt = 0; data_cnt < 8; data_cnt++) { data = data << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) data |= 1; gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); gpio_out(structure->Sda, 0); } sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); sht11_delay(structure); /* * Read checksum. */ unsigned char chk = 0; for(data_cnt = 0; data_cnt < 8; data_cnt++) { chk = chk << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) chk |= 1; gpio_out(structure->Scl, 0); sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); structure->busy = false; if(structure->reg_inst == SHT11_START_MEASURE_TEMPERATURE) { structure->temperature = structure->vdd_comp + 0.01 * (float)data; } else if(structure->reg_inst == SHT11_START_MEASURE_HUMIDITY) { structure->humidity = -4 + 0.0405 * (float)data + -0.0000028 * ((float)data * (float)data); } return true; }