久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2472|回復: 0
打印 上一主題 下一主題
收起左側

STM32 TMP101無線測溫傳感器低功耗+RF源程序

[復制鏈接]
跳轉到指定樓層
樓主
分享一個無線測溫傳感器的設計資料,原理圖+代碼,經測試可以用。


單片機源程序如下:
  1. /*
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5. */
  6. #include "stm32l0xx_hal.h"
  7. #include "rtc.h"
  8. #include "rcc.h"

  9. #include "communication.h"
  10. #include "LowPower.h"
  11. #include "sensor.h"

  12. #define SLEEP_MODE 0       //為0時是低功耗模式,為非零時我延時模式

  13. void _Error_Handler(char * file, int line);
  14. int main(void)
  15. {
  16.     int timer = 0;
  17.         HAL_Init();
  18.         
  19.         SystemClock_Config();
  20.         WirelessInit();//配置通信
  21.         RTC_Config();
  22.         HAL_Delay(100);
  23.    
  24.     SensorCheck(); //識別硬件版本
  25.     SensorInit();  //初始化各個設備
  26.         while(1)
  27.         {   
  28.         SystemClock_Config();     //系統時鐘配置,回復后重新配置
  29.         
  30.         SensorUpload();         //傳感器數據處理
  31.         WirelessTaskLowPower(); //nrf數據發送處理
  32.         
  33.         if(WirelessTxIsNotEmpyt()) //如果nrf還要要發送的數據
  34.         {
  35.             SleepTime(10,SLEEP_MODE);//延時方式
  36.             SensorTimerDec(9);      //傳感器內部計時器減10s
  37.         }
  38.         else
  39.         {
  40.             SleepTime(SensorTimerGet(),SLEEP_MODE); //等待延時
  41.             SensorTimerSet(-1);                     //延時完成一個周器,觸發一次測量
  42.         }
  43.         
  44.         }
  45. }

  46. /* USER CODE BEGIN 4 */
  47. /**
  48.   * @brief  RTC Wake Up callback
  49.   * @param  None
  50.   * @retval None
  51.   * RTC喚醒中斷的處理函數中斷的處理內容可以在此處執行
  52.   */
  53. void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  54. {
  55.   /* Clear Wake Up Flag */
  56.   __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
  57. }

  58. /* USER CODE END 4 */

  59. /**
  60.   * @brief  This function is executed in case of error occurrence.
  61.   * @param  None
  62.   * @retval None
  63.   */
  64. void _Error_Handler(char * file, int line)
  65. {
  66.   /* USER CODE BEGIN Error_Handler_Debug */
  67.   /* User can add his own implementation to report the HAL error return state */
  68.   while(1)
  69.   {
  70.   }
  71.   /* USER CODE END Error_Handler_Debug */
  72. }

  73. #ifdef USE_FULL_ASSERT

  74. /**
  75.    * @brief Reports the name of the source file and the source line number
  76.    * where the assert_param error has occurred.
  77.    * @param file: pointer to the source file name
  78.    * @param line: assert_param error line source number
  79.    * @retval None
  80.    */
  81. void assert_failed(uint8_t* file, uint32_t line)
  82. {
  83.   /* USER CODE BEGIN 6 */
  84.   /* User can add his own implementation to report the file name and line number,
  85.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  86.   /* USER CODE END 6 */

  87. }
  88. #endif

  89. /**
  90.   * @}
  91.   */

  92. /**
  93.   * @}
  94. */

  95. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復制代碼
  1. #include "SI7021.h"
  2. #include "Battery.h"
  3. #include "temperature.h"
  4. #include "fifo.h"
  5. #include "led.h"
  6. #include "sensor.h"

  7. #define HARD_NUM         4
  8. #define SENSOR_NUM       3     //定義傳感器的數量
  9. #define SENSOR_BUF_NUM   7
  10. enum {
  11.     BATTERY,
  12.     TEMP101,
  13.     SI1702,
  14. };
  15. static int sensorState[SENSOR_NUM] ={0,0,0};  //用來表示對應的傳感器是否在線,如果不在線為0,用來識別硬件
  16. static uint8_t sensorBuf[SENSOR_BUF_NUM]; //傳感器數據緩存的buf

  17. //檢測設備已獲取硬件版本
  18. //適用硬件版本如下:OVE350S取電,OVE350S,OVE350S-C,OVE350S-B,和硬件故障
  19. static enum{
  20.     OVE350S_A = 0,
  21.     OVE350S_B,
  22.     OVE350S_C,
  23.     OVE350S_AA,
  24.     HARD_ERROR,
  25. }hardVersion;
  26. static int sampleCycle[HARD_NUM] ={20,20,20,20}; //定義每個硬件產品的循環周期
  27. static int sampleCountMax[HARD_NUM] = {6,6,6,6};  //定義最遲幾個采樣周期,發送一次數據,為了防止數據不變的時候一致不發送,定一個最低的周期
  28. static int timer=0;                      //需要外部一個以s為單位的,石基單元驅動
  29. static int sampleCount = 0;

  30. //計時器為遞減模式
  31. int SensorTimerGet(void)
  32. {
  33.     return timer;
  34. }
  35. void SensorTimerSet(int inc)
  36. {
  37.     timer = inc;
  38. }
  39. void SensorTimerDec(int dec)
  40. {
  41.     timer = timer - dec;
  42. }
  43. //檢查硬件已獲得硬件版本號

  44. //#define SET_DEV OVE350S_C //如果指定硬件版本
  45. void SensorCheck(void)
  46. {
  47.    
  48. #ifdef SET_DEV
  49.     hardVersion = SET_DEV; //硬件版本為指定
  50. #else                       //否則,自動檢測
  51.     sensorState[0] = 0;
  52.     sensorState[1] = 0;
  53.     sensorState[2] = 0;
  54.    
  55.     sensorState[TEMP101] = TempCheck(0);
  56.     sensorState[SI1702]  = SI70XX_Check(0);


  57.     sensorState[BATTERY] = BatteryCheck();
  58.     if((sensorState[BATTERY]!=0)&&(sensorState[TEMP101]!=0)&&(sensorState[SI1702]==0))
  59.     {
  60.         hardVersion = OVE350S_B;
  61.     }
  62.     else if((sensorState[BATTERY]!=0)&&(sensorState[SI1702]!=0)&&(sensorState[TEMP101]==0))
  63.     {
  64.         hardVersion = OVE350S_C;
  65.     }
  66.     else if((sensorState[BATTERY]==0)&&(sensorState[TEMP101]!=0)&&(sensorState[SI1702]==0))
  67.     {
  68.         hardVersion = OVE350S_A;
  69.     }
  70.     else
  71.     {
  72.         hardVersion = HARD_ERROR;
  73.     }

  74. #endif
  75.    
  76. }

  77. void SensorInit(void)
  78. {
  79.     switch(hardVersion)
  80.     {
  81.         case OVE350S_A:  //通用版硬件取電模式
  82.         {
  83.             TempInit(0);
  84.             break;
  85.         }
  86.         case OVE350S_B:  //通用版硬件電池模式
  87.         {
  88.             BatteryMeasereInit();
  89.             TempInit(0);
  90.             break;
  91.         }
  92.         case OVE350S_C:  //通用版硬件,溫濕度模式
  93.         {
  94.             BatteryMeasereInit();
  95.             SI70XX_Init(0);
  96.             break;
  97.         }
  98.         case HARD_ERROR: //都不是,上報硬件錯誤,led等常亮
  99.         {
  100.             while(1)
  101.             {
  102.                 LedOn();
  103.             }
  104.             break;
  105.         }
  106.         case OVE350S_AA:
  107.         {
  108.             while(1)
  109.             {
  110.                 LedOn();
  111.             }
  112.             break;
  113.         }
  114.         default:
  115.         {
  116.             while(1)
  117.             {
  118.                 LedOn();
  119.                 HAL_Delay(300);
  120.                 LedOff();
  121.                 HAL_Delay(300);
  122.             }            
  123.             break;
  124.         }
  125.     }
  126. }


  127. //獲取溫度,濕度,和電壓值,每個數據緩存在兩個字節的空間中
  128. //第0-1字節:溫度
  129. //第2-3字節:濕度
  130. //第4-5字節:電壓
  131. //第6字節:  空閑
  132. int SensorGet(void)
  133. {
  134.     int ret = 0;
  135.     switch(hardVersion)
  136.     {
  137.         case OVE350S_A:
  138.         {
  139.             SensorTimerSet(sampleCycle[OVE350S_A]); //填入采用周期
  140.             if(TempGetLowPower(&sensorBuf[0]))
  141.             {
  142.                 ret = 1;
  143.             }
  144.             *(uint16_t *)&sensorBuf[2] = 0;//濕度為零
  145.             *(uint16_t *)&sensorBuf[4] = 0;//電壓為零
  146.             sensorBuf[6] =0 ;//空閑位為零
  147.             break;
  148.         }
  149.         case OVE350S_B:
  150.         {
  151.             SensorTimerSet(sampleCycle[OVE350S_B]);
  152.             if(TempGetLowPower(&sensorBuf[0]))
  153.             {
  154.                 ret = 1;
  155.             }
  156.             *(uint16_t *)&sensorBuf[2] = 0;//濕度為零     
  157.             if(BatteryGetLowPower(&sensorBuf[4]))
  158.             {
  159.                 ret = 1;
  160.             }            
  161.             sensorBuf[6] =0 ;//空閑位為零
  162.             break;
  163.         }
  164.         case OVE350S_C:
  165.         {
  166.             SensorTimerSet(sampleCycle[OVE350S_C]);
  167.             if(TempAndHumiGetLowPower(&sensorBuf[0]))//溫度濕度一起處理,4個字節
  168.             {
  169.                 ret = 1;
  170.             }
  171.             if(BatteryGetLowPower(&sensorBuf[4]))
  172.             {
  173.                 ret = 1;
  174.             }            
  175.             sensorBuf[6] =0 ;//空閑位為零
  176.             break;
  177.         }
  178.         case OVE350S_AA:
  179.         {
  180.             break;
  181.         }
  182.         case HARD_ERROR:
  183.         {
  184.             
  185.             break;
  186.         }
  187.         default:
  188.         {
  189.             
  190.             break;
  191.         }
  192.     }
  193.     return ret;
  194. }

  195. //上傳傳感器數據
  196. void SensorUpload(void)
  197. {
  198.     if(SensorTimerGet()<=0)  //如果計時器時間到
  199.     {
  200.         sampleCount ++;
  201.         if(SensorGet())       //如果需要發送信息,則將數據給fifo
  202.         {
  203.             sampleCount = 0;
  204.             FIFO_Insert(&sensorBuf[0],SENSOR_BUF_NUM);
  205.         }
  206.         else
  207.         {            
  208.             if(sampleCount>=sampleCountMax[hardVersion])
  209.             {
  210.                 sampleCount = 0;
  211.                 FIFO_Insert(&sensorBuf[0],SENSOR_BUF_NUM);
  212.             }
  213.         }
  214.     }
  215. }


