Flipklok
 
Loading...
Searching...
No Matches
adc.h File Reference
#include <msp430.h>

Go to the source code of this file.

Macros

#define NUM_CHANNELS   3
 
#define referenceVoltage   2.5
 
#define adcMax   1023
 

Functions

void setupAdc ()
 Setup ADC, get called in startup from main(), Shortest samling rate, with 2.5V reference, no prdivider, setup channels and enable conversion interrupts.
 
void startAdcConv ()
 Start the ADC conversion'(s), gets called when end interrupt of ISR_TA0(void) is reached.
 
double convertAdc2Voltage (int R1, int R2, int adcReading)
 Convert the adc reading to a voltage before voltage divider.
 

Variables

volatile unsigned int adcResults [NUM_CHANNELS]
 

Macro Definition Documentation

◆ adcMax

#define adcMax   1023

Definition at line 17 of file adc.h.

Referenced by convertAdc2Voltage().

◆ NUM_CHANNELS

#define NUM_CHANNELS   3

Definition at line 15 of file adc.h.

◆ referenceVoltage

#define referenceVoltage   2.5

Definition at line 16 of file adc.h.

Referenced by convertAdc2Voltage().

Function Documentation

◆ convertAdc2Voltage()

double convertAdc2Voltage ( int R1,
int R2,
int adcReading )

Convert the adc reading to a voltage before voltage divider.

Returns
double voltage

Definition at line 132 of file adc.c.

133{
134 double voltage = referenceVoltage / adcMax * adcReading; //convert ADC reading to voltage on pin, using 2.5V reference
135 voltage = (voltage * (R1 + R2)) / R1; // convert voltage to actual voltage as input on the voltage divider
136 return voltage;
137}
#define adcMax
Definition adc.h:17
#define referenceVoltage
Definition adc.h:16

References adcMax, and referenceVoltage.

Referenced by ADC10_ISR().

◆ setupAdc()

void setupAdc ( )

Setup ADC, get called in startup from main(), Shortest samling rate, with 2.5V reference, no prdivider, setup channels and enable conversion interrupts.

Definition at line 86 of file adc.c.

87{
88 while (ADC10CTL1 & ADC10BUSY); // Wait if ADC is busy
89
90#warning ("TO-DO let ADC trigger by clock directly see SHSx on page 601 of manual");
91 // Configure ADC10
92
93
94 ADC10CTL0 &= ~ENC; // Disable conversions to be able to setup ADC registers
95 ADC10CTL0 = SREF_1 | ADC10SHT_0 | REF2_5V | REFON | ADC10ON; // Reference: VR+ = VREF+ and VR- = AVSS, set sampling time 4 ADC10CLK cycles, reference-generator voltage 2.5V, reference generator on, turn on ADC10.
96 ADC10CTL1 |= INCH_4 | ADC10DF; // First select channel 4
97 #ifdef UART_ENABLED
98 P1DIR &= ~BIT4; // Set P1.4 as input: LDR
99 ADC10AE0 |= INCH_4; // enable as analog inputs
100 #else
101 P1DIR &= ~BIT4; // Set P1.4 as input: LDR
102 P1DIR &= ~BIT1; // Set P1.1 as input: Battery
103 P1DIR &= ~BIT2; // Set P1.2 as input: Ignition switch
104 ADC10AE0 |= INCH_4 | INCH_1 | INCH_2; // enable as analog inputs
105 #endif
106 ADC10CTL1 &= ~ADC10DF; //Straight binary
107 ADC10CTL1 |= ADC10DIV_0; // Predivider 00b = Predivide by 1
108 ADC10CTL1 |= ADC10SSEL1; // Use sampling timer and set clock source to ACLK
109
110// ADC10CTL0 |= ENC; // Enable conversions
111 ADC10CTL0 |= ADC10IE; // Enable ADC conv complete interrupt
112
113 while ((ADC10CTL1 & ADC10BUSY)); // Wait if ADC is busy
114}

Referenced by main().

◆ startAdcConv()

void startAdcConv ( )

Start the ADC conversion'(s), gets called when end interrupt of ISR_TA0(void) is reached.

Definition at line 119 of file adc.c.

120{
121 ADC10CTL0 &= ~ENC; // Disable before editing control registers
122 ADC10CTL1 |= INCH_4; // First select channel 4
123 ADC10CTL0 |= ENC;
124 ADC10CTL0 |= ADC10SC; // Start conversion-software trigger
125}

Referenced by ISR_TA0().

Variable Documentation

◆ adcResults

volatile unsigned int adcResults[NUM_CHANNELS]

Definition at line 19 of file adc.h.

Referenced by ADC10_ISR().