找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STH30系列官方示例程序 STM8单片机

[复制链接]
跳转到指定楼层
楼主
ID:753786 发表于 2023-7-23 09:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
正在尝试用STM08写一个温湿度的代码,从官网下的,希望对大家有所帮助。

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

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

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

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

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

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

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

  176. //-----------------------------------------------------------------------------
  177. static void LedBlueOff(void)             /* -- adapt this code for your platform -- */
  178. {
  179.   GPIOC->BSRR = 0x01000000;
  180. }

  181. //-----------------------------------------------------------------------------
  182. static void LedGreenOn(void)             /* -- adapt this code for your platform -- */
  183. {
  184.   GPIOC->BSRR = 0x00000200;
  185. }

  186. //-----------------------------------------------------------------------------
  187. static void LedGreenOff(void)            /* -- adapt this code for your platform -- */
  188. {
  189.   GPIOC->BSRR = 0x02000000;
  190. }

  191. //-----------------------------------------------------------------------------
  192. static u8t ReadUserButton(void)          /* -- adapt this code for your platform -- */
  193. {
  194.   return (GPIOA->IDR & 0x00000001);
  195. }
复制代码

代码下载:
Sensirion_Humidity_Sensors_Software_SHT3x_Sample_Code.zip (410.11 KB, 下载次数: 5)

评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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