-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
320 lines (286 loc) · 6.83 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
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "main.h" /* Function inits, global variables, definitions, etc. */
void main(void) {
initialize(); //Initialize ALL THE THINGS.
for(;;) {
_FEED_COP(); /* feeds the dog */
if(tenths == 1){ //if our tenth of a second flag is set...
tenths = 0; //unset it...
ATD_Convert(); //run an ATD Conversion
convertTemp(); //convert the temperature from the conversion
displayTemp(); //and display the temperature.
}
} /* loop forever */
/* please make sure that you never leave main */
}
/* initialize */
/* Runs all of the initializations, also
initializes global variables */
void initialize(void){
tencnt = 0;
tenths = 0;
DDRT = 0xFF;
ATDDIEN =0x00;
TIM_Init();
PWM_Init();
ATD_Init();
SPI_Init();
IR_Init();
LCD_Init();
EnableInterrupts;
}
/* TIM-Init */
/* Initilizes the Timer */
void TIM_Init(void){
TSCR1 = 0x80;
TIOS = 0x80;
TSCR2 = 0x0C;
TC7 = 15000;
TIE = 0x80; //enable or disable???? enable
}
/* PWM_Init */
/* Initilizes the PWM */
void PWM_Init(void){
//sampling frequency approximately 100Hz
MODRR = 0x0F;
PWME = 0x0F;
PWMPOL = 0x00;
PWMCTL = 0x00;
PWMCAE = 0x00;
PWMPER0 = 0xFF;
PWMPER1 = 0xFF;
PWMPER2 = 0xFF;
PWMPER3 = 0xFF;
PWMDTY0 = 0x00;
PWMDTY1 = 0x00;
PWMDTY2 = 0x00;
PWMDTY3 = 0x00;
PWMPRCLK= 0x77;
PWMCLK = 0x0F;
PWMSCLA = 0x04;
PWMSCLB = 0x04;
//DDRT
//PTT
}
/* ATD_Init */
/* Initilizes the ATD */
void ATD_Init(void){
ATDCTL2 = 0x80;
ATDCTL3 = 0x00; //Sequence length = 8
ATDCTL4 = 0x05; //%00000101 10 bit resolution;2 ATD clock periods;
}
/* SPI_Init */
/* Initilizes the SPI */
void SPI_Init(void){
SPIBR = 0x01;
SPICR1 = 0x50;
SPICR2 = 0x00;
}
/* LCD_Init */
/* Initilizes the LCD */
void LCD_Init(void){
PTT |= LCDCLK;
PTT &= ~RW;
LCD_Instruction(LCDON);
LCD_Instruction(TWOLINE);
LCD_Instruction(LCDCLR);
wait(2998); //delay for 2ms;
}
/* IR_Init */
/* Initilizes the IR 'nominal' value *
* That is, reads the IR in the room and *
* uses that as the nominal value */
void IR_Init(void)
{
ATDCTL5 = 0x10; //begin with channel 0
while((ATDSTAT1&0x01)==0){
continue;
}; // wait for CCF0
//Get the IR Sensor inputs
//Use the minimum value, as it's the one reading the highest IR (5V = no IR, 0V = OMG SO MUCH IR!!)
if (ATDDR0 < ATDDR1 && ATDDR0 < ATDDR2 && ATDDR0 < ATDDR3){
maxDist = ATDDR0;
}
else if (ATDDR1 < ATDDR2 && ATDDR1 < ATDDR3 && ATDDR1 < ATDDR0){
maxDist = ATDDR1;
}
else if (ATDDR2 < ATDDR3 && ATDDR2 < ATDDR0 & ATDDR2 < ATDDR1){
maxDist = ATDDR2;
}
else{ maxDist = ATDDR3; }
}
/* wait */
/* Waits for a given period of time.
wait(2998): appx 2ms
wait(2): appx 30 cycles
TCNT will increase every 0.667 us
[1.5MHz(24/(prescalar=16))] */
void static wait(unsigned short delay){
unsigned short startTime;
startTime = TCNT;
while((TCNT-startTime) <= delay){}
}
/* LCD_Instruction */
/* Sends an instruction to the LCD */
void LCD_Instruction(unsigned char instru){
PTT &= ~RS;
LCD_Send(instru);
}
/* LCD_Send */
/* Sends a byte to the LCD */
void LCD_Send(unsigned char byt){
while (SPISR_SPTEF == 0) {}
SPIDR = byt;
wait(2);
PTT &= ~LCDCLK;
asm nop;
asm nop
PTT |= LCDCLK;
wait(50);
}
/* LCD_Printc */
/* Sends a character to the LCD */
void LCD_Printc(unsigned char character){
PTT |= RS;
LCD_Send(character);
}
/* LCD_Printmsg */
/* Prints a string to the LCD */
void LCD_Printmsg(char *pt){
while(*pt){
LCD_Printc((unsigned char)*pt);
pt++;
}
}
/*ATD_Convert */
/* Performs ATD Conversions for the Temperature Sensors and the
* IR receivers. */
void ATD_Convert(void){
char dist1, dist2, dist3, dist4
int duty1, duty2, duty3, duty4;
ATDCTL5 = 0x10; //begin with channel 0
while((ATDSTAT1&0x01)==0){
continue;
}; // wait for CCF0
//Get the temperature sensor inputs
tem1 = ATDDR4;
tem2 = ATDDR5;
tem3 = ATDDR6;
tem4 = ATDDR7;
//Get the IR Sensor inputs
dist1 = ATDDR0;
dist2 = ATDDR1;
dist3 = ATDDR2;
dist4 = ATDDR3;
//Run conversions
duty1 = distConvert(dist1);
duty2 = distConvert(dist2);
duty3 = distConvert(dist3);
duty4 = distConvert(dist4);
//Output to the PWMs
PWMDTY0 = duty1; //modify the PWM duty cycle
PWMDTY1 = duty2;
PWMDTY2 = duty3;
PWMDTY3 = duty4;
}
/* distConvert */
/* Converts the 0-5 percentage from the ATD to a duty cycle
* to send to the PWM */
int distConvert(char distance)
{
int output;
//So, what happens here:
//distance/maxDist gives us a 0-1 value, with 1 being nothing there and 0 being really close.
//We multiply this by 100 to get a percentage that's the opposite of what we need, so
//we just subtract it from 100 to get what we need.
output = 100 - (100*distance)/maxDist;
return output;
}
/* Convert Temp */
/* Converts the given temperature from the actual
* sensor (via the ATD) to an actual temperature
* (right?) */
void convertTemp(void){
//Convert tem1
asm(" ldd tem1");
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm(" std tem1");
tem1 = tem1 * 500/1024;
//Convert tem2
asm(" ldd tem2");
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm(" std tem2");
tem2 = tem2 * 500/1024;
//Convert tem3
asm(" ldd tem3");
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm(" std tem3");
tem3 = tem3 * 500/1024;
//Convert tem4
asm(" ldd tem4");
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm lsrd
asm(" std tem4");
tem4 = tem4 * 500/1024;
}
/* displayTemp */
/* Runs all of the LCD temperature display code */
void displayTemp(void){
LCD_Instruction(LCDCLR);
LCD_Instruction(CURMOV);
LCD_Instruction(LINE1);
LCD_Printmsg(" P1:");
printLCDTemp(tem1);
LCD_Printmsg(" P2:");
printLCDTemp(tem2);
LCD_Instruction(CURMOV);
LCD_Instruction(LINE2);
LCD_Printmsg("P3:");
printLCDTemp(tem3);
LCD_Printmsg(" P4:");
printLCDTemp(tem4);
}
/* printLCDTemp procedure */
/* prints arbitrary messages to the LCD based on
given temperature */
void printLCDTemp(unsigned short temp){
if(temp >= 32)
LCD_Printmsg("HOT ");
else if(temp >= 26)
LCD_Printmsg("WARM");
else if(temp >= 21)
LCD_Printmsg("NORM");
else if(temp >= 15)
LCD_Printmsg("COOL");
else
LCD_Printmsg("COLD");}
/* TIM Interrupt Procedure */
/* increments tenth of a second counter */
void interrupt 15 timerInterrupt(void){
TFLG1 = 0x80;
tencnt++;
if(tencnt >= 10){
tenths = 1;
tencnt = 0;
}
}