标题: 温湿度传感SHT3x源程序 STM32F100RB芯片 [打印本页]

作者: BaBaTu    时间: 2020-3-25 09:12
标题: 温湿度传感SHT3x源程序 STM32F100RB芯片
温湿度传感SHT3x

单片机源程序如下:
  1. //-- Includes -----------------------------------------------------------------
  2. #include "system.h"
  3. #include "sht3x.h"

  4. //-- Static function prototypes -----------------------------------------------
  5. static void EvalBoardPower_Init(void);
  6. static void Led_Init(void);
  7. static void UserButton_Init(void);
  8. static void LedBlueOn(void);
  9. static void LedBlueOff(void);
  10. static void LedGreenOn(void);
  11. static void LedGreenOff(void);
  12. static u8t ReadUserButton(void);

  13. //-----------------------------------------------------------------------------
  14. int main(void)
  15. {
  16.   etError   error;       // error code
  17.   u32t      serialNumber;// serial number
  18.   regStatus status;      // sensor status
  19.   ft        temperature; // temperature [癈]
  20.   ft        humidity;    // relative humidity [%RH]
  21.   bt        heater;      // heater, false: off, true: on
  22.   
  23.   SystemInit();
  24.   Led_Init();
  25.   UserButton_Init();
  26.   EvalBoardPower_Init();
  27.   
  28.   SHT3X_Init(0x45); // Address: 0x44 = Sensor on EvalBoard connector
  29.                     //          0x45 = Sensor on EvalBoard
  30.   
  31.   // wait 50ms after power on
  32.   DelayMicroSeconds(50000);   
  33.   
  34.   error = SHT3x_ReadSerialNumber(&serialNumber);
  35.   if(error != NO_ERROR){} // do error handling here
  36.   
  37.   // demonstrate a single shot measurement with clock-stretching
  38.   error = SHT3X_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH, MODE_CLKSTRETCH, 50);
  39.   if(error != NO_ERROR){} // do error handling here
  40.   
  41.   // demonstrate a single shot measurement with polling and 50ms timeout
  42.   error = SHT3X_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH, MODE_POLLING, 50);
  43.   if(error != NO_ERROR){} // do error handling here
  44.   
  45.   // loop forever
  46.   while(1)
  47.   {
  48.     error = NO_ERROR;
  49.    
  50.     // loop while no error
  51.     while(error == NO_ERROR)
  52.     {
  53.       // read status register
  54.       error |= SHT3X_ReadStatus(&status.u16);
  55.       if(error != NO_ERROR) break;
  56.       
  57.       // check if the reset bit is set after a reset or power-up
  58.       if(status.bit.ResetDetected)
  59.       {
  60.         //override default temperature and humidity alert limits (red LED)
  61.         error = SHT3X_SetAlertLimits( 70.0f,  50.0f,  // high set:   RH [%], T [癈]
  62.                                       68.0f,  48.0f,  // high clear: RH [%], T [癈]
  63.                                       32.0f,  -2.0f,  // low clear:  RH [%], T [癈]
  64.                                       30.0f,  -4.0f); // low set:    RH [%], T [癈]
  65.                     if(error != NO_ERROR) break;
  66.                
  67.         
  68.         // clear reset and alert flags
  69.         error = SHT3X_ClearAllAlertFlags();
  70.         if(error != NO_ERROR) break;
  71.         
  72.         //start periodic measurement, with high repeatability and 1 measurements per second
  73.         error = SHT3X_StartPeriodicMeasurment(REPEATAB_HIGH, FREQUENCY_1HZ);
  74.         if(error != NO_ERROR) break;
  75.         
  76.         //switch green LED on
  77.         LedGreenOn();
  78.       }
  79.         
  80.       // read measurment buffer
  81.       error = SHT3X_ReadMeasurementBuffer(&temperature, &humidity);
  82.       if(error == NO_ERROR)
  83.       {
  84.         // flash blue LED to signalise new temperature and humidity values
  85.         LedBlueOn();
  86.         DelayMicroSeconds(10000);
  87.         LedBlueOff();
  88.       }
  89.       else if (error == ACK_ERROR)
  90.       {
  91.         // there were no new values in the buffer -> ignore this error
  92.         error = NO_ERROR;
  93.       }
  94.       else break;
  95.       
  96.       // read heater status
  97.       heater = status.bit.HeaterStatus ? TRUE : FALSE;
  98.       
  99.       // if the user button is not pressed ...
  100.       if(ReadUserButton() == 0)
  101.       {
  102.          // ... and the heater is on
  103.          if(heater)
  104.          {
  105.            // switch off the sensor internal heater
  106.            error |= SHT3X_DisableHeater();
  107.            if(error != NO_ERROR) break;
  108.          }
  109.       }
  110.       else
  111.       // if the user button is pressed ...
  112.       {
  113.          // ... and the heater is off
  114.          if(!heater)
  115.          {
  116.            // switch on the sensor internal heater
  117.            error |= SHT3X_EnableHeater();
  118.            if(error != NO_ERROR) break;
  119.          }
  120.       }
  121.       
  122.       // wait 100ms
  123.       DelayMicroSeconds(100000);
  124.     }
  125.    
  126.     // in case of an error ...
  127.    
  128.     // ... switch green and blue LED off
  129.     LedGreenOff();
  130.     LedBlueOff();
  131.    
  132.     // ... try first a soft reset ...
  133.     error = SHT3X_SoftReset();
  134.    
  135.     // ... if the soft reset fails, do a hard reset
  136.     if(error != NO_ERROR)
  137.     {
  138.       SHT3X_HardReset();
  139.     }
  140.    
  141.     // flash green LED to signalise an error
  142.     LedGreenOn();
  143.     DelayMicroSeconds(10000);
  144.     LedGreenOff();
  145.   }
  146. }

  147. //-----------------------------------------------------------------------------
  148. static void EvalBoardPower_Init(void)    /* -- adapt this code for your platform -- */
  149. {
  150.   RCC->APB2ENR |= 0x00000008;  // I/O port B clock enabled
  151.   
  152.   GPIOB->CRH   &= 0x0FFF0FFF;  // set push-pull output for Vdd & GND pins
  153.   GPIOB->CRH   |= 0x10001000;  //
  154.   
  155.   GPIOB->BSRR = 0x08008000;    // set Vdd to High, set GND to Low
  156. }

  157. //-----------------------------------------------------------------------------
  158. static void Led_Init(void)               /* -- adapt this code for your platform -- */
  159. {
  160.   RCC->APB2ENR |= 0x00000010;  // I/O port C clock enabled
  161.   GPIOC->CRH   &= 0xFFFFFF00;  // set general purpose output mode for LEDs
  162.   GPIOC->CRH   |= 0x00000011;  //
  163.   GPIOC->BSRR   = 0x03000000;  // LEDs off
  164. }

  165. //-----------------------------------------------------------------------------
  166. static void UserButton_Init(void)        /* -- adapt this code for your platform -- */
  167. {
  168.   RCC->APB2ENR |= 0x00000004;  // I/O port A clock enabled
  169.   GPIOA->CRH   &= 0xFFFFFFF0;  // set general purpose input mode for User Button
  170.   GPIOA->CRH   |= 0x00000004;  //
  171. }

  172. //-----------------------------------------------------------------------------
  173. static void LedBlueOn(void)              /* -- adapt this code for your platform -- */
  174. {
  175.   GPIOC->BSRR = 0x00000100;
  176. }
  177. ……………………

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

所有资料51hei提供下载:
温湿度传感官方.zip (33.84 KB, 下载次数: 31)







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1