Flipklok
 
Loading...
Searching...
No Matches
adc.c
Go to the documentation of this file.
1/*
2 * adc.c
3 *
4 * Created on: 21 mrt. 2025
5 */
10
11#include "adc.h"
12#include "led.h"
13#include "gpio.h"
14#include "uart.h"
15#include "timer.h"
16#include "dcfReceive.h"
17
18volatile unsigned int ADCvar;
19volatile unsigned char currentChannel = 0;
20volatile double voltReading;
21
26#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
27#pragma vector = ADC10_VECTOR
28__interrupt void ADC10_ISR(void)
29#elif defined(__GNUC__)
30void __attribute__ ((interrupt(ADC10_VECTOR))) ADC10_ISR (void)
31#else
32#error Compiler not supported!
33#endif
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}
82
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}
115
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}
126
132double convertAdc2Voltage(int R1, int R2, int adcReading)
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}
volatile unsigned char currentChannel
Definition adc.c:19
void startAdcConv()
Start the ADC conversion'(s), gets called when end interrupt of ISR_TA0(void) is reached.
Definition adc.c:119
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 ADCvar
Definition adc.c:18
__interrupt void ADC10_ISR(void)
ADC interrupt routine, gets called when end interrupt of ISR_TA0(void) is reached via startAdcConv().
Definition adc.c:28
void setupAdc()
Setup ADC, get called in startup from main(), Shortest samling rate, with 2.5V reference,...
Definition adc.c:86
volatile unsigned int adcResults[NUM_CHANNELS]
Definition adc.h:19
#define adcMax
Definition adc.h:17
#define referenceVoltage
Definition adc.h:16
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()