1 | #include "io430.h"
|
2 | #include "stdlib.h"
|
3 | #include <stdint.h> // Integers of defined sizes
|
4 | #include <math.h>
|
5 | #include <float.h>
|
6 | #define Vobj 0x00;
|
7 | #define Temp 0x01;
|
8 | #define Conf 0x02;
|
9 |
|
10 | /// P2.5 => switches on
|
11 | // after switch on, chech left and right, delay ~0.25 s
|
12 | //if all touch buttons idle=> start measurments
|
13 | // if set temperature is acchived=> go stand by with delay of 12 s
|
14 | void I2C_SEND (int address, int pointer);
|
15 | void I2C_REC () ;
|
16 | void Measure() ;
|
17 | uint8_t LowerByte,UpperByte;
|
18 | int16_t Voltage;
|
19 | int Temperature;
|
20 | void Calc(int T, int P);
|
21 | void Touch_Sample(uint8_t BTN);
|
22 | void Touch_Start(uint8_t BTN);
|
23 | long double Result;
|
24 | int SetTemp=20;//default temperature
|
25 | uint16_t SampleTime;
|
26 | uint8_t R,G,B=0;
|
27 | uint8_t BTN_SLT =BIT5; // P2.5 (centre)
|
28 | uint16_t delay=0xFFFF ;
|
29 | int FLAG = 0;
|
30 | int wait =10;
|
31 | void StandBy(int Value);
|
32 | void Send_1();
|
33 | void Send_0();
|
34 | void LED_RST();
|
35 | void LED_CTRL(int red, int green, int blue);
|
36 | main( void )
|
37 | {
|
38 |
|
39 | WDTCTL = WDTPW|WDTHOLD; // Stop watchdog timer to prevent time out reset
|
40 | BCSCTL1 |= RSEL0|RSEL1|RSEL2|RSEL3;
|
41 | BCSCTL2 = SELM_3|SELS; // VCO as SMCLK,DCO as MCLK
|
42 | BCSCTL3 = LFXT1S_2; //USE VCO as LFXT (ACLK)
|
43 | P1OUT =0x0 ; //LOW
|
44 | P2OUT =0x0; //LOW
|
45 | P1DIR =0xFF; //output
|
46 | P2DIR =0xFF; //output
|
47 | P2SEL = 0;
|
48 | P2SEL2 = 0;
|
49 | TACTL |= TASSEL0|TAIE; // Timer clock ACLK
|
50 | //I2C Configuration//
|
51 | USICKCTL |= USISSEL_1|USICKPL; // SMLCK, IdleState=Low
|
52 | USICTL1 |= USII2C; // I2C Mode
|
53 | USICTL0 = USIPE7|USIPE6|USIMST; // Enable SDO/SDI/SCLK etc, master mode
|
54 | while(1){
|
55 | __enable_interrupt();
|
56 | StandBy(delay) ;// start timer in standby mode
|
57 | if(FLAG ==1){ //touch detected
|
58 | Measure(); //measure temp.
|
59 | }
|
60 | __low_power_mode_3();
|
61 |
|
62 |
|
63 | }
|
64 | }
|
65 | #pragma vector = TIMER0_A1_VECTOR
|
66 | __interrupt void TIMER0_A1_ISR (void) // ISR for TACCR1 CCIFG and TAIFG
|
67 | {
|
68 | Touch_Start(BTN_SLT);
|
69 |
|
70 | }
|
71 | #pragma vector = WDT_VECTOR
|
72 | __interrupt void WDT (void)
|
73 | {
|
74 | Touch_Sample(BTN_SLT);
|
75 | __bic_SR_register_on_exit(CPUOFF);
|
76 | }
|
77 | void Measure (){
|
78 |
|
79 | P1OUT|=BIT1; //acivate TMP006
|
80 | LED_CTRL(0,0,150);
|
81 |
|
82 | __delay_cycles(15000000);
|
83 | I2C_SEND(0x80,0x01) ; // get temp
|
84 | __delay_cycles(100) ;
|
85 | I2C_REC() ;
|
86 | Temperature =(UpperByte<<8) | LowerByte; //merge 2x8Bits
|
87 | Temperature = Temperature>>2;
|
88 | I2C_SEND(0x80,0x00) ; //get voltage
|
89 | __delay_cycles(100) ;
|
90 | I2C_REC() ;
|
91 | Voltage =(UpperByte<<8) | LowerByte;
|
92 | //Calculate object temperature//
|
93 | Calc(Temperature, Voltage);
|
94 | P1OUT &=~BIT1;
|
95 | if(Result>SetTemp){ //exit measument mode
|
96 | FLAG=0;
|
97 | delay=0xFFFF;
|
98 | LED_CTRL(0,200,0);
|
99 | __delay_cycles(1000000);
|
100 |
|
101 | }
|
102 |
|
103 | }
|
104 | void I2C_SEND (int address, int pointer ) {
|
105 | USICNT &=~USI16B; //8 bit register
|
106 | USISRH =0;
|
107 | USISRL =0;
|
108 | USICTL0 |=USIGE|USIOE ; //Latch/SDA output enabled
|
109 | USICTL0 &=~USIGE; // Latch disabled
|
110 | USISR = address; // send address first
|
111 | USICTL0 |= USIOE; //output enable
|
112 | USICNT =8;
|
113 | while((USICTL1&USIIFG)==0) {} // wait till finish
|
114 | USICTL0 &=~USIOE;
|
115 | USICNT=1;
|
116 | while((USICTL1&USIIFG)==0) {} //wait ack
|
117 | USISR = pointer; //send pointer
|
118 | USICTL0 |= USIOE;
|
119 | USICNT =8;
|
120 | while((USICTL1&USIIFG)==0) {} // wait till finish
|
121 | USICTL0 &=~USIOE;
|
122 | USICNT=1;
|
123 | while((USICTL1&USIIFG)==0) {} //wait ack
|
124 | // SEND STOP//
|
125 | USICTL0 |= USIOE; //output enable
|
126 | USISRL =0;
|
127 | USICNT =1;
|
128 | while((USICTL1&USIIFG)==0) {}
|
129 | USISRL= 0xFF;
|
130 | USICTL0 |=USIGE;
|
131 | USICTL0 &=~USIOE|USIGE;
|
132 | }
|
133 | void I2C_REC() {
|
134 | USICNT &=~USI16B; //8 bit register
|
135 | USISRH =0;
|
136 | USISRL =0;
|
137 | USICTL0 |=USIGE|USIOE ; //Latch/SDA output enabled
|
138 | USICTL0 &=~USIGE; // Latch disabled
|
139 | USISR = 0x81; // send address first
|
140 | USICTL0 |= USIOE; //output enable
|
141 | USICNT =8;
|
142 | while((USICTL1&USIIFG)==0) {} // wait till finish
|
143 | USICTL0 &=~USIOE;
|
144 | USICNT=1;
|
145 | //wait for ACK
|
146 | USICTL0 &=~USIOE;
|
147 | USICNT=1;
|
148 | while((USICTL1&USIIFG)==0) {} //wait ack
|
149 | USICNT=8; // receive first 8 bit
|
150 | while((USICTL1&USIIFG)==0) {} //wait till done
|
151 | // generate ACK
|
152 | UpperByte = USISRL;
|
153 | USICTL0 |= USIOE;
|
154 | USISRL =0;
|
155 | USICNT=1;
|
156 | while((USICTL1&USIIFG)==0) {}//wait
|
157 | USICTL0 &=~USIOE;
|
158 | USICNT=8; // receive second 8 bit
|
159 | while((USICTL1&USIIFG)==0) {} //wait till done
|
160 | //SEND STOP
|
161 | LowerByte =USISRL;
|
162 | USICTL0 |= USIOE; //output enable
|
163 | USISRL =0;
|
164 | USICNT =1;
|
165 | while((USICTL1&USIIFG)==0) {}
|
166 | USISRL= 0xFF;
|
167 | USICTL0 |=USIGE;
|
168 | USICTL0 &=~USIOE|USIGE;
|
169 |
|
170 | }
|
171 | void Calc(int T, int P){
|
172 | long double Vobj2 = (double)P*156.25*pow(10,-9);
|
173 | long double Tdie2 = (double)T*.03125 + 273.15;
|
174 | long double S0 =6.633*pow(10,-14);
|
175 | long double a1 = 1.75*pow(10,-3);
|
176 | long double a2 = -1.678*pow(10,-5);
|
177 | long double b0 = -2.94*pow(10,-5);
|
178 | long double b1 = -5.7*pow(10,-7);
|
179 | long double b2 =4.63*pow(10,-9);
|
180 | long double c2 = 13.4;
|
181 | long double Tref = 298.15;
|
182 | long double S = S0*(1+a1*(Tdie2 - Tref)+a2*pow((Tdie2 - Tref),2));
|
183 | long double Vos = b0 + b1*(Tdie2 - Tref) + b2*pow((Tdie2 - Tref),2);
|
184 | long double fObj = (Vobj2 - Vos) + c2*pow((Vobj2 - Vos),2);
|
185 | long double Tobj = pow(pow(Tdie2,4) + (fObj/S),.25);
|
186 | Result= (Tobj-273.15);
|
187 | }
|
188 | void Touch_Start(uint8_t BTN) // start touch sensing
|
189 | {
|
190 | TACTL &=~TAIE; //disable Timer interrupt
|
191 | WDTCTL = WDTPW|WDTHOLD|WDTCNTCL; // WDT OFF
|
192 | TACTL |= TASSEL1|TASSEL0|ID0; //CLOCK TACLK (oscillating pin)
|
193 | IE1 |=WDTIE; // Enable WDT interrupt
|
194 | P2SEL = 0;
|
195 | P2SEL2 =BTN;// oscillation on BTN
|
196 | WDTCTL = WDTPW|WDTTMSEL|WDTSSEL|WDTCNTCL|WDTIS1; //interval mode,ACLK,clear
|
197 | TACTL &=~TAIFG; // Clear flag
|
198 | IFG1 &=~WDTIFG;
|
199 | TACTL &=~MC_1;
|
200 | TACTL |= MC_2|TACLR; //start timer free mode
|
201 |
|
202 | }
|
203 | void Touch_Sample(uint8_t BTN)
|
204 | {
|
205 | TACTL &=~MC_2; // STOP TIMER
|
206 | P2SEL2 &=~ BTN; // Stop oscillation
|
207 | WDTCTL = WDTPW|WDTHOLD|WDTCNTCL; //WDT OFF
|
208 | IE1 &=~ WDTIE; // disable WTD interrupt
|
209 | SampleTime= TAR; // save sample time
|
210 | LED_CTRL(SetTemp,0,0);
|
211 |
|
212 | if (BTN==BIT5){ // if true switch on the deivce!!! Measure left/right pads
|
213 | if( SampleTime<33000){
|
214 | FLAG=0;
|
215 | wait=20;
|
216 | P1OUT|=BIT0;
|
217 | BTN_SLT=BIT4;
|
218 | delay=0x05FF; //dalay for touch sampling
|
219 | }
|
220 | }
|
221 | if (BTN==BIT4) {
|
222 | if(SampleTime<32000){ //touch detected? dec temp
|
223 | if(SetTemp>2){
|
224 | SetTemp=SetTemp-2;
|
225 | }
|
226 | wait =20;
|
227 |
|
228 | } else {
|
229 | wait--;
|
230 | }
|
231 | BTN_SLT=BIT3;
|
232 | if(wait==0){ //deactivate setTemp mode, measure temp
|
233 | FLAG=1;
|
234 | P1OUT &=~BIT0;
|
235 | BTN_SLT=BIT5;
|
236 | delay=0xFFFF;// delay for temp measurments
|
237 |
|
238 | }
|
239 | }
|
240 | if (BTN==BIT3) {
|
241 | if(SampleTime<32000){ //touch detected? inc temp
|
242 | if(SetTemp<85){
|
243 | SetTemp=SetTemp+2;
|
244 | }
|
245 | wait =20;
|
246 | } else {
|
247 | wait--;
|
248 | }
|
249 | BTN_SLT=BIT4;
|
250 |
|
251 | if(wait==0){ //deactivate setTemp mode, measure temp
|
252 | FLAG=1;
|
253 | P1OUT &=~BIT0;
|
254 | BTN_SLT=BIT5;
|
255 | delay=0xFFFF;// delay for temp measurments
|
256 | LED_CTRL(0,0,0);
|
257 |
|
258 | }
|
259 | }
|
260 |
|
261 |
|
262 |
|
263 | TACTL |= TAIE|TACLR; // Enable timer interrupt, clear flag
|
264 | TACTL &=~TAIFG; // Clear flag
|
265 | TACTL &=~TASSEL1;
|
266 |
|
267 |
|
268 | }
|
269 | void StandBy( int Value){
|
270 | TACCR0 = Value;
|
271 | TACTL|= MC_1|TACLR; // clear and start counting to TAR, stand by mode
|
272 | }
|
273 |
|
274 | void LED_CTRL(int red, int green, int blue) {
|
275 | //LED_RST();
|
276 | if((green&BIT7)==BIT7){
|
277 | Send_1();
|
278 | } else {
|
279 | Send_0();
|
280 | }
|
281 | if((green&BIT6)==BIT6){
|
282 | Send_1();
|
283 | } else {
|
284 | Send_0();
|
285 | }
|
286 | if((green&BIT5)==BIT5){
|
287 | Send_1();
|
288 | } else {
|
289 | Send_0();
|
290 | }
|
291 | if((green&BIT4)==BIT4){
|
292 | Send_1();
|
293 | } else {
|
294 | Send_0();
|
295 | }
|
296 | if((green&BIT3)==BIT3){
|
297 | Send_1();
|
298 | } else {
|
299 | Send_0();
|
300 | }
|
301 | if((green&BIT2)==BIT2){
|
302 | Send_1();
|
303 | } else {
|
304 | Send_0();
|
305 | }
|
306 | if((green&BIT1)==BIT1){
|
307 | Send_1();
|
308 | } else {
|
309 | Send_0();
|
310 | }
|
311 | if((green&BIT0)==BIT0){
|
312 | Send_1();
|
313 | } else {
|
314 | Send_0();
|
315 | }
|
316 | if((green&BIT7)==BIT7){
|
317 | Send_1();
|
318 | } else {
|
319 | Send_0();
|
320 | }
|
321 | //////////RED///////////////////////////////////////
|
322 | if((red&BIT6)==BIT6){
|
323 | Send_1();
|
324 | } else {
|
325 | Send_0();
|
326 | }
|
327 | if((red&BIT5)==BIT5){
|
328 | Send_1();
|
329 | } else {
|
330 | Send_0();
|
331 | }
|
332 | if((red&BIT4)==BIT4){
|
333 | Send_1();
|
334 | } else {
|
335 | Send_0();
|
336 | }
|
337 | if((red&BIT3)==BIT3){
|
338 | Send_1();
|
339 | } else {
|
340 | Send_0();
|
341 | }
|
342 | if((red&BIT2)==BIT2){
|
343 | Send_1();
|
344 | } else {
|
345 | Send_0();
|
346 | }
|
347 | if((red&BIT1)==BIT1){
|
348 | Send_1();
|
349 | } else {
|
350 | Send_0();
|
351 | }
|
352 | if((red&BIT0)==BIT0){
|
353 | Send_1();
|
354 | } else {
|
355 | Send_0();
|
356 | }
|
357 | ///////////////////BLUE
|
358 | if((blue&BIT7)==BIT7){
|
359 | Send_1();
|
360 | } else {
|
361 | Send_0();
|
362 | }
|
363 | if((blue&BIT6)==BIT6){
|
364 | Send_1();
|
365 | } else {
|
366 | Send_0();
|
367 | }
|
368 | if((blue&BIT5)==BIT5){
|
369 | Send_1();
|
370 | } else {
|
371 | Send_0();
|
372 | }
|
373 | if((blue&BIT4)==BIT4){
|
374 | Send_1();
|
375 | } else {
|
376 | Send_0();
|
377 | }
|
378 | if((blue&BIT3)==BIT3){
|
379 | Send_1();
|
380 | } else {
|
381 | Send_0();
|
382 | }
|
383 | if((green&BIT2)==BIT2){
|
384 | Send_1();
|
385 | } else {
|
386 | Send_0();
|
387 | }
|
388 | if((blue&BIT1)==BIT1){
|
389 | Send_1();
|
390 | } else {
|
391 | Send_0();
|
392 | }
|
393 | if((blue&BIT0)==BIT0){
|
394 | Send_1();
|
395 | } else {
|
396 | Send_0();
|
397 | }
|
398 |
|
399 |
|
400 |
|
401 | }
|
402 | void Send_0(){
|
403 | P2OUT|=BIT0; //set LED_IN HIGH
|
404 | __delay_cycles(3); //delay
|
405 | P2OUT &=~BIT0; //set LED_IN LOW
|
406 | __delay_cycles(8);
|
407 | }
|
408 | void Send_1(){
|
409 | P2OUT|=BIT0; //set LED_IN HIGH
|
410 | __delay_cycles(16);
|
411 | P2OUT &=~BIT0;
|
412 | __delay_cycles(1);
|
413 | }
|
414 |
|
415 | void LED_RST(){
|
416 | P2OUT|=BIT0; //set LED_IN HIGH
|
417 | P2OUT &=~BIT0;
|
418 | __delay_cycles(2000);
|
419 | P2OUT|=BIT0; //set LED_IN HIGH
|
420 |
|
421 | }
|