找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5369|回复: 2
收起左侧

SHT30温湿度传感器 STM32F103 IIC程序源码

  [复制链接]
ID:70915 发表于 2018-12-26 16:33 | 显示全部楼层 |阅读模式


1、程序功能


       程序成功读取到SHT30的温湿度数据后,通过单片机的 串口1 将测得的温湿度数据打印出来。(注:程序中有4个warning,无需理会)




2、硬件连接

                         单片机                 SHT30模块


                          PB14    ----------     SDA

                          PB13    ----------     SCL



声明:程序是在 原子哥的例程--实验4 串口实验 基础上修改的,感谢原子哥为大家提供了诸多stm32教程和例程。

单片机源程序如下:
  1. //=============================================================================
  2. //    S E N S I R I O N   AG,  Laubisruetistr. 50, CH-8712 Staefa, Switzerland
  3. //=============================================================================
  4. // Project   :  SHT3x Sample Code (V1.1)
  5. // File      :  sht3x.c (V1.1)
  6. // Author    :  RFU
  7. // Date      :  6-Mai-2015
  8. // Controller:  STM32F100RB
  9. // IDE       :  礦ision V5.12.0.0
  10. // Compiler  :  Armcc
  11. // Brief     :  Sensor Layer: Implementation of functions for sensor access.
  12. //=============================================================================

  13. //-- Includes -----------------------------------------------------------------
  14. #include "sht3x.h"
  15. #include "i2c_hal.h"

  16. //-- Defines ------------------------------------------------------------------
  17. // Generator polynomial for CRC
  18. #define POLYNOMIAL  0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001

  19. //=============================================================================
  20. // IO-Pins                            /* -- adapt the defines for your uC -- */
  21. //-----------------------------------------------------------------------------
  22. // Reset on port B, bit 12
  23. #define RESET_LOW()  (GPIOB->BSRR = 0x10000000) // set Reset to low
  24. #define RESET_HIGH() (GPIOB->BSRR = 0x00001000) // set Reset to high

  25. // Alert on port B, bit 10
  26. #define ALERT_READ   (GPIOB->IDR  & 0x0400)     // read Alert
  27. //=============================================================================

  28. //-- Global variables ---------------------------------------------------------
  29. static u8t _i2cAddress; // I2C Address

  30. //-- Static function prototypes -----------------------------------------------
  31. static etError SHT3X_WriteAlertLimitData(ft humidity, ft temperature);
  32. static etError SHT3X_ReadAlertLimitData(ft* humidity, ft* temperature);
  33. static etError SHT3X_StartWriteAccess(void);
  34. static etError SHT3X_StartReadAccess(void);
  35. static void SHT3X_StopAccess(void);
  36. static etError SHT3X_WriteCommand(etCommands command);
  37. static etError SHT3X_Read2BytesAndCrc(u16t* data, etI2cAck finaleAckNack,
  38.                                       u8t timeout);
  39. static etError SHT3X_Write2BytesAndCrc(u16t data);
  40. static u8t SHT3X_CalcCrc(u8t data[], u8t nbrOfBytes);
  41. static etError SHT3X_CheckCrc(u8t data[], u8t nbrOfBytes, u8t checksum);
  42. static ft SHT3X_CalcTemperature(u16t rawValue);
  43. static ft SHT3X_CalcHumidity(u16t rawValue);
  44. static u16t SHT3X_CalcRawTemperature(ft temperature);
  45. static u16t SHT3X_CalcRawHumidity(ft humidity);

  46. //-----------------------------------------------------------------------------
  47. void SHT3X_Init(u8t i2cAddress)          /* -- adapt the init for your uC -- */
  48. {
  49.   // init I/O-pins
  50.   RCC->APB2ENR |= 0x00000008;  // I/O port B clock enabled
  51.   
  52.   // Alert on port B, bit 10
  53.   GPIOB->CRH   &= 0xFFFFF0FF;  // set floating input for Alert-Pin
  54.   GPIOB->CRH   |= 0x00000400;  //
  55.   
  56.   // Reset on port B, bit 12
  57.   GPIOB->CRH   &= 0xFFF0FFFF;  // set push-pull output for Reset pin
  58.   GPIOB->CRH   |= 0x00010000;  //
  59.   RESET_LOW();
  60.   
  61.   I2c_Init(); // init I2C
  62.   SHT3X_SetI2cAdr(i2cAddress);
  63.   
  64.   // release reset
  65.   RESET_HIGH();
  66. }

  67. //-----------------------------------------------------------------------------
  68. void SHT3X_SetI2cAdr(u8t i2cAddress)
  69. {
  70.   _i2cAddress = i2cAddress;
  71. }

  72. //-----------------------------------------------------------------------------
  73. etError SHT3x_ReadSerialNumber(u32t* serialNumber)
  74. {
  75.   etError error; // error code
  76.   u16t serialNumWords[2];
  77.   
  78.   error = SHT3X_StartWriteAccess();
  79.   
  80.   // write "read serial number" command
  81.   error |= SHT3X_WriteCommand(CMD_READ_SERIALNBR);
  82.   // if no error, start read access
  83.   if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  84.   // if no error, read first serial number word
  85.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&serialNumWords[0], ACK, 100);
  86.   // if no error, read second serial number word
  87.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&serialNumWords[1], NACK, 0);
  88.   
  89.   SHT3X_StopAccess();
  90.   
  91.   // if no error, calc serial number as 32-bit integer
  92.   if(error == NO_ERROR)
  93.   {
  94.     *serialNumber = (serialNumWords[0] << 16) | serialNumWords[1];
  95.   }
  96.   
  97.   return error;
  98. }

  99. //-----------------------------------------------------------------------------
  100. etError SHT3X_ReadStatus(u16t* status)
  101. {
  102.   etError error; // error code
  103.   
  104.   error = SHT3X_StartWriteAccess();
  105.   
  106.   // if no error, write "read status" command
  107.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_READ_STATUS);
  108.   // if no error, start read access
  109.   if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  110.   // if no error, read status
  111.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(status, NACK, 0);
  112.   
  113.   SHT3X_StopAccess();
  114.   
  115.   return error;
  116. }

  117. //-----------------------------------------------------------------------------
  118. etError SHT3X_ClearAllAlertFlags(void)
  119. {
  120.   etError error; // error code
  121.   
  122.   error = SHT3X_StartWriteAccess();
  123.   
  124.   // if no error, write clear status register command
  125.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_CLEAR_STATUS);
  126.   
  127.   SHT3X_StopAccess();
  128.   
  129.   return error;
  130. }

  131. //-----------------------------------------------------------------------------
  132. etError SHT3X_GetTempAndHumi(ft* temperature, ft* humidity,
  133.                              etRepeatability repeatability, etMode mode,
  134.                              u8t timeout)
  135. {
  136.   etError error;
  137.                               
  138.   switch(mode)
  139.   {   
  140.     case MODE_CLKSTRETCH: // get temperature with clock stretching mode
  141.       error = SHT3X_GetTempAndHumiClkStretch(temperature, humidity,
  142.                                              repeatability, timeout);
  143.       break;
  144.     case MODE_POLLING:    // get temperature with polling mode
  145.       error = SHT3X_GetTempAndHumiPolling(temperature, humidity,
  146.                                           repeatability, timeout);
  147.       break;
  148.     default:              
  149.       error = PARM_ERROR;
  150.       break;
  151.   }
  152.   
  153.   return error;
  154. }


  155. //-----------------------------------------------------------------------------
  156. etError SHT3X_GetTempAndHumiClkStretch(ft* temperature, ft* humidity,
  157.                                        etRepeatability repeatability,
  158.                                        u8t timeout)
  159. {
  160.   etError error;        // error code
  161.   u16t    rawValueTemp; // temperature raw value from sensor
  162.   u16t    rawValueHumi; // humidity raw value from sensor
  163.   
  164.   error = SHT3X_StartWriteAccess();
  165.   
  166.   // if no error ...
  167.   if(error == NO_ERROR)
  168.   {
  169.     // start measurement in clock stretching mode
  170.     // use depending on the required repeatability, the corresponding command
  171.     switch(repeatability)
  172.     {
  173.       case REPEATAB_LOW:
  174.         error = SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_L);
  175.         break;
  176.       case REPEATAB_MEDIUM:
  177.         error = SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_M);
  178.         break;
  179.       case REPEATAB_HIGH:
  180.         error = SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_H);
  181.         break;
  182.       default:
  183.         error = PARM_ERROR;
  184.         break;
  185.     }
  186.   }

  187.   // if no error, start read access
  188.   if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  189.   // if no error, read temperature raw values
  190.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&rawValueTemp, ACK, timeout);
  191.   // if no error, read humidity raw values
  192.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&rawValueHumi, NACK, 0);
  193.   
  194.   SHT3X_StopAccess();
  195.   
  196.   // if no error, calculate temperature in 癈 and humidity in %RH
  197.   if(error == NO_ERROR)
  198.   {
  199.     *temperature = SHT3X_CalcTemperature(rawValueTemp);
  200.     *humidity = SHT3X_CalcHumidity(rawValueHumi);
  201.   }
  202.   
  203.   return error;
  204. }

  205. //-----------------------------------------------------------------------------
  206. etError SHT3X_GetTempAndHumiPolling(ft* temperature, ft* humidity,
  207.                                     etRepeatability repeatability,
  208.                                     u8t timeout)
  209. {
  210.   etError error;           // error code
  211.   u16t    rawValueTemp;    // temperature raw value from sensor
  212.   u16t    rawValueHumi;    // humidity raw value from sensor
  213.   
  214.   error  = SHT3X_StartWriteAccess();
  215.   
  216.   // if no error ...
  217.   if(error == NO_ERROR)
  218.   {
  219.     // start measurement in polling mode
  220.     // use depending on the required repeatability, the corresponding command
  221.     switch(repeatability)
  222.     {
  223.       case REPEATAB_LOW:
  224.         error = SHT3X_WriteCommand(CMD_MEAS_POLLING_L);
  225.         break;
  226.       case REPEATAB_MEDIUM:
  227.         error = SHT3X_WriteCommand(CMD_MEAS_POLLING_M);
  228.         break;
  229.       case REPEATAB_HIGH:
  230.         error = SHT3X_WriteCommand(CMD_MEAS_POLLING_H);
  231.         break;
  232.       default:
  233.         error = PARM_ERROR;
  234.         break;
  235.     }
  236.   }
  237.   
  238.   // if no error, wait until measurement ready
  239.   if(error == NO_ERROR)
  240.   {
  241.     // poll every 1ms for measurement ready until timeout
  242.     while(timeout--)
  243.     {
  244.       // check if the measurement has finished
  245.       error = SHT3X_StartReadAccess();
  246.   
  247.       // if measurement has finished -> exit loop
  248.       if(error == NO_ERROR) break;
  249.   
  250.       // delay 1ms
  251.       DelayMicroSeconds(1000);
  252.     }
  253.    
  254.     // check for timeout error
  255.     if(timeout == 0) error = TIMEOUT_ERROR;
  256.   }
  257.   
  258.   // if no error, read temperature and humidity raw values
  259.   if(error == NO_ERROR)
  260.   {
  261.     error |= SHT3X_Read2BytesAndCrc(&rawValueTemp, ACK, 0);
  262.     error |= SHT3X_Read2BytesAndCrc(&rawValueHumi, NACK, 0);
  263.   }
  264.   
  265.   SHT3X_StopAccess();
  266.   
  267.   // if no error, calculate temperature in 癈 and humidity in %RH
  268.   if(error == NO_ERROR)
  269.   {
  270.     *temperature = SHT3X_CalcTemperature(rawValueTemp);
  271.     *humidity = SHT3X_CalcHumidity(rawValueHumi);
  272.   }
  273.   
  274.   return error;
  275. }

  276. //-----------------------------------------------------------------------------
  277. etError SHT3X_StartPeriodicMeasurment(etRepeatability repeatability,
  278.                                       etFrequency frequency)
  279. {
  280.   etError error;        // error code
  281.   
  282.   error = SHT3X_StartWriteAccess();
  283.   
  284.   // if no error, start periodic measurement
  285.   if(error == NO_ERROR)
  286.   {
  287.     // use depending on the required repeatability and frequency,
  288.     // the corresponding command
  289.     switch(repeatability)
  290.     {
  291.       case REPEATAB_LOW: // low repeatability
  292.         switch(frequency)
  293.         {
  294.           case FREQUENCY_HZ5:  // low repeatability,  0.5 Hz
  295.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_05_L);
  296.             break;         
  297.           case FREQUENCY_1HZ:  // low repeatability,  1.0 Hz
  298.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_1_L);
  299.             break;         
  300.           case FREQUENCY_2HZ:  // low repeatability,  2.0 Hz
  301.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_2_L);
  302.             break;         
  303.           case FREQUENCY_4HZ:  // low repeatability,  4.0 Hz
  304.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_4_L);
  305.             break;         
  306.           case FREQUENCY_10HZ: // low repeatability, 10.0 Hz
  307.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_10_L);
  308.             break;         
  309.           default:
  310.             error |= PARM_ERROR;
  311.             break;
  312.         }
  313.         break;
  314.         
  315.       case REPEATAB_MEDIUM: // medium repeatability
  316.         switch(frequency)
  317.         {
  318.           case FREQUENCY_HZ5:  // medium repeatability,  0.5 Hz
  319.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_05_M);
  320.                         break;
  321.           case FREQUENCY_1HZ:  // medium repeatability,  1.0 Hz
  322.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_1_M);
  323.                         break;        
  324.           case FREQUENCY_2HZ:  // medium repeatability,  2.0 Hz
  325.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_2_M);
  326.                         break;        
  327.           case FREQUENCY_4HZ:  // medium repeatability,  4.0 Hz
  328.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_4_M);
  329.                         break;      
  330.           case FREQUENCY_10HZ: // medium repeatability, 10.0 Hz
  331.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_10_M);
  332.                         break;
  333.           default:
  334.             error |= PARM_ERROR;
  335.                         break;
  336.         }
  337.         break;
  338.         
  339.       case REPEATAB_HIGH: // high repeatability
  340.         switch(frequency)
  341.         {
  342.           case FREQUENCY_HZ5:  // high repeatability,  0.5 Hz
  343.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_05_H);
  344.             break;
  345.           case FREQUENCY_1HZ:  // high repeatability,  1.0 Hz
  346.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_1_H);
  347.             break;
  348.           case FREQUENCY_2HZ:  // high repeatability,  2.0 Hz
  349.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_2_H);
  350.             break;
  351.           case FREQUENCY_4HZ:  // high repeatability,  4.0 Hz
  352.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_4_H);
  353.             break;
  354.           case FREQUENCY_10HZ: // high repeatability, 10.0 Hz
  355.             error |= SHT3X_WriteCommand(CMD_MEAS_PERI_10_H);
  356.             break;
  357.           default:
  358.             error |= PARM_ERROR;
  359.             break;
  360.         }
  361.         break;
  362.       default:
  363.         error |= PARM_ERROR;
  364.         break;
  365.     }
  366.   }

  367.   SHT3X_StopAccess();

  368.   return error;
  369. }

  370. //-----------------------------------------------------------------------------
  371. etError SHT3X_ReadMeasurementBuffer(ft* temperature, ft* humidity)
  372. {
  373.   etError  error;        // error code
  374.   u16t     rawValueTemp; // temperature raw value from sensor
  375.   u16t     rawValueHumi; // humidity raw value from sensor

  376.   error = SHT3X_StartWriteAccess();

  377.   // if no error, read measurements
  378.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_FETCH_DATA);
  379.   if(error == NO_ERROR) error = SHT3X_StartReadAccess();  
  380.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&rawValueTemp, ACK, 0);
  381.   if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&rawValueHumi, NACK, 0);

  382.   // if no error, calculate temperature in 癈 and humidity in %RH
  383.   if(error == NO_ERROR)
  384.   {
  385.     *temperature = SHT3X_CalcTemperature(rawValueTemp);
  386.     *humidity = SHT3X_CalcHumidity(rawValueHumi);
  387.   }

  388.   SHT3X_StopAccess();

  389.   return error;
  390. }

  391. //-----------------------------------------------------------------------------
  392. etError SHT3X_EnableHeater(void)
  393. {
  394.   etError error; // error code

  395.   error = SHT3X_StartWriteAccess();

  396.   // if no error, write heater enable command
  397.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_HEATER_ENABLE);

  398.   SHT3X_StopAccess();

  399.   return error;
  400. }

  401. //-----------------------------------------------------------------------------
  402. etError SHT3X_DisableHeater(void)
  403. {
  404.   etError error; // error code

  405.   error = SHT3X_StartWriteAccess();

  406.   // if no error, write heater disable command
  407.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_HEATER_DISABLE);

  408.   SHT3X_StopAccess();

  409.   return error;
  410. }


  411. //-----------------------------------------------------------------------------
  412. etError SHT3X_SetAlertLimits(ft humidityHighSet,   ft temperatureHighSet,
  413.                              ft humidityHighClear, ft temperatureHighClear,
  414.                              ft humidityLowClear,  ft temperatureLowClear,
  415.                              ft humidityLowSet,    ft temperatureLowSet)
  416. {
  417.   etError  error;  // error code
  418.   
  419.   // write humidity & temperature alter limits, high set
  420.   error = SHT3X_StartWriteAccess();
  421.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_W_AL_LIM_HS);
  422.   if(error == NO_ERROR) error = SHT3X_WriteAlertLimitData(humidityHighSet,
  423.                                                           temperatureHighSet);
  424.   SHT3X_StopAccess();

  425.   if(error == NO_ERROR)
  426.   {
  427.     // write humidity & temperature alter limits, high clear
  428.     error = SHT3X_StartWriteAccess();
  429.     if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_W_AL_LIM_HC);
  430.     if(error == NO_ERROR) error = SHT3X_WriteAlertLimitData(humidityHighClear,
  431.                                                             temperatureHighClear);
  432.     SHT3X_StopAccess();
  433.   }

  434.   if(error == NO_ERROR)
  435.   {
  436.     // write humidity & temperature alter limits, low clear
  437.     error = SHT3X_StartWriteAccess();
  438.     if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_W_AL_LIM_LC);
  439.     if(error == NO_ERROR) error = SHT3X_WriteAlertLimitData(humidityLowClear,
  440.                                                             temperatureLowClear);
  441.     SHT3X_StopAccess();
  442.   }
  443.   
  444.   if(error == NO_ERROR)
  445.   {
  446.     // write humidity & temperature alter limits, low set
  447.     error = SHT3X_StartWriteAccess();
  448.     if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_W_AL_LIM_LS);
  449.     if(error == NO_ERROR) error = SHT3X_WriteAlertLimitData(humidityLowSet,
  450.                                                             temperatureLowSet);
  451.     SHT3X_StopAccess();
  452.   }

  453.   return error;
  454. }

  455. //-----------------------------------------------------------------------------
  456. etError SHT3X_GetAlertLimits(ft* humidityHighSet,   ft* temperatureHighSet,
  457.                              ft* humidityHighClear, ft* temperatureHighClear,
  458.                              ft* humidityLowClear,  ft* temperatureLowClear,
  459.                              ft* humidityLowSet,    ft* temperatureLowSet)
  460. {
  461.   etError  error;  // error code
  462.   
  463.   // read humidity & temperature alter limits, high set
  464.   error = SHT3X_StartWriteAccess();
  465.   if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_R_AL_LIM_HS);
  466.   if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  467.   if(error == NO_ERROR) error = SHT3X_ReadAlertLimitData(humidityHighSet,
  468.                                                          temperatureHighSet);
  469.   SHT3X_StopAccess();

  470.   if(error == NO_ERROR)
  471.   {
  472.     // read humidity & temperature alter limits, high clear
  473.     error = SHT3X_StartWriteAccess();
  474.     if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_R_AL_LIM_HC);
  475.     if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  476.     if(error == NO_ERROR) error = SHT3X_ReadAlertLimitData(humidityHighClear,
  477.                                                            temperatureHighClear);
  478.     SHT3X_StopAccess();
  479.   }

  480.   if(error == NO_ERROR)
  481.   {
  482.     // read humidity & temperature alter limits, low clear
  483.     error = SHT3X_StartWriteAccess();
  484.     if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_R_AL_LIM_LC);
  485.     if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  486.     if(error == NO_ERROR) error = SHT3X_ReadAlertLimitData(humidityLowClear,
  487.                                                            temperatureLowClear);
  488.     SHT3X_StopAccess();
  489.   }

  490.   if(error == NO_ERROR)
  491.   {
  492.     // read humidity & temperature alter limits, low set
  493.     error = SHT3X_StartWriteAccess();
  494.     if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_R_AL_LIM_LS);
  495.     if(error == NO_ERROR) error = SHT3X_StartReadAccess();
  496.     if(error == NO_ERROR) error = SHT3X_ReadAlertLimitData(humidityLowSet,
  497.                                                            temperatureLowSet);
  498.     SHT3X_StopAccess();
  499.   }

  500.   return error;
  501. }
  502.                              
  503. //-----------------------------------------------------------------------------
  504. bt SHT3X_ReadAlert(void)
  505. {
  506.   // read alert pin
  507.   return (ALERT_READ != 0) ? TRUE : FALSE;
  508. }

  509. //-----------------------------------------------------------------------------
  510. etError SHT3X_SoftReset(void)
  511. {
  512.   etError error; // error code

  513.   error = SHT3X_StartWriteAccess();

  514.   // write reset command
  515.   error |= SHT3X_WriteCommand(CMD_SOFT_RESET);

  516.   SHT3X_StopAccess();
  517.   
  518.   // if no error, wait 50 ms after reset
  519.   if(error == NO_ERROR) DelayMicroSeconds(50000);

  520.   return error;
  521. }

  522. //-----------------------------------------------------------------------------
  523. void SHT3X_HardReset(void)
  524. {
  525.   // set reset low
  526.   RESET_LOW();

  527.   // wait 100 ms
  528.   DelayMicroSeconds(100000);
  529.   
  530.   // release reset
  531.   RESET_HIGH();
  532.   
  533.   // wait 50 ms after reset
  534.   DelayMicroSeconds(50000);
  535. }

  536.                              
  537. //-----------------------------------------------------------------------------
  538. static etError SHT3X_WriteAlertLimitData(ft humidity, ft temperature)
  539. {
  540.   etError  error;           // error code
  541.   
  542.   i16t rawHumidity;
  543.   i16t rawTemperature;
  544.   
  545.   if((humidity < 0.0f) || (humidity > 100.0f)
  546.   || (temperature < -45.0f) || (temperature > 130.0f))
  547.   {
  548.     error = PARM_ERROR;
  549.   }
  550.   else
  551.   {
  552.     rawHumidity    = SHT3X_CalcRawHumidity(humidity);
  553.     rawTemperature = SHT3X_CalcRawTemperature(temperature);

  554.     error = SHT3X_Write2BytesAndCrc((rawHumidity & 0xFE00) | ((rawTemperature >> 7) & 0x001FF));
  555.   }
  556.   
  557.   return error;
  558. }

  559. //-----------------------------------------------------------------------------
  560. static etError SHT3X_ReadAlertLimitData(ft* humidity, ft* temperature)
  561. {
  562.   etError  error;           // error code
  563.   u16t     data;
  564.   
  565.   error = SHT3X_Read2BytesAndCrc(&data, NACK, 0);
  566.   
  567.   if(error == NO_ERROR)
  568.   {
  569.     *humidity = SHT3X_CalcHumidity(data & 0xFE00);
  570.     *temperature = SHT3X_CalcTemperature(data << 7);
  571.   }
  572.   
  573.   return error;
  574. }

  575. //-----------------------------------------------------------------------------
  576. static etError SHT3X_StartWriteAccess(void)
  577. {
  578.   etError error; // error code

  579.   // write a start condition
  580.   I2c_StartCondition();

  581.   // write the sensor I2C address with the write flag
  582.   error = I2c_WriteByte(_i2cAddress << 1);

  583.   return error;
  584. }

  585. //-----------------------------------------------------------------------------
  586. static etError SHT3X_StartReadAccess(void)
  587. {
  588.   etError error; // error code

  589.   // write a start condition
  590.   I2c_StartCondition();

  591.   // write the sensor I2C address with the read flag
  592.   error = I2c_WriteByte(_i2cAddress << 1 | 0x01);

  593.   return error;
  594. }

  595. //-----------------------------------------------------------------------------
  596. static void SHT3X_StopAccess(void)
  597. {
  598.   // write a stop condition
  599.   I2c_StopCondition();
  600. }

  601. //-----------------------------------------------------------------------------
  602. static etError SHT3X_WriteCommand(etCommands command)
  603. {
  604.   etError error; // error code

  605.   // write the upper 8 bits of the command to the sensor
  606.   error  = I2c_WriteByte(command >> 8);

  607.   // write the lower 8 bits of the command to the sensor
  608.   error |= I2c_WriteByte(command & 0xFF);

  609.   return error;
  610. }

  611. //-----------------------------------------------------------------------------
  612. static etError SHT3X_Read2BytesAndCrc(u16t* data, etI2cAck finaleAckNack,
  613.                                       u8t timeout)
  614. {
  615.   etError error;    // error code
  616.   u8t     bytes[2]; // read data array
  617.   u8t     checksum; // checksum byte

  618.   // read two data bytes and one checksum byte
  619.                         error = I2c_ReadByte(&bytes[0], ACK, timeout);
  620.   if(error == NO_ERROR) error = I2c_ReadByte(&bytes[1], ACK, 0);
  621.   if(error == NO_ERROR) error = I2c_ReadByte(&checksum, finaleAckNack, 0);
  622.   
  623.   // verify checksum
  624.   if(error == NO_ERROR) error = SHT3X_CheckCrc(bytes, 2, checksum);
  625.   
  626.   // combine the two bytes to a 16-bit value
  627.   *data = (bytes[0] << 8) | bytes[1];
  628.   
  629.   return error;
  630. }

  631. //-----------------------------------------------------------------------------
  632. static etError SHT3X_Write2BytesAndCrc(u16t data)
  633. {
  634. ……………………

  635. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
SHT3x STM32F103.rar (300.74 KB, 下载次数: 182)

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:828831 发表于 2021-4-6 15:27 | 显示全部楼层
请问你看过传感的手册吗?
不用等待ACK吗?
不用配置IIC的方向吗
回复

使用道具 举报

ID:919071 发表于 2021-5-11 00:42 | 显示全部楼层
这篇文章很详细,很容易理解
谢谢!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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