1 | #include "ds18b20.h"
|
2 | #include <util/delay.h>
|
3 |
|
4 | uint8_t therm_reset(uint8_t pin)
|
5 | {
|
6 | uint8_t i;
|
7 | //Pull line low and wait for 480uS
|
8 | THERM_LOW(pin);
|
9 | THERM_OUTPUT_MODE(pin);
|
10 | _delay_us(430); //480 //this must be smaller when moving delay func to other .c file
|
11 | //Release line and wait for 60uS
|
12 | THERM_INPUT_MODE(pin);
|
13 | _delay_us(60); //60
|
14 | //Store line value and wait until the completion of 480uS period
|
15 | i=(THERM_PIN & (1<<pin));
|
16 | _delay_us(420); //420
|
17 | //Return the value read from the presence pulse (0=OK, 1=WRONG)
|
18 | return i;
|
19 | }
|
20 |
|
21 | void therm_write_bit(uint8_t bit, uint8_t pin)
|
22 | {
|
23 | //Pull line low for 1uS
|
24 | THERM_LOW(pin);
|
25 | THERM_OUTPUT_MODE(pin);
|
26 | _delay_us(1);//1
|
27 | //If we want to write 1, release the line (if not will keep low)
|
28 | if(bit) THERM_INPUT_MODE(pin);
|
29 | //Wait for 60uS and release the line
|
30 | _delay_us(50);//60
|
31 | THERM_INPUT_MODE(pin);
|
32 | }
|
33 |
|
34 | uint8_t therm_read_bit(uint8_t pin)
|
35 | {
|
36 | uint8_t bit=0;
|
37 | //Pull line low for 1uS
|
38 | THERM_LOW(pin);
|
39 | THERM_OUTPUT_MODE(pin);
|
40 | _delay_us(3);//1
|
41 | //Release line and wait for 14uS
|
42 | THERM_INPUT_MODE(pin);
|
43 | _delay_us(10);//14
|
44 | //Read line value
|
45 | if(THERM_PIN&(1<<pin)) bit=1;
|
46 | //Wait for 45uS to end and return read value
|
47 | _delay_us(53);//45
|
48 | return bit;
|
49 | }
|
50 |
|
51 | uint8_t therm_read_byte(uint8_t pin)
|
52 | {
|
53 | uint8_t i=8, n=0;
|
54 | while(i--) {
|
55 | //Shift one position right and store read value
|
56 | n>>=1;
|
57 | n|=(therm_read_bit(pin)<<7);
|
58 | }
|
59 | return n;
|
60 | }
|
61 |
|
62 | uint16_t therm_read_word(uint8_t pin)
|
63 | {
|
64 | uint16_t i=16, n=0;
|
65 | while(i--) {
|
66 | //Shift one position right and store read value
|
67 | n>>=1;
|
68 | n|=(therm_read_bit(pin)<<15);
|
69 | }
|
70 | return n;
|
71 | }
|
72 |
|
73 | void therm_write_byte(uint8_t byte, uint8_t pin)
|
74 | {
|
75 | uint8_t i=8;
|
76 | while(i--) {
|
77 | //Write actual bit and shift one position right to make the next bit ready
|
78 | therm_write_bit(byte&1, pin);
|
79 | byte>>=1;
|
80 | }
|
81 | }
|
82 |
|
83 | float therm_read_temperature(uint8_t pin)
|
84 | {
|
85 | //Reset, skip ROM and start temperature conversion
|
86 | uint8_t re = therm_reset(pin);
|
87 | if(re >= 4) {
|
88 | return 1000.0;
|
89 | }
|
90 | therm_write_byte(THERM_CMD_SKIPROM, pin);
|
91 | therm_write_byte(THERM_CMD_CONVERTTEMP, pin);
|
92 | //Wait until conversion is complete
|
93 | while(!therm_read_bit(pin));
|
94 | //Reset, skip ROM and send command to read Scratchpad
|
95 | therm_reset(pin);
|
96 | therm_write_byte(THERM_CMD_SKIPROM, pin);
|
97 | therm_write_byte(THERM_CMD_RSCRATCHPAD, pin);
|
98 | uint8_t l = therm_read_byte(pin);
|
99 | uint8_t h = therm_read_byte(pin);
|
100 | therm_reset(pin);
|
101 | float temp = ( (h << 8) + l )*0.0625;
|
102 | return temp;
|
103 | }
|