main.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 on 1-Wire bus with multiple DS18B20
  16. *
  17. * Board: STC8H3K32
  18. *
  19. * P35 -> DQ
  20. * GND -> GND
  21. * 3.3V -> VCC
  22. */
  23. #include "fw_hal.h"
  24. #include "ds18b20.h"
  25. uint8_t DS18B20_Buff[9], addr[8], Search_Stack[8];
  26. void PrintArray(uint8_t *arr, uint8_t start, uint8_t end)
  27. {
  28. uint8_t i;
  29. for (i = start; i < end; i++)
  30. {
  31. UART1_TxHex(*(arr + i));
  32. }
  33. }
  34. int main(void)
  35. {
  36. uint8_t i, sp;
  37. SYS_SetClock();
  38. // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
  39. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  40. DS18B20_Init();
  41. while(1)
  42. {
  43. // Reset split point and search for all DS18B20
  44. sp = 0;
  45. do
  46. {
  47. // ROM search and store ROM bytes to addr
  48. sp = DS18B20_Search(addr, Search_Stack, sp);
  49. // Print the new split point and address
  50. UART1_TxHex(sp);
  51. UART1_TxChar(' ');
  52. PrintArray(addr, 0, 8);
  53. // Convert and read from this address
  54. DS18B20_Start(addr);
  55. while (!DS18B20_AllDone())
  56. {
  57. UART1_TxChar('.');
  58. SYS_Delay(50);
  59. }
  60. DS18B20_ReadScratchpadFromAddr(addr, DS18B20_Buff);
  61. PrintArray(DS18B20_Buff, 0, 9);
  62. UART1_TxChar(' ');
  63. i = DS18B20_Crc(DS18B20_Buff, 8);
  64. UART1_TxString("CRC:");
  65. UART1_TxHex(i);
  66. UART1_TxChar(' ');
  67. UART1_TxString("\r\n");
  68. } while (sp);
  69. UART1_TxString("\r\n");
  70. SYS_Delay(1000);
  71. }
  72. }