/Designs/Measuring_instruments/AWS01A/SW/SHT.c
15,30 → 15,28
// //
///////////////////////////////////////////////////////////////////////////////
 
#define sht_data_pin PIN_D3 //(I use A1 as data input)
#define sht_clk_pin PIN_C4
#define sht_data_pin PIN_D0
#define sht_clk_pin PIN_D1
 
 
 
 
//***** Function to alert SHT75 *****
 
void comstart (void)
{
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
output_low(sht_clk_pin); //clk low
delay_us(2);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(2);
output_low(sht_data_pin); //data low
delay_us(2);
output_low(sht_clk_pin); //clk low
delay_us(5);
output_high(sht_clk_pin); //clk high
delay_us(2);
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
delay_us(2);
output_low(sht_clk_pin); //clk low
}
 
 
50,22 → 48,25
int1 ack;
 
//Shift out command
delay_us(4);
for(i=0; i<8> 0) output_float(sht_data_pin); //data high if MSB high
else output_bit(sht_data_pin, 0); //data low if MSB low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
delay_us(5);
for(i=0; i<8; i++)
{
output_low(sht_clk_pin); //clk low
if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high
else output_low(sht_data_pin); //data low if MSB low
delay_us(2);
output_high(sht_clk_pin); //clk high
delay_us(2);
mask = mask >> 1; //shift to next bit
}
 
//Shift in ack
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_low(sht_clk_pin); //clk low
delay_us(2);
ack = input(sht_data_pin); //get ack bit
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_bit(sht_clk_pin, 0); //clk low
output_high(sht_clk_pin); //clk high
delay_us(2);
output_low(sht_clk_pin); //clk low
return(ack);
}
 
83,21 → 84,21
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(2);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_low(sht_clk_pin); //clk low
delay_us(2);
}
 
//send ack 0 bit
output_bit(sht_data_pin, 0); //data low
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
output_low(sht_data_pin); //data low
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(5);
output_low(sht_clk_pin); //clk low
delay_us(2);
output_float(sht_data_pin); //data high
 
//shift in LSB data
104,20 → 105,20
for(i=0; i<8; i++)
{
iobyte = iobyte << 1;
output_bit(sht_clk_pin, 1); //clk high
delay_us(1);
output_high(sht_clk_pin); //clk high
delay_us(2);
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
else iobyte |= mask0;
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_low(sht_clk_pin); //clk low
delay_us(2);
}
 
//send ack 1 bit
output_float(sht_data_pin); //data high
delay_us(1);
output_bit(sht_clk_pin, 1); //clk high
delay_us(2);
output_bit(sht_clk_pin, 0); //clk low
output_high(sht_clk_pin); //clk high
delay_us(5);
output_low(sht_clk_pin); //clk low
 
return(iobyte);
}
130,8 → 131,8
int16 sht_delay;
 
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(1);
output_low(sht_clk_pin); //clk low
delay_us(2);
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms
{
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready
147,14 → 148,14
int8 i;
 
output_float(sht_data_pin); //data high
output_bit(sht_clk_pin, 0); //clk low
delay_us(2);
output_low(sht_clk_pin); //clk low
delay_us(5);
for(i=0; i<9; i++)
{
output_bit(sht_clk_pin, 1); //toggle clk 9 times
delay_us(2);
output_bit(sht_clk_pin, 0);
delay_us(2);
output_high(sht_clk_pin); //toggle clk 9 times
delay_us(5);
output_low(sht_clk_pin);
delay_us(5);
}
comstart();
}
242,4 → 243,3
comreset(); //reset SHT75
delay_ms(20); //delay for power-up
}