-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.c
67 lines (57 loc) · 1.64 KB
/
adc.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
#include <LPC11xx.h>
#include <math.h>
#include "adc.h"
int get_temp (int ch) {
float ad, f;
ad = get_adc(ch);
ad = ad / (float)0x3ff * 3.3;
switch (ch) {
case ADC_TEMP_BEAD1:
case ADC_TEMP_BEAD2:
f = ad;
// f = THERMISTOR_R0 * f / (1.0 - f);
f = f / ((3.3 - f) / THERMISTOR_R0);
f = (1.0 / ((1.0 / (THERMISTOR_T0 + 273.15)) + ((float)log(f / THERMISTOR_R0) / THERMISTOR_B))) - 273.15;
break;
case ADC_TEMP_EXT:
f = ad * 0.97;
// f = THERMISTOR_ER0 * f / (1.0 - f);
f = f / ((3.3 - f) / THERMISTOR_ER0);
f = (1.0 / ((1.0 / (THERMISTOR_ET0 + 273.15)) + ((float)log(f / THERMISTOR_ER0) / THERMISTOR_EB))) - 273.15;
break;
}
return (int)f;
}
uint32_t get_adc (int ch) {
uint32_t ad = 0, stat;
LPC_ADC->CR &= ~0xff;
LPC_ADC->CR |= (1<<24)|(1<<ch); // start adc
for (;;) {
stat = LPC_ADC->STAT;
if (stat & (1<<ch)) {
ad = (LPC_ADC->DR[ch] >> 6) & 0x3ff;
break;
}
if (stat & (1<<(ch+8))) { // overrun
stat = LPC_ADC->DR[ch];
break;
}
}
LPC_ADC->CR &= ~(1<<24); // stop
return ad;
}
void init_adc (uint32_t ADC_Clk) {
LPC_SYSCON->PDRUNCFG &= ~(1<<4); // power up
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13); // AHB clock
LPC_ADC->CR = ((SystemCoreClock / LPC_SYSCON->SYSAHBCLKDIV) / ADC_Clk - 1) << 8;
// ADC I/O config
LPC_IOCON->R_PIO0_11 &= ~0x8F; // ADC0
LPC_IOCON->R_PIO0_11 |= 0x02;
LPC_IOCON->R_PIO1_0 &= ~0x8F; // ADC1
LPC_IOCON->R_PIO1_0 |= 0x02;
LPC_IOCON->R_PIO1_1 &= ~0x8F; // ADC2
LPC_IOCON->R_PIO1_1 |= 0x02;
LPC_IOCON->R_PIO0_11 = 0x02; // Select AD0 pin function
LPC_IOCON->R_PIO1_0 = 0x02; // Select AD1 pin function
LPC_IOCON->R_PIO1_1 = 0x02; // Select AD2 pin function
}