TempDisplay.c


1
/*
2
 * TempDisplay.c
3
 *
4
 * Created: 19.01.2012 11:56:25
5
 *  Author: Serpent
6
 *  PORT D5 = DQ
7
 */ 
8
9
#define LCD_DATA_D DDRB
10
#define LCD_CMD_D DDRD
11
12
#define LCD_DATA PORTB
13
#define LCD_CMD PORTD
14
15
#define EN (1<<PD1) 
16
#define RW (1<<PD6)
17
#define RS (1<<PD0)
18
#define CSB (1<<PD1)
19
#define DQ (1<<PD5)
20
21
#define F_CPU 16000000UL
22
23
#include <avr/io.h>
24
#include <util/delay.h>
25
#include <avr/interrupt.h>
26
27
//Global variable temp
28
int temp;
29
30
/************************************************************************/
31
/* Bit-field
32
/* Consists out of struct + union                                                                   
33
/************************************************************************/
34
35
struct charBF{
36
  unsigned char  B0:1,
37
          B1:1,
38
          B2:1,
39
          B3:1,
40
          B4:1,
41
          B5:1,
42
          B6:1,
43
          B7:1;
44
45
};
46
47
typedef union {
48
  struct charBF b;
49
  unsigned char complete;
50
} Pchar;
51
52
/************************************************************************/
53
/* resetSensor
54
/* Function: Resets the Sensor DS18S20                                                                 
55
/************************************************************************/
56
int resetSensor(){
57
  int num;
58
59
  PORTD &=~ DQ;
60
  DDRD |= DQ; 
61
  _delay_us(480); //550
62
  DDRD &=~ DQ;
63
  _delay_us(66); //70
64
  num = PIND & DQ;
65
  _delay_us(480-66);
66
  if((PIND & DQ) == 0){
67
    num = 1;
68
  }
69
  
70
  return num;
71
  /*if((PIND & (1<<PIND5)) == 0){
72
     num =  1;
73
  }else
74
     num =  0;
75
  _delay_us(550); //550
76
  return num;*/
77
}
78
79
/************************************************************************/
80
/* writeSensor
81
/* Function: Writes commands to the DS18S20                                                                  
82
/************************************************************************/
83
84
void writeSensor(int i){
85
if(i == 1){
86
87
88
  DDRD |= DQ;
89
  PORTD &=~ DQ;  
90
    _delay_us(10); //10//1
91
    DDRD &=~ DQ;
92
    _delay_us(65); //65
93
  }else if(i == 0){
94
95
  
96
  DDRD |= DQ;
97
  PORTD &=~ DQ;
98
    _delay_us(90); //90 //65
99
    DDRD &=~ DQ;
100
    _delay_us(5); //5 //1
101
  }
102
}
103
104
/************************************************************************/
105
/* readSensor
106
/* Function: Returns one out of 8 bit for the variable temp + sign                                                               
107
/************************************************************************/
108
109
int readSensor(){
110
  int i;
111
112
  DDRD |= DQ;//TRISB.B4 = 0;
113
  PORTD &=~ DQ;
114
  _delay_us(1);//Delay_us(1); //1//2
115
  DDRD &=~ DQ;//TRISB.B4 = 1; //PortB.0-3 output, PortB.4-7 input
116
  _delay_us(1);//Delay_us(1); //1//5
117
  i = (PIND & DQ);//i = PORTB.B4;
118
  _delay_us(70);//Delay_us(70); //70
119
  return i;
120
}
121
122
/************************************************************************/
123
/* getTemp
124
/* Function: Gets 9 bits from the DS18S20 and interprets the 
125
/*           bits. Result is saved in the variable temp and returns temp.                                                              
126
/************************************************************************/
127
128
int getTemp() {
129
  int temp;
130
  Pchar templsb;
131
  Pchar tempmsb;
132
  
133
  DDRD &=~ DQ;
134
    _delay_ms(5);
135
  
136
    resetSensor();
137
    
138
  writeChar(0xCC); // Skip ROM (works only with one DS18S20)
139
    writeChar(0x44); // Begin Temperatur-Conversation
140
  
141
142
    while(!readSensor())
143
    {
144
    }
145
146
   _delay_ms(1);
147
148
   resetSensor();
149
    
150
   writeChar(0xCC); // Skip ROM (works only with one DS18S20)
151
   writeChar(0xBE); // Begin Temperatur-Conversation
152
   
153
   _delay_ms(750);
154
     
155
   templsb.b.B0 = readSensor(); //Data-Bit 0
156
   templsb.b.B1 = readSensor(); //Data-Bit 1
157
   templsb.b.B2 = readSensor(); //Data-Bit 2
158
   templsb.b.B3 = readSensor(); //Data-Bit 3
159
   templsb.b.B4 = readSensor(); //Data-Bit 4
160
   templsb.b.B5 = readSensor(); //Data-Bit 5
161
   templsb.b.B6 = readSensor(); //Data-Bit 6
162
   templsb.b.B7 = readSensor(); //Data-Bit 7
163
   tempmsb.b.B0 = readSensor(); //sign
164
   
165
166
    
167
       temp = 0;
168
       if(templsb.b.B0)
169
          temp += 5;
170
       if(templsb.b.B1)
171
          temp += 10;
172
       if(templsb.b.B2)
173
          temp += 20;
174
       if(templsb.b.B3)
175
          temp += 40;
176
       if(templsb.b.B4)
177
          temp += 80;
178
       if(templsb.b.B5)
179
          temp += 160;
180
       if(templsb.b.B6)
181
          temp += 320;
182
       if(templsb.b.B7)
183
          temp += 640;
184
185
       if(tempmsb.b.B0)
186
          return -temp;
187
       return temp;
188
     
189
   
190
}
191
192
/************************************************************************/
193
/* writeChar
194
/* Function: Bitmask. Interprets a Hexadecimal-Code and commit it
195
/*           bit by bit.                                                              
196
/************************************************************************/
197
void writeChar(char c){
198
199
   writeSensor((c>>0) & 0x01);
200
   writeSensor((c>>1) & 0x01);
201
   writeSensor((c>>2) & 0x01);
202
   writeSensor((c>>3) & 0x01);
203
   writeSensor((c>>4) & 0x01);
204
   writeSensor((c>>5) & 0x01);
205
   writeSensor((c>>6) & 0x01);
206
   writeSensor((c>>7) & 0x01);
207
}
208
209
210
211
/************************************************************************/
212
/* Display-Control:
213
/* Port B0-B7 Data (orange)
214
/* Port D7 Reset (white)
215
/* PORT D6 /RW (green)
216
/* PORT D5 E (blue)                                                                    
217
/************************************************************************/
218
219
220
221
void wait_ms(uint16_t ms){
222
  uint16_t i;
223
  for (i=0;i<ms;i++)
224
  {
225
    _delay_ms(1);
226
  }  
227
}  
228
229
void wait_us(uint16_t us){
230
  uint16_t i;
231
  for (i=0;i<us;i++)
232
  {
233
    _delay_us(1);
234
  }  
235
}  
236
237
void Lcd_set(){
238
  LCD_CMD |= EN;
239
  wait_ms(1);
240
  LCD_CMD &= ~EN;
241
  wait_ms(1);
242
}
243
244
void Lcd_WriteData(uint8_t dta){
245
  LCD_CMD |= RS;
246
  LCD_DATA = dta;
247
  Lcd_set();
248
}
249
250
void Lcd_WriteStr(char* str){
251
  while (*str != 0){
252
    Lcd_WriteData(*str++);
253
  }
254
}
255
256
void Lcd_WriteStrDelay(char* str, uint16_t ms){
257
  while (*str != 0){
258
    Lcd_WriteData(*str++);
259
    wait_ms(ms);
260
  }
261
}
262
263
void Lcd_WriteCmd(uint8_t cmd){
264
  LCD_CMD &= ~RS;
265
  LCD_DATA = cmd;
266
  Lcd_set();
267
}
268
269
void Lcd_WriteDDRAM(uint8_t adr){
270
  LCD_CMD &= ~RS;
271
  LCD_DATA = adr + 0x80;
272
  Lcd_set();
273
}
274
275
void Lcd_WriteCGRAM(uint8_t adr){
276
  LCD_CMD &= ~RS;
277
  LCD_DATA = adr + 0x40;
278
  Lcd_set();
279
}
280
281
void Lcd_WriteCG(char *sign, uint8_t plc){
282
  switch(plc){
283
    case 0: Lcd_WriteCGRAM(0); break;
284
    case 1: Lcd_WriteCGRAM(8); break;
285
    case 2: Lcd_WriteCGRAM(16); break;
286
    case 3: Lcd_WriteCGRAM(24); break;
287
    case 4: Lcd_WriteCGRAM(32); break;
288
    case 5: Lcd_WriteCGRAM(40); break;
289
    case 6: Lcd_WriteCGRAM(48); break;
290
    case 7: Lcd_WriteCGRAM(56); break;
291
  }
292
  while (*sign != 0){
293
    if(*sign==255){Lcd_WriteData(0);}
294
    else{Lcd_WriteData(*sign);};
295
    sign++;
296
  }
297
}
298
299
void Lcd_Clear(){
300
  Lcd_WriteCmd(1);
301
}
302
303
void Lcd_WriteStrMLB(char* str, char bsa, uint8_t offs, uint16_t speed){
304
  int c;
305
  for(c = 0; c <= offs; c++){
306
    Lcd_WriteDDRAM(bsa);
307
    Lcd_WriteStr(str + c);
308
    wait_ms(speed);
309
  }
310
  for(c = offs; c >= 0; c--){
311
    Lcd_WriteDDRAM(bsa);
312
    Lcd_WriteStr(str + c);
313
    wait_ms(speed);
314
  }
315
}
316
317
318
void Lcd_WriteStrTemp(int val){
319
  char tmp[7];
320
  
321
  sprintf(tmp, "%d", val);
322
  
323
  Lcd_WriteStr(tmp);
324
}
325
326
327
void _Lcd_Init(){
328
  wait_ms(60);
329
  Lcd_WriteCmd(0b00111001);
330
  Lcd_WriteCmd(0b00011100);
331
  Lcd_WriteCmd(0b01010011);
332
  Lcd_WriteCmd(0b01101001);
333
  Lcd_WriteCmd(0b01110100);
334
  Lcd_WriteCmd(0b00111000);
335
  Lcd_WriteCmd(0b00001100);
336
  Lcd_Clear();
337
  Lcd_WriteCmd(0b00000110);
338
}
339
340
/************************************************************************/
341
/* MAIN
342
/************************************************************************/
343
344
int main(void)
345
{
346
  char pfl[] = {4, 14, 31, 255, 31, 14, 4, 255, 0};
347
  char sml[] = {255, 17, 4, 4, 17, 17, 14, 255, 0};
348
  char *lineo = "Projekt:";
349
  char *linet = "Wetterstation ";
350
  char *str = "Hadouken - Oxygen (Gemini Remix) ";
351
  
352
  LCD_DATA_D = 0xFF;
353
  LCD_CMD_D = 0xFF;
354
  
355
  LCD_DATA = 0x00;
356
  LCD_CMD = 0x00;
357
  
358
  _Lcd_Init();
359
  Lcd_WriteCG(sml, 0);
360
361
  Lcd_Clear();
362
  Lcd_WriteDDRAM(0);
363
  Lcd_WriteStrDelay(lineo, 100);
364
365
  Lcd_WriteDDRAM(64);
366
  Lcd_WriteStrDelay(linet, 100);
367
  
368
  Lcd_WriteData(0);
369
  
370
  wait_ms(1000);
371
  
372
  Lcd_Clear();
373
  Lcd_WriteDDRAM(0);  
374
  Lcd_WriteStr("Temperatur:");
375
  
376
  temp = getTemp();
377
  Lcd_WriteStrTemp(getTemp());
378
  while(1){
379
  
380
    //Lcd_WriteStrMLB(str,64,16, 1300);
381
      _delay_ms(1000);
382
      Lcd_Clear();
383
      Lcd_WriteDDRAM(0);  
384
      Lcd_WriteStr("Temperatur:");
385
    Lcd_WriteStrTemp(getTemp());             
386
387
    
388
  }
389
390
}