Unverified Commit 803f605e authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

msp430fr5994lp/adc: add getReading function

parent d3c494bb
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@
#ifndef ADC_H
#define ADC_H

#include <stdint.h>

class ADC {
	private:
		ADC(ADC const &copy);
@@ -15,6 +17,7 @@ class ADC {

		float getTemp();
		float getVCC();
		uint16_t getReading();
};

extern ADC adc;
+33 −7
Original line number Diff line number Diff line
@@ -17,14 +17,13 @@ float ADC::getTemp()

	REFCTL0 = REFVSEL_0 | REFON;
	ADC12CTL0 &= ~ADC12ENC;
	ADC12CTL0 = ADC12SHT0_8 | ADC12ON;
	ADC12CTL0 = ADC12SHT0_8 | ADC12ON; // Sample and Hold: 256 ADC12CLK Cycles
	ADC12CTL1 = ADC12SHP;
	ADC12CTL3 = ADC12TCMAP;
	ADC12MCTL0 = ADC12VRSEL_1 | ADC12INCH_30;
	while(!(REFCTL0 & REFGENRDY));

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

	ret = (float)((long)ADC12MEM0 - CALADC12_12V_30C) * (85 - 30) /
@@ -49,18 +48,16 @@ float ADC::getVCC()

	REFCTL0 = REFVSEL_1 | REFON;
	ADC12CTL0 &= ~ADC12ENC;
	ADC12CTL0 = ADC12SHT0_8 | ADC12ON;
	ADC12CTL0 = ADC12SHT0_8 | ADC12ON; // Sample and Hold: 256 ADC12CLK Cycles
	ADC12CTL1 = ADC12SHP;
	ADC12CTL3 = ADC12BATMAP;
	ADC12MCTL0 = ADC12VRSEL_1 | ADC12INCH_31;
	while(!(REFCTL0 & REFGENRDY));

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

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

	// Disable ADC
	ADC12CTL0 &= ~ADC12ENC; // disable any conversion to allow ADC configuration
@@ -73,4 +70,33 @@ float ADC::getVCC()
	return ret;
}

uint16_t ADC::getReading()
{
	ADC12CTL0 &= ~ADC12ENC; // disable ADC for configuration
	ADC12CTL0 = ADC12SHT0_8 | ADC12SHT1_8 | ADC12ON; // Sample & Hold: 64 ADC12CLK cycles; enable ADC
	ADC12CTL1 = ADC12SHP; // MODOSC, no prescaler
	ADC12CTL2 = ADC12RES_2; // 12 bit resolution
	ADC12CTL3 = ADC12CSTARTADD_0; // store reading in ADCMEM0

	// disable output driver and input schmitt triggers on P3.0 (A12)
	P3SEL0 |= BIT0;
	P3SEL1 |= BIT0;

	// use default reference (AVSS / AVCC) (only required if changed by some other runtime code beforehand)
	// read from A12 (P3.0)
	ADC12MCTL0 = ADC12INCH_12;

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

	uint16_t ret = ADC12MEM0;

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

	return ret;
}

ADC adc;