ds18b20.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ds18b20.c
  3. *
  4. * Copyright 2022 dh33ex <dh33ex@riseup.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. */
  23. #ifndef __msp430_h_
  24. #include <msp430.h>
  25. #endif
  26. #include "ds18b20.h"
  27. void reset_18B20(unsigned char pin) {
  28. P1DIR |= pin;
  29. P1OUT &= ~pin;
  30. __delay_cycles(500);
  31. P1OUT |= pin;
  32. P1DIR &= ~pin;
  33. __delay_cycles(500);
  34. }
  35. void send_18B20(unsigned char pin, char data) {
  36. char i;
  37. for (i = 8; i > 0; i--) {
  38. P1DIR |= pin;
  39. P1OUT &= ~pin;
  40. __delay_cycles(2);
  41. if (data & 0x01){
  42. P1OUT |= pin;
  43. }
  44. __delay_cycles(60);
  45. P1OUT |= pin;
  46. P1DIR &= ~pin;
  47. data >>= 1;
  48. }
  49. }
  50. unsigned int read_18B20(unsigned char pin) {
  51. unsigned int data=0;
  52. char i;
  53. /* read only first 2 bytes (temperature) */
  54. for (i = 16; i > 0; i--) {
  55. P1DIR |= pin;
  56. P1OUT &= ~pin;
  57. __delay_cycles(2);
  58. P1OUT |= pin;
  59. P1DIR &= ~pin;
  60. __delay_cycles(8);
  61. if (P1IN & pin) {
  62. data |= 0x8000;
  63. }
  64. data >>= 1;
  65. __delay_cycles(120);
  66. }
  67. return(data);
  68. }
  69. void set_resolution(unsigned char pin, unsigned char resolution) {
  70. unsigned char package;
  71. switch (resolution) {
  72. case 9:
  73. package = 0b00011111;
  74. break;
  75. case 10:
  76. package = 0b00111111;
  77. break;
  78. case 11:
  79. package = 0b01011111;
  80. break;
  81. case 12:
  82. package = 0b01111111;
  83. break;
  84. default:
  85. package = 0b00011111; /* wrong resolution argument, set default */
  86. break;
  87. }
  88. reset_18B20(pin);
  89. send_18B20(pin, 0xcc); /* send skip ROM command */
  90. send_18B20(pin, 0x4e); /* send write scratchpad command */
  91. send_18B20(pin, 0x00); /* send TH byte (unused) */
  92. send_18B20(pin, 0x00); /* send TL byte (unused) */
  93. send_18B20(pin, package); /* send configure byte */
  94. reset_18B20(pin);
  95. __delay_cycles(120);
  96. }
  97. float get_temp(unsigned char pin) {
  98. unsigned int temp;
  99. reset_18B20(pin);
  100. send_18B20(pin, 0xcc); /* send skip ROM command */
  101. send_18B20(pin, 0x44); /* send convert temperature command */
  102. __delay_cycles(1000000);
  103. reset_18B20(pin);
  104. send_18B20(pin, 0xcc); /* send skip ROM command */
  105. send_18B20(pin, 0xbe); /* send read scratchpad command */
  106. temp = read_18B20(pin); /* read raw data */
  107. return((float) temp / 8.0); /* calculate temperature */
  108. }