-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temp Sensor TMP36
109 lines (84 loc) · 2.88 KB
/
Temp Sensor TMP36
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
#include "msp.h"
#include "stdio.h"
#define ADC18 P8
#define TEMP BIT7
#define PHOTO_ADC P6
#define TEMP_LED BIT3
void ADC14init(void);
void SysTick_init(void);
void delay_ms(uint16_t delay);
uint16_t result;
float NADC, tempC, tempF;
int delayInterrupt, tempFlag;
void main(void){
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
ADC14init();
SysTick_init();
delayInterrupt = 0;
while(1){
ADC14-> CTL0 |= 1; //start conversion
tempFlag = 0;
while(!ADC14->IFGR0);
result = ADC14->MEM[0]; //get val from adc
NADC = ((result * 3.3) / 16384); //conversion based on value in mem0
tempC = (abs((NADC * 1000) - 500) / 10);
tempF = ((tempC * 1.8) + 32);
//printf("Raw ADC val: %d\nConverted voltage val: %fV\n", result, NADC);
//printf("Temp in F: %fF\n", tempF);
//printf("Temp in C: %fC\n", tempC);
//delay_ms(1000); //sample every second
if(tempF >= 80){
tempFlag = 1;
do{
PHOTO_ADC->OUT |= TEMP_LED;
delay_ms(500); //3/4 sec
PHOTO_ADC->OUT &= ~TEMP_LED;
tempFlag = 0;
} while(tempFlag);
}
}
}
void ADC14init(void){ //initialize P8.7 for ADC (taken from Kandalaft)
//pin intialization:
ADC18-> SEL0 |= TEMP;
ADC18-> SEL1 |= TEMP;
ADC18-> DIR &= ~TEMP;
PHOTO_ADC->SEL0 &= ~TEMP_LED; //setting up pin as an output
PHOTO_ADC->SEL1 &= ~TEMP_LED;
PHOTO_ADC->DIR |= TEMP_LED;
PHOTO_ADC->OUT &= ~TEMP_LED;
//peripheral initialization
ADC14-> CTL0 &= ~0x02; //disable ADC converter during initialization
ADC14-> CTL0 |= 0x04200210; //S/H pulse mode, SMCLK, 16 sample clocks
ADC14-> CTL1 = 0x00000030; //14 bit resolution
ADC14-> MCTL[0] = 0x12; //ADC14INCHx = 0 for mem[0]
ADC14-> CTL0 |= 0x02; //enable ADC14ENC, Starts the ADC after confg.
}
void SysTick_init(void){
if(delayInterrupt == 1){
SysTick -> CTRL = 0;
SysTick -> LOAD = 3000000; //1 second interrupts
SysTick -> VAL = 0; //start val
SysTick -> CTRL = 0x00000007; //SysTick enable 3 MHz interrupts enabled
}
else {
SysTick -> CTRL = 0;
SysTick -> LOAD = 0x00FFFFFF; //max load val
SysTick -> VAL = 0; //start val
SysTick -> CTRL = 0x00000005; //SysTick enable 3 MHz no interrupts
}
}
void delay_ms(uint16_t delay){
SysTick->LOAD = (3000 * delay) - 1; //1ms * ms delay
SysTick->VAL = 0;
SysTick->CTRL |= (0x05); //bit 16 SysTick CTRL countflag
while((SysTick->CTRL & 0x00010000) == 0);
}
//void delay_us(uint8_t usDelay) { //taken from Kandalaft's slides
//
// SysTick->LOAD = (3 * usDelay) - 1; //1ms * ms delay
// SysTick->VAL = 0;
// SysTick->CTRL |= (0x05); //bit 16 SysTick CTRL countflag
//
// while(!(SysTick->CTRL & 0x00010000) == 0);
//}