oneWireDriver.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "stm32f1xx_hal.h"
  2. #include<stdio.h>
  3. #define DS18B20_PIN GPIO_PIN_14
  4. #define DS18B20_PORT GPIOC
  5. void us_delay(uint16_t delay)
  6. {
  7. TIM1->CNT = 0;
  8. while (TIM1->CNT < delay);
  9. }
  10. /**
  11. * Control registers for F1xx GPIOs are 2 x 2 bits
  12. * For Port C, CRH, pin 14 bits 27-24
  13. * CNF: Input mode: 01=Float input Output mode: 01=Open-drain (Reset stage is 01)
  14. * MODE: 00=Input 10=Out 2 MHz
  15. *
  16. * So bits should be for | XXXX |
  17. * Input: 0100 AND-Mask: 0xF4FFFFFF 11110100111111111111111111111111
  18. * Output: 0110 OR-Mask: 0x06000000 00000110000000000000000000000000
  19. *
  20. * For init use OR 0x04000000 and then AND 0xF4FFFFFF = Input
  21. *
  22. */
  23. void init_gpio_pin(void)
  24. {
  25. DS18B20_PORT->CRH |= 0x04000000;
  26. DS18B20_PORT->CRH &= 0xF4FFFFFF;
  27. }
  28. void gpio_set_input(void)
  29. {
  30. DS18B20_PORT->CRH &= 0xF4FFFFFF;
  31. DS18B20_PORT->BSRR = (1 << 14);
  32. }
  33. void gpio_set_output(void)
  34. {
  35. DS18B20_PORT->BSRR = (1 << 14);
  36. DS18B20_PORT->CRH |= 0x06000000;
  37. }
  38. int readInput()
  39. {
  40. if (((DS18B20_PORT->IDR) & (1 << 14)) == 0)
  41. return 0;
  42. else
  43. return 1;
  44. }
  45. void writeBitHigh() { DS18B20_PORT->BSRR = (1 << 14); }
  46. void writeBitLow() { DS18B20_PORT->BSRR = (1 << 30); }
  47. void writeSlot(uint8_t data) {
  48. gpio_set_output(); // set as output
  49. if( data == 1 ) {
  50. // write 1
  51. writeBitLow();
  52. us_delay(1);
  53. writeBitHigh();
  54. us_delay(64);
  55. }
  56. else {
  57. // write 0
  58. writeBitLow();
  59. us_delay(65);
  60. writeBitHigh();
  61. }
  62. gpio_set_input();
  63. us_delay(5);
  64. }
  65. uint8_t readSlot(void) {
  66. uint8_t retVal = 0;
  67. gpio_set_output(); // set as output
  68. writeBitLow();
  69. us_delay(1);
  70. writeBitHigh();
  71. gpio_set_input(); // 2.5mS has passed
  72. us_delay(10); // 12.5mS has passed
  73. if (readInput() == 1) // if the pin is HIGH
  74. {
  75. retVal = 1;
  76. }
  77. us_delay(55);
  78. return retVal;
  79. }
  80. void writeByte(uint8_t data)
  81. {
  82. for (int i = 0; i < 8; i++)
  83. {
  84. writeSlot( ((data & (1 << i)) == 0) ? 0:1 );
  85. }
  86. }
  87. uint8_t readByte(void)
  88. {
  89. uint8_t value = 0;
  90. for (int i = 0; i < 8; i++)
  91. {
  92. if (readSlot() == 1) value |= 1 << i;
  93. }
  94. return value;
  95. }
  96. uint8_t oneWire_init(void)
  97. {
  98. volatile int i;
  99. uint8_t retVal = 0;
  100. gpio_set_output();
  101. writeBitLow();
  102. us_delay(500);
  103. writeBitHigh();
  104. gpio_set_input();
  105. us_delay(20);
  106. for(i=0;i<48;i++) { // We must wait minimum 480uS in total. Every loop is 10uS
  107. if( readInput() == 0 ) retVal = 1; // Ok, sensor was found
  108. us_delay(10);
  109. }
  110. return retVal;
  111. }