找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2488|回复: 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 单片机教程网

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