test_dht11.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. typedef struct
  23. {
  24. uint32_t LastValue;
  25. uint8_t Data[6];
  26. uint8_t Pos;
  27. uint8_t BitPos;
  28. uint8_t BytePos;
  29. }DHT11_CtrlStruct;
  30. static int32_t DHT11_ReadBit(void *pData, void *pParam)
  31. {
  32. OPQueue_CmdStruct *Cmd = (OPQueue_CmdStruct *)pData;
  33. DHT11_CtrlStruct *DHT11 = (DHT11_CtrlStruct *)pParam;
  34. uint32_t diff;
  35. switch(DHT11->Pos)
  36. {
  37. case 0:
  38. // DBG("%u", Cmd->uParam.MaxCnt);
  39. break;
  40. case 1:
  41. // DBG("%u", Cmd->uParam.MaxCnt);
  42. DHT11->LastValue = Cmd->uParam.MaxCnt;
  43. DHT11->BitPos = 7;
  44. DHT11->BytePos = 0;
  45. DHT11->Data[DHT11->BytePos] = 0;
  46. break;
  47. default:
  48. diff = (Cmd->uParam.MaxCnt - DHT11->LastValue);
  49. DHT11->LastValue = Cmd->uParam.MaxCnt;
  50. // DBG("%u,%u,%u,%u", DHT11->BytePos, DHT11->BitPos, diff, Cmd->uParam.MaxCnt);
  51. if (diff > (100 * SYS_TIMER_1US))
  52. {
  53. DHT11->Data[DHT11->BytePos] |= (1 << DHT11->BitPos);
  54. }
  55. if (DHT11->BitPos)
  56. {
  57. DHT11->BitPos--;
  58. }
  59. else
  60. {
  61. DHT11->BitPos = 7;
  62. DHT11->BytePos++;
  63. DHT11->Data[DHT11->BytePos] = 0;
  64. }
  65. }
  66. DHT11->Pos++;
  67. }
  68. void DHT11_TestOnce(uint8_t Pin, CBFuncEx_t CB)
  69. {
  70. uint8_t HWTimerID = HW_TIMER0;
  71. uint32_t i;
  72. OPQueue_CmdStruct OPCmd;
  73. HANDLE TaskHandle = Task_GetCurrent();
  74. HWTimer_InitOperationQueue(HWTimerID, 100, 1, 0, CB, TaskHandle);
  75. OPCmd.Operation = OP_QUEUE_CMD_SET_GPIO_DIR_OUT;
  76. OPCmd.Arg1 = Pin;
  77. OPCmd.uArg.IOArg.Level = 0;
  78. OPCmd.uArg.IOArg.PullMode = OP_QUEUE_CMD_IO_PULL_UP;
  79. HWTimer_AddOperation(HWTimerID, &OPCmd);
  80. OPCmd.Operation = OP_QUEUE_CMD_ONE_TIME_DELAY;
  81. OPCmd.Arg1 = 0;
  82. OPCmd.uArg.Time = 20000;
  83. HWTimer_AddOperation(HWTimerID, &OPCmd);
  84. OPCmd.Operation = OP_QUEUE_CMD_CAPTURE_SET;
  85. OPCmd.Arg1 = Pin;
  86. OPCmd.uArg.ExitArg.ExtiMode = OP_QUEUE_CMD_IO_EXTI_DOWN;
  87. OPCmd.uArg.ExitArg.PullMode = OP_QUEUE_CMD_IO_PULL_UP;
  88. OPCmd.uParam.MaxCnt = 100 * SYS_TIMER_1MS;
  89. HWTimer_AddOperation(HWTimerID, &OPCmd);
  90. OPCmd.Operation = OP_QUEUE_CMD_CAPTURE;
  91. OPCmd.Arg1 = Pin;
  92. OPCmd.uArg.IOArg.Level = 0xff;
  93. for(i = 0; i < 43; i++) //40bit data+2bit start+1bit end
  94. {
  95. HWTimer_AddOperation(HWTimerID, &OPCmd);
  96. }
  97. OPCmd.Operation = OP_QUEUE_CMD_CAPTURE_END;
  98. OPCmd.Arg1 = Pin;
  99. HWTimer_AddOperation(HWTimerID, &OPCmd);
  100. HWTimer_StartOperationQueue(HWTimerID);
  101. }
  102. void DHT11_TestResult(void)
  103. {
  104. uint8_t HWTimerID = HW_TIMER0;
  105. DHT11_CtrlStruct DHT11;
  106. DHT11.Pos = 0;
  107. HWTimer_GetOperationQueueCaptureResult(HWTimerID, DHT11_ReadBit, &DHT11);
  108. DHT11.Data[5] = SumCheck(DHT11.Data, 4);
  109. if (DHT11.Data[4] == DHT11.Data[5])
  110. {
  111. DBG("湿度 %d.%d, 温度 %d.%d", DHT11.Data[0], DHT11.Data[1], DHT11.Data[2], DHT11.Data[3]);
  112. }
  113. else
  114. {
  115. DBG("check error %x,%x", DHT11.Data[4], DHT11.Data[5]);
  116. DBG_HexPrintf(DHT11.Data, 4);
  117. }
  118. }