标题:
51单片机SHT35驱动程序
[打印本页]
作者:
115704475
时间:
2021-9-9 21:26
标题:
51单片机SHT35驱动程序
//////////////////////////////////////////////////////////////////////////////////////////
#define _SH15_DEFINE_
#include "includes.h"
#define DELAY_CNT (5)
//////////////////////////////////////////////////////////////////////////////////////////
//static void TransStart ( void );
//static uchar WriteByte ( uchar sendData );
//static uchar ReadByte ( uchar ack );
//static uchar SH15_GetChecksum ( uchar * pbuf, uchar cmd, uchar len );
//static uchar SH15_GetMeasureVal ( uchar *pbuf, uchar mode );
static void DelayShort ( unsigned char cnt );
static uchar StartI2C ( void );
static uchar StopI2C ( void );
static void AckI2C ( void );
static void NoAckI2C1 ( void );
static uchar RecieveByte ( uchar ack );
static uchar SendByte ( uchar TransmitByte );
uchar SoftResetSHT35 ( void );
static uchar CaculateCRC8 ( uchar *check_data, uchar num_of_data );
static uchar SetSHT35OneMeasure ( void );
static uchar ReadSHT35OneMeasureValue ( uchar *pbuf );
static uchar SHT35_GetTemeratureAndHumidity ( void );
const uchar CRC_TABLE[256] = { 0, 49, 98, 83,196,245,166,151, //0
185,136,219,234,125, 76, 31, 46, //8
67,114, 33, 16,135,182,229,212, //16
250,203,152,169, 62, 15, 92,109, //24
134,183,228,213, 66,115, 32, 17, //32
63, 14, 93,108,251,202,153,168, //40
197,244,167,150, 1, 48, 99, 82, //48
124, 77, 30, 47,184,137,218,235, //56
61, 12, 95,110,249,200,155,170, //64
132,181,230,215,064,113, 34, 19, //72
126, 79, 28, 45,186,139,216,233, //80
199,246,165,148, 3, 50, 97, 80, //88
187,138,217,232,127, 78, 29, 44, //96
2, 51, 96, 81,198,247,164,149, //104
248,201,154,171, 60, 13, 94,111, //112
65,112, 35, 18,133,180,231,214, //120
122, 75, 24, 41,190,143,220,237, //128
195,242,161,144, 7, 54,101, 84, //136
57, 8, 91,106,253,204,159,174, //144
128,177,226,211, 68,117, 38, 23, //152
252,205,158,175, 56, 9, 90,107, //160
69,116, 39, 22,129,176,227,210, //168
191,142,221,236,123, 74, 25, 40, //176
6, 55,100, 85,194,243,160,145, //184
71,118, 37, 20,131,178,225,208, //192
254,207,156,173, 58, 11, 88,105, //200
4, 53,102, 87,192,241,162,147, //208
189,140,223,238,121, 72, 27, 42, //216
193,240,163,146, 5, 52,103, 86, //224
120, 73, 26, 43,188,141,222,239, //232
130,179,224,209, 70,119, 36, 21, //240
59, 10, 89,104,255,206,157,172 //248
};
static uint16 xdata s_T[5];
static uint16 xdata s_H[5];
static uchar s_sht35ReadErrCnt;
/*
**********************************************************************************************
** Function name : DelayShort
**
** Descriptions : Ñóê±oˉêy >1us
**
** Parameters : êäèë2Îêy£o
** 1)cnt: Ñóê±¼ÆêyÆ÷
**
** Returned value : none
**
***********************************************************************************************
*/
static void DelayShort ( unsigned char cnt )
{
while ( cnt-- );
}
/*
*****************************************************************************************
* fuction name: AckI2C
*
* Description : this function generates a acknowdge signal in I2C bus by CPU.
*
* Arguments : none
*
* returns : none
*
* Notes :
******************************************************************************************
*/
static void AckI2C ( void )
{
// SH15_SDA_OUT;
// SH15_SCL_OUT;
SH15_SCL_L;
DelayShort ( 5 );
SH15_SDA_L;
DelayShort ( 5 );
SH15_SCL_H;
DelayShort ( 5 );
SH15_SCL_L;
DelayShort ( 1 );
return ;
}
/*
*****************************************************************************************
* fuction name: NoAckI2C
*
* Description : this function generates a no acknowdge signal in I2C bus by CPU.
*
* Arguments : none
*
* returns : none
*
* Notes :
******************************************************************************************
*/
static void NoAckI2C( void )
{
//SH15_SDA_OUT;
//SH15_SCL_OUT;
SH15_SCL_L;
DelayShort ( 5 );
SH15_SDA_H;
DelayShort ( 5 );
SH15_SCL_H;
DelayShort ( 5 );
SH15_SCL_L;
DelayShort ( 5 );
SH15_SDA_L;
DelayShort ( 1 );
return ;
}
/*
*****************************************************************************************
* fuction name: StartI2C
*
* Description : this function generates a start condition in I2C bus.
*
* Arguments : none
*
* returns : none
*
* Notes :
******************************************************************************************
*/
static uchar StartI2C ( void )
{
//SH15_SDA_OUT;
//SH15_SCL_OUT;
SH15_SDA_H; //send data signal of start condition
DelayShort ( 5 );
SH15_SCL_H; //send clock signal of start condition
DelayShort ( 5 ); //here the time must be more bigger than 4.7us
SH15_SDA_L; //send start condition singal
DelayShort ( 5 ); //>4us
SH15_SCL_L; //make the I2C bus ready to send or recieve data
return TRUE;
}
/*
*****************************************************************************************
* fuction name: StopI2C
*
* Description : this function generates a stop condition in I2C bus.
*
* Arguments : none
*
* returns : none
*
* Notes :
******************************************************************************************
*/
static uchar StopI2C ( void )
{
//SH15_SDA_OUT;
//SH15_SCL_OUT;
SH15_SDA_L; //send data signal of stop condition
SH15_SCL_L;
DelayShort ( 5 );
SH15_SCL_H; //send clock signal of stop condition
DelayShort ( 5 ); //here the time must be more bigger than 4us
SH15_SDA_H; //send start condition singal
DelayShort ( 5 ); //>4us
return TRUE;
}
/*
*****************************************************************************************
* fuction name: RecieveByte
*
* Description : this function receive a byte from I2C bus.
*
* Arguments : none
*
* returns : unsigned char, recieved byte from I2C bus.
*
* Notes :
******************************************************************************************
*/
static uchar RecieveByte ( uchar ack )
{
uchar i;
uchar RecieveByte = 0;
//SH15_SDA_H;
//SH15_SDA_IN;
for ( i=0; i<8; i++ )
{
RecieveByte <<= 1;
SH15_SCL_L;
DelayShort ( 5 ); //>4us
SH15_SCL_H;
DelayShort ( 5 ); //>4us
if ( SH15_SDA )
RecieveByte += 1;
}
SH15_SCL_L;
if ( ack == 0 )
NoAckI2C ( );
else
AckI2C ( );
return ( RecieveByte );
}
/*
*****************************************************************************************
* fuction name: TransmitByte
*
* Description : this function transmit a byte from I2C bus.
*
* Arguments : unsigned char, transmit byte in I2C bus.
*
* returns : bit, acknowdge flag.
*
* Notes :
******************************************************************************************
*/
static uchar SendByte ( uchar TransmitByte )
{
uchar i;
uchar Ack;
// SH15_SDA_OUT;
Ack = FALSE;
SH15_SCL_L;
for ( i=0; i<8; i++ )
{
SH15_SCL_L;
DelayShort ( 5 ); //>4us
if ( TransmitByte&0x80 )
SH15_SDA_H;
else
SH15_SDA_L;
DelayShort ( 5 ); //>4us
SH15_SCL_H;
DelayShort ( 5 ); //>4us
TransmitByte <<= 1;
}
SH15_SCL_L;
SH15_SDA_H;
// SH15_SDA_IN; //input mode
SH15_SCL_H;
DelayShort ( 5 ); //>4us
i = 30;
while ( SH15_SDA ) {
i--;
DelayShort ( 5 ); //>4us
if ( i == 0 ) {
break;
}
DelayShort ( 5 );
}
if ( i == 0 )
Ack = FALSE;
else
Ack = TRUE;
SH15_SCL_H;
DelayShort ( 5 );
SH15_SCL_L;
// SH15_SDA_OUT;
return ( Ack );
}
/*
*****************************************************************************************
* fuction name: SoftResetSHT35
*
* Description : this function transmit a byte from I2C bus.
*
* Arguments : unsigned char, transmit byte in I2C bus.
*
* returns : bit, acknowdge flag.
*
* Notes :
******************************************************************************************
*/
uchar SoftResetSHT35 ( void )
{
uchar ret;
//ioport_set_pin_dir(SH15SCL_GPIO, IOPORT_DIR_OUTPUT);
//ioport_set_pin_dir(SH15SDA_GPIO, IOPORT_DIR_OUTPUT);
//ioport_set_pin_mode(SH15SDA_GPIO, IOPORT_MODE_PULLUP);
StartI2C ( );
ret = SendByte ( SHT35_ADDR );
if ( ret == FALSE ) return FALSE;
ret = SendByte ( SHT35_SOFT_RST_CMD_MSB );
if ( ret == FALSE ) return FALSE;
ret = SendByte ( SHT35_SOFT_RST_CMD_LSB );
if ( ret == FALSE ) return FALSE;
StopI2C ( );
return TRUE;
}
/*
*****************************************************************************************
* fuction name: CaculateCRC8
*
* Description : this function transmit a byte from I2C bus.
*
* Arguments : unsigned char, transmit byte in I2C bus.
*
* returns : bit, acknowdge flag.
*
* Notes :
******************************************************************************************
*/
uchar CaculateCRC8 ( uchar *check_data, uchar num_of_data )
{
uchar j; // bit mask
uchar crc = 0xFF; // calculated checksum
uchar i; // byte counter
// calculates 8-Bit checksum with given polynomial
for( i = 0; i < num_of_data; i++) {
crc ^= check_data[i];
//crc?????????????0x31
for( j = 8; j > 0; --j ) {
if(crc & 0x80) {
crc = (crc << 1) ^ 0x31;
} else {
crc = (crc << 1);
}
}
}
return crc;
}
/*
*****************************************************************************************
* fuction name: SHT35_GetTemeratureAndHumidity
*
* Description : this function transmit a byte from I2C bus.
*
* Arguments : unsigned char, transmit byte in I2C bus.
*
* returns : bit, acknowdge flag.
*
* Notes :
******************************************************************************************
*/
uchar SHT35_GetTemeratureAndHumidity (void )
{
uchar ret;
uchar buf[6];
uint16 tempInt;
uint32 tempL;
float tempF;
ret = SoftResetSHT35();
if ( ret == FALSE ) return FALSE;
ret = ReadSHT35OneMeasureValue( buf );
if ( ret == FALSE ) return FALSE;
// s_sht35ReadErrCnt = 0;
tempInt = (((uint16)buf[0]) << 8) | buf[1];
tempF = ((175.0 * ((float)tempInt) / 65535.0 - 45.0));
g_FT = tempF;
s_T[0] = s_T[1];
s_T[1] = s_T[2];
if ( tempF > 0.0 )
{
s_T[2] = (uint16)((uint32)(tempF*10))&0xffff; //1?????
if ( g_TFlag == 1 )
{
g_TFlag = 0;
s_T[0] = 0;
}
}
else
{
s_T[2] = (uint16)((uint32)(tempF*(-1)*10))&0xffff; //1?????
if ( g_TFlag == 0 )
{
g_TFlag = 1;
s_T[0] = 0;
}
}
tempL = s_T[0]+s_T[1]+s_T[2];
//g_measureDlyDisp |= 0x01;
g_temperatureSH15 =s_T[2];
//s_T[2];
//;
// 11
/*
if ( g_TFlag == 1 ) // 11
{
g_status &= 0xfb;
}
else //+
{
if ( g_temperatureSH15 > 600 )
g_status |= 0x04;
else
g_status &= 0xfb;
}
*/
//humidity
//g_measureDlyDisp |= 0x02;
tempInt = (((uint16)buf[3]) << 8) | buf[4];
tempF = ((100.0 * ((float)tempInt) / 65535.0) );
if ( (tempF > 99.0) || (tempF < 0.1) ) return FALSE;
s_H[0] = s_H[1];
s_H[1] = s_H[2];
s_H[2] = (uint16)((uint32)(tempF*10))&0xffff; //
g_humidity =s_H[2];
return TRUE;
}
/*
*****************************************************************************************
* fuction name: ReadSHT35OneMeasureValue
*
* Description : this function transmit a byte from I2C bus.
*
* Arguments : unsigned char, transmit byte in I2C bus.
*
* returns : bit, acknowdge flag.
*
* Notes :
******************************************************************************************
*/
static uchar ReadSHT35OneMeasureValue ( uchar *pbuf )
{
uchar ret;
uchar crc8;
uchar s_sht35OneShotFlag;
uchar stretchClockDly;
s_sht35OneShotFlag=SetSHT35OneMeasure();
if (s_sht35OneShotFlag==FALSE)
{
g_temperatureSH15=0001;
return FALSE;
}
DelayShort(30000);
StartI2C ( );
ret = SendByte ( SHT35_ADDR+1 ); //read cmd with clock stretching
if ( ret == FALSE ) return FALSE;
/*
stretchClockDly = 100;
//SH15_SCL_IN;
while ( stretchClockDly > 0 ) {
stretchClockDly--;
//if ( SH15_SCL== 1 )
// break;
}
//SH15_SCL_OUT;
// if ( stretchClockDly == 0 )
// return FALSE;
*/
pbuf[0] = RecieveByte ( 1 ); //ACK
pbuf[1] = RecieveByte ( 1 ); //ACK
pbuf[2] = RecieveByte ( 1 ); //ACK
pbuf[3] = RecieveByte ( 1 ); //ACK
pbuf[4] = RecieveByte ( 1 ); //ACK
pbuf[5] = RecieveByte ( 0); //ACK
StopI2C ( );
//crc checksum
crc8 = CaculateCRC8 ( pbuf, 2 );
if ( crc8 != pbuf[2] )
{
g_temperatureSH15 =0002;
return FALSE;
}
crc8 = CaculateCRC8 ( pbuf+3, 2 );
if ( crc8 != pbuf[5])
{
//g_temperatureSH15 =0004;
return FALSE;
}
return TRUE;
}
/*
*****************************************************************************************
* fuction name: SetSHT35OneMeasure
*
* Description : this function transmit a byte from I2C bus.
*
* Arguments : unsigned char, transmit byte in I2C bus.
*
* returns : bit, acknowdge flag.
*
* Notes :
******************************************************************************************
*/
static uchar SetSHT35OneMeasure ( void )
{
uchar ret;
StartI2C ( );
ret = SendByte ( SHT35_ADDR );
if ( ret == FALSE )
{
return FALSE;
}
ret = SendByte ( SHT35_ONE_SHOT_CMD_MSB );
if ( ret == FALSE )
{
return FALSE;
}
ret = SendByte ( SHT35_ONE_SHOT_CMD_LSB );
if ( ret == FALSE )
{
return FALSE;
}
StopI2C ( );
return TRUE;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1