1 | // Display an PORTC
|
2 | // MAX6675 an PORTB
|
3 | // Taster an PORTD
|
4 | // Output Relais an PORTD
|
5 |
|
6 |
|
7 | #include <stdint.h>
|
8 | #include <stdlib.h>
|
9 | #include <string.h>
|
10 | #include <avr/io.h>
|
11 | #include <util/delay.h>
|
12 | #include "lcd-routines.h"
|
13 |
|
14 | extern void lcd_init(void);
|
15 | extern void lcd_clear(void);
|
16 | extern void set_cursor(uint8_t x, uint8_t y);
|
17 | extern void lcd_string(char *data);
|
18 |
|
19 | /* Defines MAX !!*/
|
20 |
|
21 | #define MAX_DDR DDRB
|
22 | #define MAX_PORT PORTB
|
23 | #define MAX_PIN PINB
|
24 |
|
25 | #define CS PB0
|
26 | #define SCK PB1
|
27 | #define SO PB2
|
28 | // ------------------------- //
|
29 | /* Defines Menütasten */
|
30 |
|
31 | #define MENU_DDR DDRD
|
32 | #define MENU_PIN PIND
|
33 | #define MENU_PORT PORTD
|
34 |
|
35 | #define MENU_SET PD2
|
36 | #define MENU_UP PD0
|
37 | #define MENU_DOWN PD1
|
38 | // ------------------------- //
|
39 | /* Defines Relais */
|
40 | #define REL_OUT_DDR DDRD
|
41 | #define REL_OUT_PORT PORTD
|
42 | #define REL_OUT PD7
|
43 | // ------------------------- //
|
44 |
|
45 | // ---------- Temp. holen -----------
|
46 | uint16_t gettemp(void){
|
47 | uint8_t bit = 0, bitnr = 12;
|
48 | uint8_t foo1 = 0;
|
49 | uint16_t Rohdata = 0;
|
50 |
|
51 |
|
52 | MAX_PORT &= ~(1 << CS);
|
53 |
|
54 | for(foo1 = 0 ; foo1 < 16 ; foo1++){
|
55 |
|
56 | bit = 15 - foo1;
|
57 |
|
58 |
|
59 | MAX_PORT |= (1 << SCK);
|
60 |
|
61 |
|
62 | if((bit <= 14) && (bit >= 3)){
|
63 |
|
64 | if((MAX_PIN & (1 << SO))){
|
65 | bitnr--;
|
66 | Rohdata |= (1 << bitnr);
|
67 | }else{
|
68 | bitnr--;
|
69 | Rohdata &= ~(1 << bitnr);
|
70 | }
|
71 |
|
72 | }else{
|
73 | bitnr = 12;
|
74 | }
|
75 |
|
76 | MAX_PORT &= ~(1 << SCK);
|
77 |
|
78 | }
|
79 |
|
80 |
|
81 | MAX_PORT |= (1 << CS);
|
82 |
|
83 |
|
84 | return Rohdata; //gibt die 12 relevanten Bits zurück
|
85 | }
|
86 |
|
87 | // ----------- Long Delay -------------------
|
88 | void long_delay(uint16_t ms) {
|
89 | for(; ms>0; ms--) _delay_ms(1);
|
90 | //while(ms--) _delay_ms(1);
|
91 | }
|
92 | // ------------------------------------------
|
93 |
|
94 | //------------ Taster entprellen ------------
|
95 |
|
96 | inline uint8_t debounce(volatile uint8_t *port, uint8_t pin)
|
97 | {
|
98 | if ( ! (*port & (1 << pin)) )
|
99 | {
|
100 | /* Pin wurde auf Masse gezogen, 100ms warten */
|
101 | long_delay(110); // max. 262.1 ms / F_CPU in MHz
|
102 | if ( *port & (1 << pin) )
|
103 | {
|
104 | /* Anwender Zeit zum Loslassen des Tasters geben */
|
105 | long_delay(50);
|
106 | return 1;
|
107 | }
|
108 | }
|
109 | return 0;
|
110 | }
|
111 | //-------------------------------------------
|
112 |
|
113 |
|
114 | void initavr(void){ //AVR initialisieren
|
115 |
|
116 | MAX_DDR &= ~(1 << SO);
|
117 | MAX_DDR |= ((1 << CS) | (1 << SCK));
|
118 |
|
119 | MAX_PORT |= (1 << CS);
|
120 | MAX_PORT &= ~(1 << SCK);
|
121 |
|
122 | }
|
123 |
|
124 |
|
125 | int main(void)
|
126 | {
|
127 | char MenuVal;
|
128 | char intMaxTemp;
|
129 | int intMaxTempCompare;
|
130 | char strMaxTemp[3];
|
131 |
|
132 | char intOffset;
|
133 | char intOffsetCompare;
|
134 | char strOffset[3];
|
135 |
|
136 | initavr();
|
137 | lcd_init();
|
138 | MENU_DDR &=~ (1<<MENU_SET) | (1<<MENU_UP) | (1<<MENU_DOWN);
|
139 | MENU_PORT |= (1<<MENU_SET) | (1<<MENU_UP) | (1<<MENU_DOWN);
|
140 |
|
141 | REL_OUT_DDR |= (1<<REL_OUT); // Ausgang für Relais
|
142 | REL_OUT_PORT |= (1<<REL_OUT);
|
143 |
|
144 | MenuVal = 0;
|
145 | intMaxTemp = 187;
|
146 | intOffset = 0;
|
147 |
|
148 | while(1){
|
149 | char temp[7];
|
150 | double temperature;
|
151 |
|
152 |
|
153 | if (debounce(&MENU_PIN, MENU_SET)==1)
|
154 | {
|
155 | MenuVal++;
|
156 | }
|
157 |
|
158 | if (MenuVal == 3) // beim letzten Menü auf Temp.Anzeige wechseln
|
159 | {
|
160 | MenuVal=0;
|
161 | }
|
162 |
|
163 |
|
164 | switch (MenuVal){
|
165 | case 0:
|
166 | temperature = gettemp();
|
167 |
|
168 | intMaxTempCompare = intMaxTemp * 4;
|
169 | intOffsetCompare = intOffset * 4;
|
170 | temperature = temperature + intOffsetCompare;
|
171 |
|
172 | if (temperature < intMaxTempCompare)
|
173 | {
|
174 | temperature = (temperature / 4);
|
175 | dtostrf(temperature, 2, 2, temp);
|
176 | long_delay(200);
|
177 | lcd_clear();
|
178 | lcd_string(temp);
|
179 | set_cursor(0,2);
|
180 | lcd_string("Heizung an!");
|
181 | REL_OUT_PORT &=~ (1<<REL_OUT);
|
182 | }
|
183 |
|
184 | if (temperature >= intMaxTempCompare)
|
185 | {
|
186 | temperature = (temperature / 4);
|
187 | dtostrf(temperature, 2, 2, temp);
|
188 | long_delay(200);
|
189 | lcd_clear();
|
190 | lcd_string(temp);
|
191 | set_cursor(0,2);
|
192 | lcd_string("Heizung aus!");
|
193 | REL_OUT_PORT |= (1<<REL_OUT);
|
194 | }break;
|
195 |
|
196 | case 1:
|
197 | lcd_clear();
|
198 | set_cursor(0,1);
|
199 | lcd_string("Set max. Temp.");
|
200 | set_cursor(0,2);
|
201 | if (debounce (&MENU_PIN, MENU_UP)==1)
|
202 | {
|
203 | intMaxTemp++;
|
204 | }
|
205 |
|
206 | if (debounce(&MENU_PIN, MENU_DOWN)==1)
|
207 | {
|
208 | intMaxTemp--;
|
209 | }
|
210 | itoa(intMaxTemp, strMaxTemp, 10);
|
211 |
|
212 | lcd_string(strMaxTemp);
|
213 | long_delay(23);
|
214 | break;
|
215 |
|
216 | case 2:
|
217 | lcd_clear();
|
218 | set_cursor(0,1);
|
219 | lcd_string("Set Offset Temp.");
|
220 | set_cursor(0,2);
|
221 | if (debounce(&MENU_PIN, MENU_UP)==1)
|
222 | {
|
223 | intOffset++;
|
224 | }
|
225 |
|
226 | if (debounce(&MENU_PIN, MENU_DOWN)==1)
|
227 | {
|
228 | intOffset--;
|
229 | }
|
230 | itoa(intOffset, strOffset, 10);
|
231 | lcd_string(strOffset);
|
232 | long_delay(23);
|
233 | break;}
|
234 |
|
235 |
|
236 | }
|
237 | }
|