1 | /* Thermometer Connections (At your choice) */
|
2 | #define THERM_PORT PORTD
|
3 | #define THERM_DDR DDRD
|
4 | #define THERM_PIN PIND
|
5 | #define THERM_DQ PD0
|
6 | /* Utils */
|
7 | #define THERM_INPUT_MODE() THERM_DDR&=~(1<<THERM_DQ)
|
8 | #define THERM_OUTPUT_MODE() THERM_DDR|=(1<<THERM_DQ)
|
9 | #define THERM_LOW() THERM_PORT&=~(1<<THERM_DQ)
|
10 | #define THERM_HIGH() THERM_PORT|=(1<<THERM_DQ)
|
11 |
|
12 | #define THERM_CMD_CONVERTTEMP 0x44
|
13 | #define THERM_CMD_RSCRATCHPAD 0xbe
|
14 | #define THERM_CMD_WSCRATCHPAD 0x4e
|
15 | #define THERM_CMD_CPYSCRATCHPAD 0x48
|
16 | #define THERM_CMD_RECEEPROM 0xb8
|
17 | #define THERM_CMD_RPWRSUPPLY 0xb4
|
18 | #define THERM_CMD_SEARCHROM 0xf0
|
19 | #define THERM_CMD_READROM 0x33
|
20 | #define THERM_CMD_MATCHROM 0x55
|
21 | #define THERM_CMD_SKIPROM 0xcc
|
22 | #define THERM_CMD_ALARMSEARCH 0xec
|
23 |
|
24 | #define THERM_DECIMAL_STEPS_12BIT 625 //.0625
|
25 | #define THERM_DECIMAL_STEPS_9BIT 50 //.5
|
26 |
|
27 |
|
28 |
|
29 | uint8_t therm_reset(){
|
30 |
|
31 | uint8_t i;
|
32 |
|
33 | //Pull line low and wait for 480uS
|
34 | THERM_LOW();
|
35 | THERM_OUTPUT_MODE();
|
36 | _delay_us(480);
|
37 |
|
38 | //Release line and wait for 60uS
|
39 | THERM_INPUT_MODE();
|
40 | _delay_us(60);
|
41 |
|
42 | //Store line value and wait until the completion of 480uS period
|
43 | i=(THERM_PIN & (1<<THERM_DQ));
|
44 | _delay_us(420);
|
45 |
|
46 | //Return the value read from the presence pulse (0=OK, 1=WRONG)
|
47 | return i;
|
48 | }
|
49 |
|
50 | void therm_write_bit(uint8_t bit){
|
51 |
|
52 | //Pull line low for 1uS
|
53 | THERM_LOW();
|
54 | THERM_OUTPUT_MODE();
|
55 | _delay_us(1);
|
56 |
|
57 | //If we want to write 1, release the line (if not will keep low)
|
58 | if(bit) THERM_INPUT_MODE();
|
59 |
|
60 | //Wait for 60uS and release the line
|
61 | _delay_us(60);
|
62 | THERM_INPUT_MODE();
|
63 | }
|
64 |
|
65 | uint8_t therm_read_bit(void){
|
66 |
|
67 | uint8_t bit=0;
|
68 |
|
69 | //Pull line low for 1uS
|
70 | THERM_LOW();
|
71 | THERM_OUTPUT_MODE();
|
72 | _delay_us(1);
|
73 |
|
74 | //Release line and wait for 14uS
|
75 | THERM_INPUT_MODE();
|
76 | _delay_us(14);
|
77 |
|
78 | //Read line value
|
79 | if(THERM_PIN&(1<<THERM_DQ)) bit=1;
|
80 |
|
81 | //Wait for 45uS to end and return read value
|
82 | _delay_us(45);
|
83 | return bit;
|
84 | }
|
85 |
|
86 | uint8_t therm_read_byte(void){
|
87 | uint8_t i=8, n=0;
|
88 | while(i--){
|
89 | //Shift one position right and store read value
|
90 | n>>=1;
|
91 | n|=(therm_read_bit()<<7);
|
92 | }
|
93 | return n;
|
94 | }
|
95 |
|
96 | void therm_write_byte(uint8_t byte){
|
97 |
|
98 | uint8_t i=8;
|
99 |
|
100 | while(i--){
|
101 | //Write actual bit and shift one position right to make the next bit ready
|
102 | therm_write_bit(byte&1);
|
103 | byte>>=1;
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 | void therm_read_temperature(char *buffer){
|
110 |
|
111 | // Buffer length must be at least 12bytes long! ["+XXX.XXXX C"]
|
112 | uint8_t temperature[2];
|
113 | int8_t digit;
|
114 | uint16_t decimal;
|
115 |
|
116 | //Reset, skip ROM and start temperature conversion
|
117 | therm_reset();
|
118 | therm_write_byte(THERM_CMD_SKIPROM);
|
119 | therm_write_byte(THERM_CMD_CONVERTTEMP);
|
120 |
|
121 |
|
122 | //Wait until conversion is complete
|
123 | while(!therm_read_bit());
|
124 |
|
125 | //Reset, skip ROM and send command to read Scratchpad
|
126 | therm_reset();
|
127 | therm_write_byte(THERM_CMD_SKIPROM);
|
128 | therm_write_byte(THERM_CMD_RSCRATCHPAD);
|
129 |
|
130 | //Read Scratchpad (only 2 first bytes)
|
131 | temperature[0]=therm_read_byte();
|
132 | temperature[1]=therm_read_byte();
|
133 | therm_reset();
|
134 |
|
135 | //Store temperature integer digits and decimal digits
|
136 | digit=temperature[0]>>4;
|
137 | digit|=(temperature[1]&0x7)<<2;
|
138 |
|
139 | //Store decimal digits
|
140 | decimal=temperature[0]&0xf;
|
141 | decimal*=THERM_DECIMAL_STEPS_12BIT;
|
142 |
|
143 | //Format temperature into a string [+XXX.XXXX C]
|
144 | sprintf(buffer, "%2d.%04uC", digit, decimal);
|
145 |
|
146 | lcd_txt(1, 1, buffer);
|
147 | }
|