// sht1x_transstart
// generates a transmission start
// ___ ___
// SCK : ___| |___| |______
// _____ ________
// DATA: |_______|
void sht1x_transstart(void)
{
IO0CLR = SENSIBUS_DATA_OUT_PIN; // data=1
IO0SET = SENSIBUS_SCK_PIN; // sck=0
delay_mcu(32); // 4us
IO0CLR = SENSIBUS_SCK_PIN; // sck=1
delay_mcu(32); // 4us
IO0SET = SENSIBUS_DATA_OUT_PIN; // data=0
IO0SET = SENSIBUS_SCK_PIN; // sck=0
delay_mcu(64); // 8us
IO0CLR = SENSIBUS_SCK_PIN; // sck=1
delay_mcu(32); // 4us
IO0CLR = SENSIBUS_DATA_OUT_PIN; // data=1
delay_mcu(32); // 4us
IO0SET = SENSIBUS_SCK_PIN; // sck=0
delay_mcu(32); // 4us
}
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
void sht1x_connectionreset(void)
{
IO0CLR = SENSIBUS_DATA_OUT_PIN; // data=1
IO0SET = SENSIBUS_SCK_PIN; // sck=0
delay_mcu(64); // 8us
for(U32 i=0; i<9; i++) // 9 SCK cycles
{
IO0CLR = SENSIBUS_SCK_PIN; // sck=1
delay_mcu(32); // 2us
IO0SET = SENSIBUS_SCK_PIN; // sck=0
delay_mcu(32); // 2us
}
sht1x_transstart(); //transmission start
}
// sht1x init: sensibus is two-wire machine: sck/data, now we apply 3 wire: sck/data_out/data_in for our system
void sht1x_init(void)
{
// init io sck/data_out pin as output
IO0DIR |= SENSIBUS_SCK_PIN;
IO0DIR |= SENSIBUS_DATA_OUT_PIN;
// init io data_in pin as input
IO0DIR = IO0DIR & (~(SENSIBUS_DATA_IN_PIN));
}
/*
* sht1x_measure
*
* Description:
* Measure temperature and relative rumidity with blocking loop delay, it is about 300ms for 14bit temperature(from our experiment), and
* about 100ms for 12 bit relative humidity(from datasheet). The measure process include checksum.
*
* Parameter:
* p_value: the address of 16bit unsigned int(U16)
* checksum: checksum byte from sensi-sensor
* mode: 0; temprature; 1: relative humidity;
*
* History:
* 2010/08/29: Init design.
*/
U8 sht1x_measure(U8 *p_value, U8 *p_checksum, U8 mode)
{
U32 i = 0;
U8 error = 0;
// do convert
error = sht1x_do_convert(mode);
//wait until sensor has finished the measurement with 2s timeout
for(i=0; i<20; i++) {
delay_ms(100);
watchdog_soft_hard_feed();
if( (IO0PIN&SENSIBUS_DATA_IN_PIN) != 0x00 ) { break; } // if data==0
}
// test measure time
//uart0_putnum(i, 10);
// read result
error = sht1x_read_result(p_value, p_checksum);
return error;
}
// sht1x_do_temperature_convert
U8 sht1x_do_convert(U8 mode)
{
U8 error = 0;
//transmission start
sht1x_connectionreset();
//send command to sensor
switch(mode){
case 0:
error += sht1x_write_byte(MEASURE_TEMP);
break;
case 1:
error += sht1x_write_byte(MEASURE_HUMI);
break;
default:
break;
}
return error;
}
// sht1x_read_result
U8 sht1x_read_result(U8 *p_value, U8 *p_checksum)
{
U8 error = 0;
// read result
if( (IO0PIN&SENSIBUS_DATA_IN_PIN) == 0x00 ) // if data==1
error += 1;
*(p_value+1) = sht1x_read_byte(ACK);
*(p_value) = sht1x_read_byte(ACK);
*p_checksum = sht1x_read_byte(noACK);
return error;
}
Allen
2010.09.05 于深圳上沙创新科技园
对SHT1x的驱动实现(一)
对SHT1x的驱动实现(二)
文章评论(0条评论)
登录后参与讨论