Flipklok
 
Loading...
Searching...
No Matches
adc.c File Reference

implementation of the reading and converting the ADC values More...

#include "adc.h"
#include "led.h"
#include "gpio.h"
#include "uart.h"
#include "timer.h"
#include "dcfReceive.h"

Go to the source code of this file.

Functions

__interrupt void ADC10_ISR (void)
 ADC interrupt routine, gets called when end interrupt of ISR_TA0(void) is reached via startAdcConv().
 
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 ADCvar
 
volatile unsigned char currentChannel = 0
 
volatile double voltReading
 

Detailed Description

implementation of the reading and converting the ADC values

Definition in file adc.c.

Function Documentation

◆ ADC10_ISR()

__interrupt void ADC10_ISR ( void )

ADC interrupt routine, gets called when end interrupt of ISR_TA0(void) is reached via startAdcConv().

Definition at line 28 of file adc.c.

34{
35 ADC10CTL0 &= ~ADC10IFG; // Clear the interrupt flag
36 adcResults[currentChannel] = ADC10MEM; // Read out the result of the current pin
37 #if !defined(UART_ENABLED)
38 // Update the ADC input channel (INCH_x)
39 ADC10CTL0 &= ~ENC; // Disable conversion before changing channel
40 switch (currentChannel)
41 {
42 case 0: // P1.4 (LDR):
43 ADC10CTL1 = (ADC10CTL1 & ~INCH_4) | INCH_1;
45 break;
46 case 1: // P1.1 (Battery):
47 ADC10CTL1 = (ADC10CTL1 & ~INCH_1) | INCH_2;
48 break;
49 case 2: // P1.2 (Ignition):
50 ADC10CTL1 = (ADC10CTL1 & ~INCH_2); // No more ADC conversions until startAdcConv() is called again
52 if (voltReading > 12.0) // Switched on
53 {
54 wakeUp();
55 volatile int k = 0;
56 __no_operation();
57 }
58 else
59 {
60 sleep();
62 volatile int k = 0;
63 __no_operation();
64 }
65 break;
66 }
67
68 currentChannel++; // Move to next channel
69 if (currentChannel >= 3)
70 {
71 currentChannel = 0; //stop starting ADC conversions until startAdcConv() is called again
72 }
73 else
74 {
75 ADC10CTL0 |= ENC; // Enable conversions
76 ADC10CTL0 |= ADC10SC; // Start next conversion
77 }
78 #else
80 #endif
81}
volatile unsigned char currentChannel
Definition adc.c:19
double convertAdc2Voltage(int R1, int R2, int adcReading)
Convert the adc reading to a voltage before voltage divider.
Definition adc.c:132
volatile double voltReading
Definition adc.c:20
volatile unsigned int adcResults[NUM_CHANNELS]
Definition adc.h:19
uint32_t countDcf77Messages
Definition dcfReceive.h:49
void wakeUp()
Setup of all GPIO's after wakeup, gets called from ADC10_ISR(void)
Definition gpio.c:79
void sleep()
Setup of all GPIO's after sleep, gets called from ADC10_ISR(void)
Definition gpio.c:90
void changeLedBrightness()

References adcResults, changeLedBrightness(), convertAdc2Voltage(), countDcf77Messages, currentChannel, sleep(), voltReading, and wakeUp().

◆ 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

◆ ADCvar

volatile unsigned int ADCvar

Definition at line 18 of file adc.c.

◆ currentChannel

volatile unsigned char currentChannel = 0

Definition at line 19 of file adc.c.

Referenced by ADC10_ISR().

◆ voltReading

volatile double voltReading

Definition at line 20 of file adc.c.

Referenced by ADC10_ISR().