summaryrefslogtreecommitdiff
path: root/src/arch/msp430fr5994lp/driver/adc.cc
blob: 5a044a6986b72ab00ac0e3f028a8f5845d7acc3f (plain)
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
#include "driver/adc.h"
#include <msp430.h>

#define CALADC12_12V_30C  *((unsigned int *)0x1A1A)
#define CALADC12_12V_85C  *((unsigned int *)0x1A1C)

float ADC::getTemp()
{
	float ret;

	while(REFCTL0 & REFGENBUSY);

	REFCTL0 = REFVSEL_0 | REFON;
	ADC12CTL0 &= ~ADC12ENC;
	ADC12CTL0 = ADC12SHT0_8 | ADC12ON;
	ADC12CTL1 = ADC12SHP;
	ADC12CTL3 = ADC12TCMAP;
	ADC12MCTL0 = ADC12VRSEL_1 | ADC12INCH_30;
	while(!(REFCTL0 & REFGENRDY));

	ADC12CTL0 |= ADC12ENC;
	ADC12CTL0 |= ADC12SC;
	while (ADC12CTL1 & ADC12BUSY);

	ret = (float)((long)ADC12MEM0 - CALADC12_12V_30C) * (85 - 30) /
		(CALADC12_12V_85C - CALADC12_12V_30C) + 30.0f;

	// Disable ADC
	ADC12CTL0 &= ~ADC12ENC; // disable any conversion to allow ADC configuration
	ADC12CTL0 &= ~ADC12ON; // Turn off ADC

	// Disable internal 2V reference
	while(REFCTL0 & REFGENBUSY);
	REFCTL0 &= ~REFON;

	return ret;
}

float ADC::getVCC()
{
	float ret;

	while(REFCTL0 & REFGENBUSY);

	REFCTL0 = REFVSEL_1 | REFON;
	ADC12CTL0 &= ~ADC12ENC;
	ADC12CTL0 = ADC12SHT0_8 | ADC12ON;
	ADC12CTL1 = ADC12SHP;
	ADC12CTL3 = ADC12BATMAP;
	ADC12MCTL0 = ADC12VRSEL_1 | ADC12INCH_31;
	while(!(REFCTL0 & REFGENRDY));

	ADC12CTL0 |= ADC12ENC;
	ADC12CTL0 |= ADC12SC;
	while (ADC12CTL1 & ADC12BUSY);

	ret = (float)ADC12MEM0 / 4096 * 2 * 2;
	return ret;

	// Disable ADC
	ADC12CTL0 &= ~ADC12ENC; // disable any conversion to allow ADC configuration
	ADC12CTL0 &= ~ADC12ON; // Turn off ADC

	// Disable internal 2V reference
	while(REFCTL0 & REFGENBUSY);
	REFCTL0 &= ~REFON;

	return ret;
}

ADC adc;