|
#include <reg52.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define MEASURE_TEMP 0x80
sbit SCLK = P1^3;
sbit DATA = P1^5;
typedef union
{
uint i;
float f;
}value;
float temp;
#define noACK 0
#define ACK 1
char s_write_byte(uchar value)
//************************************************************//
// writes a byte on the Sensibus and checks the acknowledge
{
uchar i,error = 0;
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;
SCLK = 1; //clk for SENSI-BUS
_nop_();
_nop_();
_nop_(); //pulswith approx. 5 us
SCLK = 0;
}
DATA = 1; //release DATA-line
SCLK = 1; //clk #9 for ack
error = DATA; //check ack (DATA will be pulled down by SHT11)
SCLK = 0;
return error; //error=1 in case of no acknowledge
}
//************************************************************//
char s_read_byte(uchar ack)
//************************************************************//
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
uchar i,val = 0;
DATA = 1; //release DATA-line
for(i = 0x80; i > 0; i /= 2) //shift bit for masking
{
SCLK=1; //clk for SENSI-BUS
if(DATA) val = (val | i); //read bit
SCLK=0;
}
DATA =! ack; //in case of "ack==1" pull down DATA-Line
SCLK = 1; //clk #9 for ack
_nop_();
_nop_();
_nop_(); //pulswith approx. 5 us
SCLK = 0;
DATA = 1; //release DATA-line
return val;
}
//************************************************************//
void s_transstart(void)
//************************************************************//
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCLK : ___| |___| |______
{
DATA = 1;
SCLK = 0; //Initial state
_nop_();
SCLK = 1;
_nop_();
DATA = 0;
_nop_();
SCLK =0;
_nop_();
_nop_();
_nop_();
SCLK = 1;
_nop_();
DATA = 1;
_nop_();
SCLK = 0;
}
char s_measure(uchar *p_value, uchar *p_checksum)
//************************************************************//
// makes a measurement (humidity/temperature) with checksum
{
uchar error = 0;
s_transstart();
error += s_write_byte(MEASURE_TEMP);
if(DATA) error += 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;
}
void main()
{
value val;
uchar error,checksum;
error = 0;
error += s_measure((uchar*)&val.i,&checksum);
}
*******************************************************************
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define MEASURE_TEMP 0x81
sbit SCLK = P1^3;
sbit DATA = P1^5;
typedef union
{
uint i;
float f;
}value;
float temp;
#define noACK 0
#define ACK 1
char s_write_byte(uchar value)
//************************************************************//
// writes a byte on the Sensibus and checks the acknowledge
{
uchar i,error = 0;
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;
SCLK = 1; //clk for SENSI-BUS
_nop_();
_nop_();
_nop_(); //pulswith approx. 5 us
SCLK = 0;
}
DATA = 1; //release DATA-line
SCLK = 1; //clk #9 for ack
error = DATA; //check ack (DATA will be pulled down by SHT11)
SCLK = 0;
return error; //error=1 in case of no acknowledge
}
//************************************************************//
char s_read_byte(uchar ack)
//************************************************************//
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
uchar i,val = 0;
DATA = 1; //release DATA-line
for(i=0;i<8;i++) //shift bit for masking
{
SCLK=1; //clk for SENSI-BUS
if(DATA) val = (val | i); //read bit
SCLK=0;
}
DATA =! ack; //in case of "ack==1" pull down DATA-Line
SCLK = 1; //clk #9 for ack
_nop_();
_nop_();
_nop_(); //pulswith approx. 5 us
SCLK = 0;
DATA = 1; //release DATA-line
return val;
}
//************************************************************//
void s_transstart(void)
//************************************************************//
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCLK : ___| |___| |______
{
DATA = 1;
SCLK = 0; //Initial state
_nop_();
SCLK = 1;
_nop_();
DATA = 0;
_nop_();
SCLK =0;
_nop_();
_nop_();
_nop_();
SCLK = 1;
_nop_();
DATA = 1;
_nop_();
SCLK = 0;
}
char s_measure(uchar *p_value,uchar *p_checksum)
//************************************************************//
// makes a measurement (humidity/temperature) with checksum
{
uchar error = 0;
s_transstart();
error += s_write_byte(MEASURE_TEMP);
if(DATA) error += 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;
}
void main()
{
value val;
uchar error,checksum;
error = 0;
error += s_measure((uchar*)&val.i,&checksum);
}
在char s_read_byte函数中发现,读取的数据是11111111。本人菜鸟新手,求高手指教 |
|