?? avr程序.c
字號:
DATA_OUT=0;
DDRC = 0b00000010; // DATA Eingang
SCK=1; //clk #9 for ack
delay_us(2);
error=DATA_IN; //check ack (DATA will be pulled down by SHT11)
delay_us(2);
SCK=0;
return error; //error=1 in case of no acknowledge
}
/*
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
DDRC = 0b00000011; // DATA Ausgang
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) DATA_OUT=1; //masking value with i , write to SENSI-BUS
else DATA_OUT=0;
SCK=1; //clk for SENSI-BUS
delay_us(5); //pulswith approx. 5 us
SCK=0;
}
DDRC = 0b00000010; // DATA Eingang
delay_us(2); //pulswith approx. 5 us
SCK=1; //clk #9 for ack
delay_us(5); //pulswith approx. 5 us
error=DATA_IN; //check ack (DATA will be pulled down by SHT11)
SCK=0;
return error; //error=1 in case of no acknowledge
}
*/
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DDRC = 0b00000010; // DATA Eingang
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK=1; //clk for SENSI-BUS
delay_us(2);
if (DATA_IN) val=(val | i); //read bit
SCK=0;
delay_us(2);
}
DDRC = 0b00000011; // DATA Ausgang
DATA_OUT=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
delay_us(5); //pulswith approx. 5 us
SCK=0;
DDRC = 0b00000010; // DATA Eingang
return val;
}
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
//----------------------------------------------------------------------------------
void s_transstart(void)
{
DDRC = 0b00000011; // DATA Ausgang
DATA_OUT=1; SCK=0; //Initial state
delay_us(2);
SCK=1;
delay_us(2);
DATA_OUT=0;
delay_us(2);
SCK=0;
delay_us(5);
SCK=1;
delay_us(2);
DATA_OUT=1;
delay_us(2);
SCK=0;
DDRC = 0b00000010; // DATA Eingang
}
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
//----------------------------------------------------------------------------------
void s_connectionreset(void)
{
unsigned char i;
DDRC = 0b00000011; // DATA Ausgang
DATA_OUT=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ SCK=1;
delay_us(1);
SCK=0;
delay_us(1);
}
s_transstart(); //transmission start
DDRC = 0b00000010; // DATA Eingang
}
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
//----------------------------------------------------------------------------------
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
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;
}
DDRC = 0b00000010; // DATA Eingang
while (1)
{
if(DATA_IN==0) break; //wait until sensor has finished the measurement
}
if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached
if (error!=0)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("FEHLER");
while(1);
}
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [癈]
//----------------------------------------------------------------------------------------
const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
void calc_sth11(float *p_humidity ,float *p_temperature)
{
float rh;
float t;
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [癈]
rh =*p_humidity; // rh: Humidity [Ticks] 12 Bit
t =*p_temperature; // t: Temperature [Ticks] 14 Bit
t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [癈]
*p_humidity=rh_true; //return humidity[%RH]
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -