-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
383 lines (315 loc) · 7.71 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//SunAVR
//
// ATmega8 @ 16 MHz (Arduino NG)
//
// A dawn simulator
//
// Originaly inspired from
// https://sites.google.com/site/qeewiki/books/avr-guide/pwm-atmega8
//
//Possible improvements:
//Bugs:
//
//-----
//Done
//
//Solved bugs:
//Done improvements
#define Version "0.2.0" //firmware version
// this code sets up counter1 for an 4kHz, 10bit, Phase Corrected PWM
// @ 16Mhz Clock
#include <avr/io.h>
#include <util/delay.h>
#include "utility.h" // macro for simplification of pin manipulations
#include "i2cmaster.h" // for the ds1307 clock
#include "clock.h" // for the ds1307 clock
#include "lcd.h" // for LCD
#define orderExponential 15 //nomber of terms to calculate en exponential (-1)
#define maxXexponential 3 // max x-axis value for calculation of en exponential
// About 2 ms of calculations per step (orderExponential=15).
// 5 means there is a ratio of ~1:100 between the minimum value and the maximum value
// 7 means there is a ratio of 1:1 000 between the minimum value and the maximum value
// 9 means there is a ratio of ~1:10 000 between the minimum value and the maximum value
#define totalTime 30 // total time of a complete sequence (minutes)
MAKE_OUTPUT(LIGHT, B, 1, 1) // PWM (actual light)
MAKE_INPUT(SELECT, C, 3, 1) // select button
MAKE_INPUT(ENTER, C, 2, 1) // enter button
#define delay_blink 250
// Current time
TimeVal curTime;
// Alarm time
TimeVal alarmTime1;
TimeVal alarmTime2;
char bufferLCD[6];
// Get the seconds value at the last iteration
uint8_t lastSec;
unsigned short mode=0;
unsigned short key;
// Calculate OCR1A value given a duty cycle (percent)
int calculateOCR1Apercent(float intensity)
{
return (int)(intensity*1023);
}
void setIO(void)
{
DDRB |= (1 << DDB1);
// PB1 is now an output
TCCR1A |= (1 << COM1A1);
// set none-inverting mode
TCCR1A |= (1 << WGM11) | (1 << WGM10);
// set 10bit phase corrected PWM Mode
TCCR1B |= (1 << CS11);
// set prescaler to 8 and starts PWM
INIT_SELECT();
INIT_ENTER();
//PULLUP_SELECT();
//PULLUP_ENTER();
PORTC |= 1 << PC2 | 1 << PC3; //temporary replacement for PULLUP_...
}
float expo(float x)
// calculate an approximation of the exponential of x
{
float out = 1; // order 0
int nfact = 1; //factorial of n == n!
float powx = 1; //x to the power of n == x^n
for(int i=1; i<orderExponential; i++)
{
nfact *= i;
powx *= x;
out += powx/nfact; // order i
}
return out;
}
void blinkLIGHT(int n)
{
for (int j=0; j<n; j++)
{
// now turn ON the LIGHT
LIGHT(1);
_delay_ms(delay_blink); // wait
// now turn off the LIGHT
LIGHT(0);
_delay_ms(delay_blink); // wait
}
}
int waitAlarm(TimeVal* ptr_alarmTime)
//return 1 if alarmTime = curTime (we have to wait)
// 0 if alarmTime != curTime
{
return cmpTimeHM(&curTime, ptr_alarmTime) != 0;
}
//initialise LCD display
void initLCD(void) {
//LCD
lcd_init(LCD_DISP_ON); /* initialize display, cursor off */
lcd_clrscr(); /* clear display and home cursor */
lcd_puts("SunAVR ");
}
void printTime(TimeVal* time, int level){
int hour = time->hour;
int min = time->min;
int sec = time->sec;
if (level>=0)
{
itoa(hour,bufferLCD,10);
if (hour < 10)
{
lcd_puts(" ");
}
lcd_puts(bufferLCD);
lcd_puts(":");
itoa(min,bufferLCD,10);
if (min < 10)
{
lcd_puts("0");
}
lcd_puts(bufferLCD);
}
if (level>=2)
{
lcd_puts(":");
itoa(sec,bufferLCD,10);
if (sec < 10)
{
lcd_puts("0");
}
lcd_puts(bufferLCD);
}
lcd_puts(" ");
if (level>=1)
{
int date = time->date;
//int month = time->month;
int year = time->year;
itoa(date,bufferLCD,10);
lcd_puts(bufferLCD);
lcd_puts(" ");
lcd_puts(getMonthStr(time));
lcd_puts(" ");
itoa(year,bufferLCD,10);
lcd_puts(bufferLCD);
lcd_puts(" ");
}
}
unsigned short read_keys()
{
unsigned short key = 0;
if (SELECT() == 0)
{
key = 1;
}
else if (ENTER() == 0)
{
key = 2;
}
return key;
}
int main(void)
{
// Initialize I2C library
i2c_init();
// Initialize the LCD
initLCD();
float intensity;
//SET TIME
/* curTime.year=2014; */
/* curTime.month=2; */
/* curTime.date=12; */
/* curTime.hour=12; */
/* curTime.min=54; */
/* curTime.sec=01; */
/* setTime(&curTime); */
//alarm time #1
alarmTime1.hour = 5;
alarmTime1.min = 0;
//alarm time #2
alarmTime2.hour = 8;
alarmTime2.min = 0;
getTime(&curTime);
//Check point (it will not shine if the clock is not available)
blinkLIGHT(2);
setIO();
short alarmActive = 0;
// wait until the alarm time
while ( waitAlarm(&alarmTime1) & waitAlarm(&alarmTime2) )
{
// Get the seconds value at the last iteration
lastSec = curTime.sec;
// Read the current time
getTime(&curTime);
// Update the seconds count
if (curTime.sec != lastSec)
{
getTime(&curTime);
lcd_gotoxy(0,0);
printTime(&curTime, 1);
lcd_gotoxy(0,1);
printTime(&alarmTime1, 0);
printTime(&alarmTime2, 0);
}
key = read_keys();
// If a key was pressed
if (key)
{
OCR1A = 50;
_delay_ms(50);
OCR1A = 0;
_delay_ms(250);
// Add a debouncing delay
}
/* lcd_gotoxy(0,1); */
/* if (key==0) */
/* { */
/* lcd_puts("00"); */
/* } */
/* else if (key==1) */
/* { */
/* lcd_puts("10"); */
/* } */
/* else if (key==2) */
/* { */
/* lcd_puts("01"); */
/* } */
/* else if (key==3) */
/* { */
/* lcd_puts("11"); */
/* } */
/* lcd_puts(" "); */
/* itoa(key,bufferLCD,10); */
/* lcd_puts(bufferLCD); */
/* lcd_puts(" "); */
/* lcd_gotoxy(0,1); */
/* itoa(SELECT(),bufferLCD,10); */
/* lcd_puts(bufferLCD); */
/* lcd_puts(" "); */
/* itoa(ENTER(),bufferLCD,10); */
/* lcd_puts(bufferLCD); */
/* lcd_puts(" "); */
}
alarmActive = 1;
OCR1A = 50;
_delay_ms(50);
unsigned int difference=0;
float maxx;
float minx;
maxx = 11;
minx = 6.4;
float x;
while (alarmActive)
{
// Get the seconds value at the last iteration
lastSec = curTime.sec;
// Read the current time
getTime(&curTime);
// Update the seconds count
if (curTime.sec != lastSec)
{
difference++;
lcd_gotoxy(0,0);
printTime(&curTime, 1);
lcd_gotoxy(0,1);
printTime(&alarmTime1, 0);
printTime(&alarmTime2, 0);
x = minx + (maxx-minx)/(totalTime*60)*(float)difference;
intensity = expo(x)/expo(maxx); // calculate the relative intensity
/* lcd_gotoxy(0,0); */
/* bufferLCD[0] = ' '; */
/* bufferLCD[1] = ' '; */
/* bufferLCD[2] = ' '; */
/* bufferLCD[3] = ' '; */
/* bufferLCD[4] = ' '; */
/* bufferLCD[5] = '\0'; */
itoa(difference,bufferLCD,10);
lcd_puts(bufferLCD);
lcd_puts(" ");
/* lcd_gotoxy(0,1); */
/* bufferLCD[0] = ' '; */
/* bufferLCD[1] = ' '; */
/* bufferLCD[2] = ' '; */
/* bufferLCD[3] = ' '; */
/* bufferLCD[4] = ' '; */
/* bufferLCD[5] = '\0'; */
/* itoa(calculateOCR1Apercent(intensity),bufferLCD,10); */
/* lcd_puts(bufferLCD); */
/* lcd_puts(" "); */
OCR1A = calculateOCR1Apercent(intensity);
// set PWM at intensity (relative intensity) @ 10bit
}
if (difference==(totalTime*60))
{
alarmActive = 0;
}
}
while(1)
{
//blink
_delay_ms(200);
// OCR1A = 0;
_delay_ms(200);
// OCR1A = 1023;
// Read the current time
getTime(&curTime);
lcd_gotoxy(0,0);
printTime(&curTime, 1);
}
}