找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1724|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机SHT35驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:964853 发表于 2021-9-9 21:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. //////////////////////////////////////////////////////////////////////////////////////////
  2. #define _SH15_DEFINE_
  3. #include "includes.h"
  4. #define DELAY_CNT                (5)
  5. //////////////////////////////////////////////////////////////////////////////////////////


  6. //static void TransStart ( void );
  7. //static uchar WriteByte ( uchar sendData );
  8. //static uchar ReadByte ( uchar ack );
  9. //static uchar SH15_GetChecksum ( uchar * pbuf, uchar cmd, uchar len );
  10. //static uchar SH15_GetMeasureVal ( uchar *pbuf, uchar mode );
  11. static void DelayShort ( unsigned char cnt );
  12. static uchar StartI2C ( void );
  13. static uchar StopI2C ( void );
  14. static void AckI2C ( void );
  15. static void NoAckI2C1 ( void );
  16. static uchar RecieveByte ( uchar ack );
  17. static uchar SendByte  ( uchar TransmitByte );
  18. uchar SoftResetSHT35  ( void );
  19. static uchar CaculateCRC8 ( uchar *check_data, uchar num_of_data );
  20. static uchar SetSHT35OneMeasure  ( void );
  21. static uchar ReadSHT35OneMeasureValue  ( uchar *pbuf );
  22. static uchar SHT35_GetTemeratureAndHumidity  ( void );

  23. const uchar CRC_TABLE[256] = {        0,  49, 98,  83,196,245,166,151,                //0
  24.                                                                 185,136,219,234,125, 76, 31, 46,                //8
  25.                                                                  67,114, 33, 16,135,182,229,212,                //16
  26.                                                                 250,203,152,169, 62, 15, 92,109,                //24
  27.                                                                 134,183,228,213, 66,115, 32, 17,                //32
  28.                                                                  63, 14, 93,108,251,202,153,168,                //40
  29.                                                                 197,244,167,150,  1, 48, 99, 82,                //48
  30.                                                                 124, 77, 30, 47,184,137,218,235,                //56
  31.                                                                  61, 12, 95,110,249,200,155,170,                //64
  32.                                                                 132,181,230,215,064,113, 34, 19,                //72
  33.                                                                 126, 79, 28, 45,186,139,216,233,                //80
  34.                                                                 199,246,165,148,  3, 50, 97, 80,                //88
  35.                                                                 187,138,217,232,127, 78, 29, 44,                //96
  36.                                                                   2, 51, 96, 81,198,247,164,149,                //104
  37.                                                                 248,201,154,171, 60, 13, 94,111,                //112
  38.                                                                  65,112, 35, 18,133,180,231,214,                //120
  39.                                                                 122, 75, 24, 41,190,143,220,237,                //128
  40.                                                                 195,242,161,144,  7, 54,101, 84,                //136
  41.                                                                
  42.                                                                  57,  8, 91,106,253,204,159,174,                //144
  43.                                                                 128,177,226,211, 68,117, 38, 23,                //152
  44.                                                                 252,205,158,175, 56,  9, 90,107,                //160
  45.                                                                  69,116, 39, 22,129,176,227,210,                //168
  46.                                                                 191,142,221,236,123, 74, 25, 40,                //176
  47.                                                                   6, 55,100, 85,194,243,160,145,                //184
  48.                                                                  71,118, 37, 20,131,178,225,208,                //192
  49.                                                                 254,207,156,173, 58, 11, 88,105,                //200
  50.                                                                   4, 53,102, 87,192,241,162,147,                //208
  51.                                                                 189,140,223,238,121, 72, 27, 42,                //216
  52.                                                                 193,240,163,146,  5, 52,103, 86,                //224
  53.                                                                 120, 73, 26, 43,188,141,222,239,                //232
  54.                                                                 130,179,224,209, 70,119, 36, 21,                //240
  55.                                                                  59, 10, 89,104,255,206,157,172                        //248
  56.                                                                 };

  57. static uint16 xdata s_T[5];
  58. static uint16 xdata s_H[5];
  59. static uchar s_sht35ReadErrCnt;      

  60. /*
  61. **********************************************************************************************
  62. ** Function name        :        DelayShort
  63. **
  64. ** Descriptions                :        Ñóê±oˉêy >1us
  65. **
  66. ** Parameters                :        êäèë2Îêy£o
  67. **                                                1)cnt: Ñóê±¼ÆêyÆ÷                              
  68. **
  69. ** Returned value        :        none
  70. **
  71. ***********************************************************************************************
  72. */
  73. static void DelayShort ( unsigned char cnt )
  74. {
  75.         while ( cnt-- );
  76. }



  77. /*
  78. *****************************************************************************************
  79. * fuction name: AckI2C
  80. *
  81. * Description : this function generates a acknowdge signal in I2C bus by CPU.
  82. *
  83. * Arguments   : none
  84. *
  85. * returns     : none
  86. *
  87. * Notes     :
  88. ******************************************************************************************
  89. */
  90. static void AckI2C ( void )
  91. {
  92. //        SH15_SDA_OUT;
  93. //        SH15_SCL_OUT;  
  94.       
  95.         SH15_SCL_L;
  96.         DelayShort ( 5 );
  97.         SH15_SDA_L;
  98.         DelayShort ( 5 );
  99.         SH15_SCL_H;
  100.         DelayShort ( 5 );
  101.       
  102.         SH15_SCL_L;
  103.         DelayShort ( 1 );
  104.       
  105.         return ;
  106. }

  107. /*
  108. *****************************************************************************************
  109. * fuction name: NoAckI2C
  110. *
  111. * Description : this function generates a no acknowdge signal in I2C bus by CPU.
  112. *
  113. * Arguments   : none
  114. *
  115. * returns     : none
  116. *
  117. * Notes     :
  118. ******************************************************************************************
  119. */
  120. static void NoAckI2C( void )
  121. {
  122.         //SH15_SDA_OUT;
  123.         //SH15_SCL_OUT;
  124.       
  125.         SH15_SCL_L;
  126.         DelayShort ( 5 );
  127.         SH15_SDA_H;
  128.         DelayShort ( 5 );
  129.         SH15_SCL_H;
  130.         DelayShort ( 5 );
  131.         SH15_SCL_L;
  132.         DelayShort ( 5 );
  133.         SH15_SDA_L;
  134.         DelayShort ( 1 );
  135.       
  136.         return ;
  137. }

  138. /*
  139. *****************************************************************************************
  140. * fuction name: StartI2C
  141. *
  142. * Description : this function generates a start condition in I2C bus.
  143. *
  144. * Arguments   : none
  145. *
  146. * returns     : none
  147. *
  148. * Notes     :
  149. ******************************************************************************************
  150. */
  151. static uchar StartI2C ( void )
  152. {
  153.         //SH15_SDA_OUT;
  154.         //SH15_SCL_OUT;
  155.       
  156.         SH15_SDA_H;                //send data signal of start condition
  157.         DelayShort ( 5 );
  158.         SH15_SCL_H;                //send clock signal of start condition
  159.         DelayShort ( 5 );                //here the time must be more bigger than 4.7us
  160.         SH15_SDA_L;                //send start condition singal
  161.         DelayShort ( 5 );                //>4us
  162.         SH15_SCL_L;                //make the I2C bus ready to send or recieve data
  163.       
  164.         return TRUE;
  165. }

  166. /*
  167. *****************************************************************************************
  168. * fuction name: StopI2C
  169. *
  170. * Description : this function generates a stop condition in I2C bus.
  171. *
  172. * Arguments   : none
  173. *
  174. * returns     : none
  175. *
  176. * Notes     :
  177. ******************************************************************************************
  178. */
  179. static uchar StopI2C ( void )
  180. {
  181.         //SH15_SDA_OUT;
  182.         //SH15_SCL_OUT;
  183.       
  184.         SH15_SDA_L;                //send data signal of stop condition
  185.         SH15_SCL_L;
  186.         DelayShort ( 5 );
  187.         SH15_SCL_H;                //send clock signal of stop condition
  188.         DelayShort ( 5 );                //here the time must be more bigger than 4us
  189.         SH15_SDA_H;                //send start condition singal
  190.         DelayShort ( 5 );                //>4us
  191.       
  192.         return TRUE;
  193. }


  194. /*
  195. *****************************************************************************************
  196. * fuction name: RecieveByte
  197. *
  198. * Description : this function receive a byte from I2C bus.
  199. *
  200. * Arguments   : none
  201. *
  202. * returns     : unsigned char, recieved byte from I2C bus.
  203. *
  204. * Notes     :
  205. ******************************************************************************************
  206. */
  207. static uchar RecieveByte ( uchar ack )
  208. {
  209.         uchar i;
  210.         uchar RecieveByte = 0;
  211.       
  212.         //SH15_SDA_H;
  213.         //SH15_SDA_IN;
  214.       
  215.         for ( i=0; i<8; i++ )
  216.         {
  217.                 RecieveByte <<= 1;
  218.                 SH15_SCL_L;
  219.                 DelayShort ( 5 );                //>4us
  220.                 SH15_SCL_H;
  221.                
  222.                 DelayShort ( 5 );                //>4us
  223.                 if ( SH15_SDA )
  224.                         RecieveByte += 1;
  225.         }
  226.       
  227.         SH15_SCL_L;
  228.       
  229.         if ( ack == 0 )
  230.                 NoAckI2C ( );
  231.         else
  232.                 AckI2C ( );      
  233.       
  234.         return (  RecieveByte );
  235. }
  236.                
  237. /*
  238. *****************************************************************************************
  239. * fuction name: TransmitByte
  240. *
  241. * Description : this function transmit a byte from I2C bus.
  242. *
  243. * Arguments   : unsigned char, transmit byte in I2C bus.
  244. *
  245. * returns     : bit, acknowdge flag.
  246. *
  247. * Notes     :
  248. ******************************************************************************************
  249. */
  250. static uchar SendByte  ( uchar TransmitByte )
  251. {
  252.         uchar i;
  253.         uchar Ack;

  254. //        SH15_SDA_OUT;
  255.         Ack = FALSE;
  256.         SH15_SCL_L;
  257.                        
  258.         for ( i=0; i<8; i++ )
  259.         {
  260.                 SH15_SCL_L;
  261.                 DelayShort ( 5 );                //>4us
  262.                 if (  TransmitByte&0x80  )
  263.                         SH15_SDA_H;
  264.                 else
  265.                         SH15_SDA_L;
  266.                 DelayShort ( 5 );                //>4us
  267.                 SH15_SCL_H;
  268.                 DelayShort ( 5 );                //>4us
  269.                 TransmitByte <<= 1;
  270.         }
  271.                        
  272.         SH15_SCL_L;
  273.         SH15_SDA_H;
  274. //        SH15_SDA_IN;                //input mode
  275.                        
  276.         SH15_SCL_H;
  277.         DelayShort ( 5 );                //>4us
  278.       
  279.         i = 30;
  280.         while ( SH15_SDA ) {
  281.                 i--;
  282.                 DelayShort ( 5 );                //>4us
  283.                
  284.                 if ( i == 0 ) {
  285.                         break;               
  286.                 }
  287.                 DelayShort ( 5 );               
  288.         }
  289.         if ( i == 0 )
  290.                 Ack = FALSE;
  291.         else
  292.                 Ack = TRUE;
  293.       
  294.         SH15_SCL_H;      
  295.         DelayShort ( 5 );      
  296.         SH15_SCL_L;               
  297. //        SH15_SDA_OUT;
  298.                        
  299.         return ( Ack );
  300. }
  301. /*
  302. *****************************************************************************************
  303. * fuction name: SoftResetSHT35
  304. *
  305. * Description : this function transmit a byte from I2C bus.
  306. *
  307. * Arguments   : unsigned char, transmit byte in I2C bus.
  308. *
  309. * returns     : bit, acknowdge flag.
  310. *
  311. * Notes     :
  312. ******************************************************************************************
  313. */
  314. uchar SoftResetSHT35  ( void )
  315. {
  316.     uchar ret;

  317.     //ioport_set_pin_dir(SH15SCL_GPIO, IOPORT_DIR_OUTPUT);

  318.           //ioport_set_pin_dir(SH15SDA_GPIO, IOPORT_DIR_OUTPUT);
  319.           //ioport_set_pin_mode(SH15SDA_GPIO, IOPORT_MODE_PULLUP);  
  320.       
  321.           StartI2C ( );
  322.       
  323.     ret = SendByte  ( SHT35_ADDR );
  324.     if ( ret == FALSE ) return FALSE;
  325.       
  326.     ret = SendByte  ( SHT35_SOFT_RST_CMD_MSB );
  327.     if ( ret == FALSE ) return FALSE;
  328.       
  329.     ret = SendByte  ( SHT35_SOFT_RST_CMD_LSB );
  330.     if ( ret == FALSE ) return FALSE;
  331.       
  332.     StopI2C ( );   

  333.     return TRUE;
  334. }

  335. /*
  336. *****************************************************************************************
  337. * fuction name: CaculateCRC8
  338. *
  339. * Description : this function transmit a byte from I2C bus.
  340. *
  341. * Arguments   : unsigned char, transmit byte in I2C bus.
  342. *
  343. * returns     : bit, acknowdge flag.
  344. *
  345. * Notes     :
  346. ******************************************************************************************
  347. */
  348. uchar  CaculateCRC8 ( uchar *check_data, uchar num_of_data )
  349. {
  350.           uchar j;        // bit mask
  351.     uchar crc = 0xFF; // calculated checksum
  352.     uchar i;    // byte counter

  353.     // calculates 8-Bit checksum with given polynomial
  354.     for( i = 0; i < num_of_data; i++) {
  355.         crc ^= check_data[i];
  356.         //crc?????????????0x31
  357.         for( j = 8; j > 0; --j ) {
  358.             if(crc & 0x80) {
  359.                 crc = (crc << 1) ^ 0x31;
  360.             }  else {
  361.                 crc = (crc << 1);
  362.             }
  363.         }
  364.     }

  365.     return crc;
  366. }

  367. /*
  368. *****************************************************************************************
  369. * fuction name: SHT35_GetTemeratureAndHumidity
  370. *
  371. * Description : this function transmit a byte from I2C bus.
  372. *
  373. * Arguments   : unsigned char, transmit byte in I2C bus.
  374. *
  375. * returns     : bit, acknowdge flag.
  376. *
  377. * Notes     :
  378. ******************************************************************************************
  379. */

  380. uchar SHT35_GetTemeratureAndHumidity  (void )
  381. {
  382.     uchar ret;
  383.     uchar buf[6];
  384.     uint16 tempInt;
  385.           uint32 tempL;
  386.     float tempF;
  387.       
  388.                 ret = SoftResetSHT35();
  389.           if ( ret == FALSE ) return FALSE;
  390.                                        
  391.           ret = ReadSHT35OneMeasureValue( buf );
  392.           if ( ret == FALSE ) return FALSE;

  393.       
  394.         //        s_sht35ReadErrCnt = 0;
  395.       
  396.     tempInt = (((uint16)buf[0]) << 8) | buf[1];

  397.     tempF = ((175.0 * ((float)tempInt) / 65535.0 - 45.0));      

  398.     g_FT = tempF;
  399.                
  400.     s_T[0] = s_T[1];
  401.     s_T[1] = s_T[2];
  402.                
  403.     if ( tempF > 0.0 )      
  404.     {                       
  405.         s_T[2] = (uint16)((uint32)(tempF*10))&0xffff;        //1?????
  406.         if ( g_TFlag == 1 )
  407.         {
  408.             g_TFlag = 0;
  409.             s_T[0] = 0;
  410.         }
  411.     }
  412.     else
  413.     {
  414.         s_T[2] = (uint16)((uint32)(tempF*(-1)*10))&0xffff;        //1?????
  415.         if ( g_TFlag == 0 )
  416.         {
  417.             g_TFlag = 1;
  418.             s_T[0] = 0;
  419.         }
  420.     }               

  421.     tempL = s_T[0]+s_T[1]+s_T[2];

  422.          //g_measureDlyDisp |= 0x01;

  423.    g_temperatureSH15 =s_T[2];
  424.                 //s_T[2];
  425.                 //;      

  426.     // 11
  427.                 /*
  428.     if ( g_TFlag == 1 ) // 11
  429.     {            
  430.             g_status &= 0xfb;               
  431.     }
  432.     else        //+
  433.     {
  434.         if ( g_temperatureSH15 > 600 )
  435.             g_status |= 0x04;      
  436.         else
  437.             g_status &= 0xfb;               
  438.     }
  439.                 */

  440.     //humidity
  441.     //g_measureDlyDisp |= 0x02;

  442.     tempInt = (((uint16)buf[3]) << 8) | buf[4];
  443.     tempF = ((100.0 * ((float)tempInt) / 65535.0) );      
  444.                               
  445.          
  446.                 if ( (tempF > 99.0) || (tempF < 0.1) ) return FALSE;
  447.                
  448.         s_H[0] = s_H[1];
  449.         s_H[1] = s_H[2];
  450.                        
  451.         s_H[2] = (uint16)((uint32)(tempF*10))&0xffff;                //                              
  452.         g_humidity =s_H[2];
  453.       
  454.       
  455.                                        
  456.   return TRUE;
  457. }


  458. /*
  459. *****************************************************************************************
  460. * fuction name: ReadSHT35OneMeasureValue
  461. *
  462. * Description : this function transmit a byte from I2C bus.
  463. *
  464. * Arguments   : unsigned char, transmit byte in I2C bus.
  465. *
  466. * returns     : bit, acknowdge flag.
  467. *
  468. * Notes     :
  469. ******************************************************************************************
  470. */
  471. static uchar ReadSHT35OneMeasureValue  ( uchar *pbuf )
  472. {
  473.     uchar ret;
  474.     uchar crc8;
  475.           uchar s_sht35OneShotFlag;
  476.     uchar stretchClockDly;
  477.                  
  478.    s_sht35OneShotFlag=SetSHT35OneMeasure();
  479.       
  480.          if (s_sht35OneShotFlag==FALSE)
  481.            {      
  482.         g_temperatureSH15=0001;                        
  483.         return FALSE;
  484.            }      
  485.                  
  486.                  DelayShort(30000);
  487.                  
  488.                  
  489.     StartI2C ( );

  490.     ret = SendByte  ( SHT35_ADDR+1 );       //read cmd with clock stretching
  491.     if ( ret == FALSE ) return FALSE;
  492.       

  493. /*  
  494.     stretchClockDly = 100;
  495.     //SH15_SCL_IN;
  496.     while ( stretchClockDly > 0 )   {
  497.         stretchClockDly--;
  498.         //if ( SH15_SCL== 1 )     
  499.          //   break;
  500.     }
  501.     //SH15_SCL_OUT;
  502.    // if ( stretchClockDly == 0 )
  503.       //  return FALSE;                    
  504. */

  505.                
  506.     pbuf[0] = RecieveByte ( 1 );     //ACK
  507.     pbuf[1] = RecieveByte ( 1 );     //ACK
  508.     pbuf[2] = RecieveByte ( 1 );     //ACK
  509.     pbuf[3] = RecieveByte ( 1 );     //ACK
  510.     pbuf[4] = RecieveByte ( 1 );     //ACK
  511.     pbuf[5] = RecieveByte ( 0);     //ACK

  512.     StopI2C ( );

  513.    //crc checksum
  514.     crc8 = CaculateCRC8 ( pbuf, 2 );
  515.     if ( crc8 != pbuf[2] )
  516.                 {   
  517.                           g_temperatureSH15 =0002;
  518.         return FALSE;
  519.                 }                       
  520.     crc8 = CaculateCRC8 ( pbuf+3, 2 );

  521.     if ( crc8 != pbuf[5])
  522.                 {
  523.                  //g_temperatureSH15 =0004;                                                                        
  524.      return FALSE;
  525.                 }
  526.                
  527.     return TRUE;
  528. }

  529. /*
  530. *****************************************************************************************
  531. * fuction name: SetSHT35OneMeasure
  532. *
  533. * Description : this function transmit a byte from I2C bus.
  534. *
  535. * Arguments   : unsigned char, transmit byte in I2C bus.
  536. *
  537. * returns     : bit, acknowdge flag.
  538. *
  539. * Notes     :
  540. ******************************************************************************************
  541. */
  542. static uchar SetSHT35OneMeasure ( void )
  543. {
  544.     uchar ret;
  545.     StartI2C ( );

  546.     ret = SendByte  ( SHT35_ADDR );

  547.         if ( ret == FALSE )
  548.         {               
  549.                  return FALSE;
  550.         }            
  551.     ret = SendByte  ( SHT35_ONE_SHOT_CMD_MSB );
  552.     if ( ret == FALSE )
  553.                 {   
  554.                           return FALSE;
  555.                 }


  556.     ret = SendByte  ( SHT35_ONE_SHOT_CMD_LSB );
  557.     if ( ret == FALSE )
  558.                 {
  559.                                   return FALSE;
  560.                 }
  561.                
  562.     StopI2C ( );
  563.                
  564.     return TRUE;
  565. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表