-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADC.c
100 lines (92 loc) · 4.41 KB
/
ADC.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// ADCSWTrigger.c
// Runs on TM4C123
// Provide functions that initialize ADC0 SS3 to be triggered by
// software and trigger a conversion, wait for it to finish,
// and return the result.
// Daniel Valvano
// August 6, 2015
/* This example accompanies the book
"Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers",
ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2015
Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file
as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/
*/
#include "simplelink.h"
#include "ADC.h"
#include "Soul.h"
#include "Butts.h"
#include "../inc/tm4c123gh6pm.h"
#define CALIBRATION 800
void DisableInterrupts(void); // Disable interrupts
// There are many choices to make when using the ADC, and many
// different combinations of settings will all do basically the
// same thing. For simplicity, this function makes some choices
// for you. When calling this function, be sure that it does
// not conflict with any other software that may be running on
// the microcontroller. Particularly, ADC0 sample sequencer 3
// is used here because it only takes one sample, and only one
// sample is absolutely needed. Sample sequencer 3 generates a
// raw interrupt when the conversion is complete, but it is not
// promoted to a controller interrupt. Software triggers the
// ADC0 conversion and waits for the conversion to finish. If
// somewhat precise periodic measurements are required, the
// software trigger can occur in a periodic interrupt. This
// approach has the advantage of being simple. However, it does
// not guarantee real-time.
//
// A better approach would be to use a hardware timer to trigger
// the ADC0 conversion independently from software and generate
// an interrupt when the conversion is finished. Then, the
// software can transfer the conversion result to memory and
// process it after all measurements are complete.
// This initialization function sets up the ADC according to the
// following parameters. Any parameters not explicitly listed
// below are not modified:
// Max sample rate: <=125,000 samples/second
// Sequencer 0 priority: 1st (highest)
// Sequencer 1 priority: 2nd
// Sequencer 2 priority: 3rd
// Sequencer 3 priority: 4th (lowest)
// SS3 triggering event: software trigger
// SS3 1st sample source: Ain9 (PE4)
// SS3 interrupts: enabled but not promoted to controller
void ADC_Init(void){
SYSCTL_RCGCADC_R |= 0x0001; // 7) activate ADC0
// 1) activate clock for Port E
SYSCTL_RCGCGPIO_R |= 0x10;
while((SYSCTL_PRGPIO_R&0x10) != 0x10){};
GPIO_PORTE_DIR_R &= ~0x20; // 2) make PE4 input
GPIO_PORTE_AFSEL_R |= 0x20; // 3) enable alternate function on PE4
GPIO_PORTE_DEN_R &= ~0x20; // 4) disable digital I/O on PE4
GPIO_PORTE_AMSEL_R |= 0x20; // 5) enable analog functionality on PE4
ADC0_PC_R &= ~0xF; // 7) clear max sample rate field
ADC0_PC_R |= 0x1; // configure for 125K samples/sec
ADC0_SSPRI_R = 0x0123; // 8) Sequencer 3 is highest priority
ADC0_ACTSS_R &= ~0x0008; // 9) disable sample sequencer 3
ADC0_EMUX_R &= ~0xF000; // 10) seq3 is software trigger
ADC0_SSMUX3_R &= ~0x000F; // 11) clear SS3 field
ADC0_SSMUX3_R += 8; // set channel
ADC0_SSCTL3_R = 0x0006; // 12) no TS0 D0, yes IE0 END0
ADC0_IM_R &= ~0x0008; // 13) disable SS3 interrupts
ADC0_ACTSS_R |= 0x0008; // 14) enable sample sequencer 3
}
uint32_t sampleCount = 0;
int32_t sampleArray[100];
int32_t sampleAvg = 0;
uint32_t ADC0_InSeq3(void) {
uint32_t result;
ADC0_PSSI_R = 0x0008; // 1) initiate SS3
while((ADC0_RIS_R&0x08)==0){}; // 2) wait for conversion done
// if you have an A0-A3 revision number, you need to add an 8 usec wait here
result = ADC0_SSFIFO3_R&0xFFF; // 3) read result
ADC0_ISC_R = 0x0008; // 4) acknowledge completion
return result;
}