main.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Example code of communication with single DS18B20
  16. *
  17. * Board: STC8H3K32
  18. *
  19. * Normal Power Mode:
  20. * P35 -> DQ
  21. * GND -> GND
  22. * 5V/3.3V -> VDD
  23. *
  24. * Parasite Power Mode:
  25. * Parasite power mode requires both DS18B20 GND and Vdd to be
  26. * connected to ground. The DQ pin is the data/parasite power line,
  27. * which requires a pull-up resistor (set by PxPU command)
  28. * P35 -> DQ
  29. * GND -> GND -> VDD
  30. * 5V
  31. *
  32. * Parasite Power Mode Emulation:
  33. * In case some DS18B20 doesn't work in parasite mode, you can add one
  34. * 0.1uF capacitor and one 1N4148 to achieve it. In thise way DS18B20
  35. * actually works in normal power mode
  36. *
  37. * +-----1N4148-|>|-----+
  38. * | |
  39. * | |DS18B20|-VCC--+
  40. * | | | |
  41. * P35 --+-DQ--|DS18B20| 0.1uF
  42. * | | |
  43. * GND ----GND-|DS18B20|-GND--+
  44. *
  45. */
  46. #include "fw_hal.h"
  47. #include "ds18b20.h"
  48. void PrintArray(uint8_t *arr, uint8_t start, uint8_t end)
  49. {
  50. uint8_t i;
  51. for (i = start; i < end; i++)
  52. {
  53. UART1_TxHex(*(arr + i));
  54. }
  55. }
  56. int main(void)
  57. {
  58. uint8_t buff[9], i;
  59. SYS_SetClock();
  60. // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
  61. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  62. DS18B20_Init();
  63. while(1)
  64. {
  65. DS18B20_StartAll();
  66. /*
  67. In Parasite Power Mode, replace the while block below with
  68. SYS_Delay(x), x can be [500, 1000]
  69. */
  70. while (!DS18B20_AllDone())
  71. {
  72. UART1_TxChar('.');
  73. SYS_Delay(50);
  74. }
  75. DS18B20_ReadScratchpad(buff);
  76. PrintArray(buff, 0, 9);
  77. UART1_TxChar(' ');
  78. i = DS18B20_Crc(buff, 8);
  79. UART1_TxString("CRC:");
  80. UART1_TxHex(i);
  81. UART1_TxChar(' ');
  82. UART1_TxString("\r\n");
  83. SYS_Delay(1000);
  84. }
  85. }