main.c


1
#include "main.h"
2
3
static const PROGMEM unsigned char copyRightChar[] =
4
{
5
  0x07, 0x08, 0x13, 0x14, 0x14, 0x13, 0x08, 0x07,
6
  0x00, 0x10, 0x08, 0x08, 0x08, 0x08, 0x10, 0x00
7
};
8
9
static const PROGMEM unsigned char tempsign[] =
10
{
11
  0x06, 0x09, 0x09 ,0x06, 0x00, 0x00, 0x00, 0x00
12
};
13
14
static const PROGMEM unsigned char templow[] =
15
{
16
  0x06, 0x09, 0x09 ,0x06, 0x00, 0x04, 0x0E, 0x3F
17
};
18
19
static const PROGMEM unsigned char temphigh[] =
20
{
21
  0x06, 0x09, 0x09 ,0x06, 0x00, 0x3F, 0x0E, 0x04
22
};
23
24
void wait_until_key_pressed(void);
25
void displaytemp(void);
26
void displaystatus(int status);
27
void disptemp(int sensor);
28
void lcd_licht(int bel);
29
#define MAXSENSORS 5
30
char buffer[7];
31
char sign;
32
int  num=134;
33
unsigned char temp1, temp2;
34
unsigned int a, b, c, l1, l2, car, entl;
35
unsigned char ret;
36
uint8_t nSensors, i, stat;
37
uint8_t subzero, cel, cel_frac_bits;
38
uint16_t decicelsius, tempalt;
39
40
inline uint8_t debounce(volatile uint8_t *port, uint8_t pin)
41
{
42
        if ( ! (*port & (1 << pin)) )
43
        {
44
                delay_ms(100);
45
                if ( ! (*port & (1 << pin)) )
46
                {
47
                        delay_ms(100);
48
                        return 1;
49
                }
50
        }
51
        return 0;
52
}
53
54
55
56
SIGNAL(SIG_INTERRUPT1)
57
{
58
  cli();
59
  delay_ms(200);
60
  if (entl == 0){
61
    displaystatus(2);
62
    entl=1;
63
    PORTC |= (1<<PC5);
64
  }
65
  else if (entl == 1)
66
  {
67
    if (car >= 2 ){
68
      displaystatus(3);
69
      PORTC |= (1<<PC5);
70
    }else{
71
      displaystatus(1);
72
      cbi(PORTC,PC5);
73
    }
74
    entl=0;
75
  }
76
}
77
78
SIGNAL(L1_INT)
79
{
80
  lcd_gotoxy(0,0);
81
  lcd_puts("Licht 1 ");
82
  cli();
83
  if (debounce(&PIND, PD3)){
84
    b++;
85
  }
86
  loop_until_bit_is_set(PIND,PIND3);
87
  if (b > 0){
88
    car++;
89
    b=0;
90
    itoa(car,buffer,10);
91
    lcd_puts(buffer);
92
  }
93
//  if (entl == 0){
94
    if (car >= 2){
95
      PORTC |= (1<<PC5);
96
      displaystatus(3);
97
    }
98
//  }
99
}
100
101
SIGNAL(L2_INT)
102
{
103
  lcd_gotoxy(0,0);
104
  lcd_puts("Licht 2 ");
105
  cli();
106
  if (debounce(&PIND, PD2)){
107
    c++;
108
  }loop_until_bit_is_set(PIND,PIND2);
109
  if (c > 0){
110
    car--;
111
    c=0;
112
    itoa(car,buffer,10);
113
    lcd_puts(buffer);
114
  }
115
//  if (entl == 0){
116
    if (car < 2){ // fehlerpunkt ?
117
      cbi(PORTC,PC5);
118
      displaystatus(1);
119
    }
120
//  }
121
}
122
123
uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
124
125
uint8_t search_sensors(void)
126
{
127
  uint8_t i;
128
  uint8_t id[OW_ROMCODE_SIZE];
129
  uint8_t diff, nSensors;
130
131
  nSensors = 0;
132
133
  for( diff = OW_SEARCH_FIRST;
134
    diff != OW_LAST_DEVICE && nSensors < MAXSENSORS ; )
135
  {
136
    DS18X20_find_sensor( &diff, &id[0] );
137
138
    if( diff == OW_PRESENCE_ERR ) {
139
      lcd_clrscr();
140
      lcd_puts( "No Sensor found" );
141
      break;
142
    }
143
144
    if( diff == OW_DATA_ERR ) {
145
      lcd_clrscr();
146
      lcd_puts( "Bus Error\r" );
147
      break;
148
    }
149
150
    for (i=0;i<OW_ROMCODE_SIZE;i++)
151
      gSensorIDs[nSensors][i]=id[i];
152
153
    nSensors++;
154
  }
155
156
  return nSensors;
157
}
158
159
int main(void)
160
{
161
  PORTD |= ( 1 << PD1 );
162
    PORTD |= ( 1 << PD2 );
163
  PORTD |= ( 1 << PD3 );
164
  outp(0x02,MCUCR);
165
  outp(0x0E,EIMSK);
166
167
  #ifndef OW_ONE_BUS
168
  ow_set_bus(&PIND,&PORTD,&DDRD,PD0);
169
  #endif
170
171
    lcd_init(LCD_DISP_ON);
172
173
  nSensors = search_sensors();
174
175
  lcd_licht(1);
176
177
  lcd_command(_BV(LCD_CGRAM));
178
  for(i=0; i<16; i++)
179
  {
180
    lcd_data(pgm_read_byte_near(&copyRightChar[i]));
181
  }
182
  for(i=0; i<8; i++)
183
  {
184
    lcd_data(pgm_read_byte_near(&tempsign[i]));
185
  }
186
  for(i=0; i<8; i++)
187
  {
188
    lcd_data(pgm_read_byte_near(&temphigh[i]));
189
  }
190
  for(i=0; i<8; i++)
191
  {
192
    lcd_data(pgm_read_byte_near(&templow[i]));
193
  }
194
195
196
  lcd_clrscr();
197
    lcd_puts("Ampelsteuerung v0.1\n");
198
  lcd_gotoxy(0,1);
199
  lcd_putc(0);
200
  lcd_putc(1);
201
  lcd_puts(" 2005 M.Iller ");
202
  delay_ms(1000);
203
  lcd_gotoxy(0,1);
204
  itoa( nSensors , buffer, 10);
205
  lcd_puts(buffer);
206
  lcd_puts(" ds18s20 found");
207
  delay_ms(1000);
208
  lcd_clrscr();
209
210
  disptemp(2);
211
  disptemp(1);
212
  delay_ms(500);
213
  stat=2;
214
  car=0;
215
216
  sei();
217
218
  a=0;
219
  b=0;
220
  c=10;
221
  entl=0;
222
223
  lcd_gotoxy(11,1);
224
  lcd_puts("Ampelsteuerung v0.1");
225
226
  for(;;) {
227
    tempalt=decicelsius;
228
229
    disptemp(1);
230
    disptemp(2);
231
    lcd_gotoxy(0,0);
232
    itoa(car,buffer,10);
233
    lcd_puts(buffer);
234
235
  }
236
237
}
238
239
void displaystatus(int status)
240
{
241
  if (status == 1){
242
    lcd_gotoxy(0,0);
243
    lcd_puts("                 Gr");
244
    lcd_putc(0xF5);
245
    lcd_puts("n                   ");
246
  }
247
  if (status == 2){
248
    lcd_gotoxy(0,0);
249
    lcd_puts("               Entladung                ");
250
  }
251
  if (status == 3){
252
    lcd_gotoxy(0,0);
253
    lcd_puts("                  ROT                   ");
254
  }
255
  if (status == 4){
256
    lcd_clrscr();
257
    lcd_puts("Einstellungen:");
258
    cli();
259
  }
260
}
261
262
void disptemp(int sensor)
263
{
264
// Routine für 2x40 LCD
265
  if ( DS18X20_start_meas( DS18X20_POWER_PARASITE,
266
    &gSensorIDs[sensor-1][0] ) == DS18X20_OK ) {
267
    delay_ms(DS18B20_TCONV_12BIT);
268
    if ( DS18X20_read_meas( &gSensorIDs[sensor-1][0], &subzero,
269
        &cel, &cel_frac_bits) == DS18X20_OK ) {
270
      if (sensor == 1)
271
        lcd_gotoxy(33,1);
272
      else
273
        lcd_gotoxy(0,1);
274
275
      itoa(subzero,buffer,10);
276
      if (subzero == 0) { sign="+"; }
277
      else {sign="-"; }
278
      lcd_puts(sign);
279
      decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
280
      itoa((decicelsius/10),buffer,10);
281
      if ((decicelsius/10) < 10){
282
        lcd_puts(" ");
283
      }
284
      lcd_puts(buffer);
285
      lcd_puts(".");
286
      itoa(( (decicelsius%10) + '0'),buffer,10);
287
      lcd_puts(buffer);
288
      if (tempalt < decicelsius){
289
        lcd_putc(4);
290
      }else if (tempalt > decicelsius){
291
        lcd_putc(3);
292
      }else if (tempalt == decicelsius){
293
        lcd_putc(2);
294
      }
295
    }
296
  }
297
// Routine für 2x16 LCD
298
/*  if ( DS18X20_start_meas( DS18X20_POWER_PARASITE,
299
    &gSensorIDs[sensor-1][0] ) == DS18X20_OK ) {
300
    delay_ms(DS18B20_TCONV_12BIT);
301
302
    if ( DS18X20_read_meas( &gSensorIDs[sensor-1][0], &subzero,
303
        &cel, &cel_frac_bits) == DS18X20_OK ) {
304
      if (sensor == 1)
305
        lcd_gotoxy(13,1); // Temp außen
306
      else
307
        lcd_gotoxy(0,1); // Temp innen
308
309
      itoa(subzero,buffer,10);
310
      if (subzero == 0) { sign="+"; }
311
      else {sign="-"; }
312
      lcd_puts(sign);
313
      decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
314
      itoa((decicelsius/10),buffer,10);
315
      if ((decicelsius/10) < 10){
316
        lcd_puts(" ");
317
      }
318
      lcd_puts(buffer);
319
      lcd_puts(".");
320
      itoa(( (decicelsius%10) + '0'),buffer,10);
321
      lcd_puts(buffer);
322
      lcd_putc(2);
323
    }
324
  }
325
*/
326
}
327
328
329
330
void lcd_licht(int bel)
331
{
332
  if (bel == 1)
333
    PORTC |= (1<<PC1);
334
  else
335
    cbi(PORTC, PC1);
336
}