Прошу помощи.
Есть контроллер cc430x6137 и есть датчик температуры и влажности с двухпроводным интерфейсом SHT71. Никак не получается получить от него чтонибудь правдопадобное. На выходе всегда ошибка (Т=12.3 RH=45.6)
Контроллер стоит на китовой плате EM430F6137RF900, питание 3В батареи, датчик подтянут через 10кОм по обеим ногам DATA - P2.2 SCK - P2.1, питание тоже.
Взял пример с оф. сайта http://www.sensirion.com/en/01_humidity_se...10_Overview.htm попытался его адаптировать, похоже что датчик савсем не реагирует на инициализацию. Скорее всего проблемма в инициализации портов на прием.
определения:
CODE
#define SCK_0 P2OUT &= ~BIT1 // P2.1 = 0
#define SCK_1 P2OUT |= BIT1 // P2.1 = 1
#define DATA_0 P2OUT &= ~BIT2 // P2.2 = 0
#define DATA_1 P2OUT |= BIT2 // P2.2 = 1
#define DATA_IN P2IN & P2_2
#define P2_2 0x04
#define WRITE_data() P2DIR |=BIT2
#define READ_data() P2DIR &=~BIT2
#define WRITE_SCK() P2DIR |=BIT1
#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
#define SCK_1 P2OUT |= BIT1 // P2.1 = 1
#define DATA_0 P2OUT &= ~BIT2 // P2.2 = 0
#define DATA_1 P2OUT |= BIT2 // P2.2 = 1
#define DATA_IN P2IN & P2_2
#define P2_2 0x04
#define WRITE_data() P2DIR |=BIT2
#define READ_data() P2DIR &=~BIT2
#define WRITE_SCK() P2DIR |=BIT1
#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
в maine в цикле вызывается TRHz(); результат посылается на ПК в терминалку (здесь передача работает правильно проверено)
далее подправленные функции работы с датчиком:
CODE
//********************************************************************************
*******
void TRHz(void)
{
value humi_val,temp_val;
unsigned char error,checksum;
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0)
{
SlaveT=123; //ERROR
SlaveRH=456;//ERROR
s_connectionreset(); //in case of an error: connection reset
}
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
SlaveT = (int)temp_val.f*10;
SlaveRH = (int)humi_val.f*10;
}
}
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
WRITE_data();
WRITE_SCK();
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if(i & value) DATA_1; //masking value with i , write to SENSI-BUS
else DATA_0;
SCK_1; //clk for SENSI-BUS
__delay_cycles(5); // 5 us delay @ ~1MHz
SCK_0;
}
DATA_1; //release DATA-line
READ_data();
SCK_1; //clk #9 for ack
error = DATA_IN; //check ack (DATA will be pulled down by SHT11)
SCK_0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
WRITE_data();
DATA_1; //release DATA-line
READ_data();
WRITE_SCK();
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK_1; //clk for SENSI-BUS
if (DATA_IN) val=(val | i); //read bit
SCK_0;
}
WRITE_data();
if(ack == 1) DATA_0; //in case of "ack==1" pull down DATA-Line
else DATA_1;
SCK_1; //clk #9 for ack
__delay_cycles(5); // 5 us delay @ ~1MHz
SCK_0;
DATA_1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
WRITE_data();
WRITE_SCK();
DATA_1; SCK_0; //Initial state
__no_operation();
SCK_1;
__no_operation();
DATA_0;
__no_operation();
SCK_0;
__no_operation();
SCK_1;
__no_operation();
DATA_1;
__no_operation();
SCK_0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
WRITE_data();
WRITE_SCK();
DATA_1; SCK_0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
__no_operation();
SCK_1;
__no_operation();
SCK_0;
}
s_transstart(); //transmission start
}
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
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;
}
READ_data();
for (i=0;i<65535;i++) if(DATA_IN==0) break; //wait until sensor has finished the measurement
if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached
*(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;
}
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
{ const float C1=-2.0468; // for 12 Bit
const float C2=+0.0367; // for 12 Bit
const float C3=-0.0000015955; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [°C]
t_C=0.01*t - 39.6; //calc. temperature from ticks to [°C]
//t_C=0.01*t - 39.6 + 0.00000002*(t-7000)*(t-7000);
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 [°C]
*p_humidity=rh_true; //return humidity[%RH]
}
*******
void TRHz(void)
{
value humi_val,temp_val;
unsigned char error,checksum;
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0)
{
SlaveT=123; //ERROR
SlaveRH=456;//ERROR
s_connectionreset(); //in case of an error: connection reset
}
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
SlaveT = (int)temp_val.f*10;
SlaveRH = (int)humi_val.f*10;
}
}
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
WRITE_data();
WRITE_SCK();
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if(i & value) DATA_1; //masking value with i , write to SENSI-BUS
else DATA_0;
SCK_1; //clk for SENSI-BUS
__delay_cycles(5); // 5 us delay @ ~1MHz
SCK_0;
}
DATA_1; //release DATA-line
READ_data();
SCK_1; //clk #9 for ack
error = DATA_IN; //check ack (DATA will be pulled down by SHT11)
SCK_0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
WRITE_data();
DATA_1; //release DATA-line
READ_data();
WRITE_SCK();
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK_1; //clk for SENSI-BUS
if (DATA_IN) val=(val | i); //read bit
SCK_0;
}
WRITE_data();
if(ack == 1) DATA_0; //in case of "ack==1" pull down DATA-Line
else DATA_1;
SCK_1; //clk #9 for ack
__delay_cycles(5); // 5 us delay @ ~1MHz
SCK_0;
DATA_1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
WRITE_data();
WRITE_SCK();
DATA_1; SCK_0; //Initial state
__no_operation();
SCK_1;
__no_operation();
DATA_0;
__no_operation();
SCK_0;
__no_operation();
SCK_1;
__no_operation();
DATA_1;
__no_operation();
SCK_0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
WRITE_data();
WRITE_SCK();
DATA_1; SCK_0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
__no_operation();
SCK_1;
__no_operation();
SCK_0;
}
s_transstart(); //transmission start
}
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
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;
}
READ_data();
for (i=0;i<65535;i++) if(DATA_IN==0) break; //wait until sensor has finished the measurement
if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached
*(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;
}
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [°C]
{ const float C1=-2.0468; // for 12 Bit
const float C2=+0.0367; // for 12 Bit
const float C3=-0.0000015955; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [°C]
t_C=0.01*t - 39.6; //calc. temperature from ticks to [°C]
//t_C=0.01*t - 39.6 + 0.00000002*(t-7000)*(t-7000);
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 [°C]
*p_humidity=rh_true; //return humidity[%RH]
}
Буду рад любой помощи, помоему я неправильно принимаю данные от датчика.