復制代碼


所有資料51hei提供下載:
OVE350S_v1.1.7z (1.23 MB, 下載次數: 20)
Sensor.pdf (60.78 KB, 下載次數: 7)
Battery.pdf (147.79 KB, 下載次數: 7)

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 伊人亚洲 | 亚洲成人一区二区 | av大片 | 成年人免费在线视频 | 中文字幕 亚洲一区 | 久久亚洲一区二区 | 日韩一级| 91一区二区三区 | 亚洲国产激情 | 免费一级片| 亚洲精品一 | 午夜资源| 午夜午夜精品一区二区三区文 | 亚洲视频一区 | 亚洲欧美日韩电影 | 青青草社区 | 成人a在线观看 | 全免费a级毛片免费看视频免 | 欧州一区二区 | 国产精品毛片一区二区三区 | 日韩在线一区二区 | 国产一区亚洲 | 成人午夜视频在线观看 | 国产精品区二区三区日本 | 久久久www成人免费无遮挡大片 | 精品美女在线观看视频在线观看 | 欧美成人第一页 | 午夜网站视频 | 欧美成年网站 | 亚洲精品二三区 | 毛片站 | 青草视频在线 | 欧美色性 | 日本视频在线播放 | 精品久久久久久久 | 免费成人高清在线视频 | 国产a一区二区 | 国产亚洲一区二区三区 | 亚洲午夜精品 | 一区二区三区在线 | 国产女人第一次做爰毛片 |