-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADC.c
More file actions
106 lines (97 loc) · 4.54 KB
/
ADC.c
File metadata and controls
106 lines (97 loc) · 4.54 KB
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
101
102
103
104
105
106
// ADC.c
// Runs on TM4C123
// Provide functions that initialize ADC0
// Last Modified: 4/23/23
// Student names: Devin Chaky
// Last modification date: change this to the last modification date or look very silly
// This file is in the inc folder so it automatically will be applied to labs 8 9 and 10
// Labs 8, 9, and 10 specify PD2
#include <stdint.h>
#include "../inc/tm4c123gh6pm.h"
// ADC initialization function using PD2
// Input: none
// Output: none
void ADC_Init(void){
// write this
SYSCTL_RCGCADC_R |= 0x0001; // 1) activate ADC0
SYSCTL_RCGCGPIO_R |= 0x08; // 2) activate clock for Port D
while((SYSCTL_PRGPIO_R&0x08) != 0x08){}; // 3 for stabilization
GPIO_PORTD_DIR_R &= ~0x04; // 4) make PD2 input
GPIO_PORTD_AFSEL_R |= 0x04; // 5) enable alternate function on PE4
GPIO_PORTD_DEN_R &= ~0x04; // 6) disable digital I/O on PE4
GPIO_PORTD_AMSEL_R |= 0x04; // 7) enable analog functionality on PE4
ADC0_PC_R &= ~0xF;
ADC0_PC_R |= 0x1; // 8) configure for 125K samples/sec
ADC0_SSPRI_R = 0x0123; // 9) Sequencer 3 is highest priority
ADC0_ACTSS_R &= ~0x0008; // 10) disable sample sequencer 3
ADC0_EMUX_R &= ~0xF000; // 11) seq3 is software trigger
ADC0_SSMUX3_R &= ~0x000F;
ADC0_SSMUX3_R += 5; // 12) set channel
ADC0_SSCTL3_R = 0x0006; // 13) no TS0 D0, yes IE0 END0
ADC0_IM_R &= ~0x0008; // 14) disable SS3 interrupts
ADC0_ACTSS_R |= 0x0008; // 15) enable sample sequencer 3
// ADC0_SAC_R = 5; // set hardware averaging --> 16-point
}
// Initializes ADC8 and ADC9 sampling
// 125k max sampling
// SS2 triggering event: software trigger, busy-wait sampling
// SS2 1st sample source: Ain9 (PE4)
// SS2 2nd sample source: Ain8 (PE5)
// SS2 interrupts: enabled after 2nd sample but not promoted to controller
void ADC_Init89(void){
volatile uint32_t delay;
// SYSCTL_RCGC0_R |= 0x00010000; // 1) activate ADC0 (legacy code)
SYSCTL_RCGCADC_R |= 0x00000001; // 1) activate ADC0
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4; // 1) activate clock for Port E
delay = SYSCTL_RCGCGPIO_R; // 2) allow time for clock to stabilize
delay = SYSCTL_RCGCGPIO_R;
GPIO_PORTE_DIR_R &= ~0x30; // 3) make PE4 PE5 input
GPIO_PORTE_AFSEL_R |= 0x30; // 4) enable alternate function on PE4 PE5
GPIO_PORTE_DEN_R &= ~0x30; // 5) disable digital I/O on PE4 PE5
// 5a) configure PE4 as ?? (skip this line because PCTL is for digital only)
GPIO_PORTE_PCTL_R = GPIO_PORTE_PCTL_R&0xFF00FFFF;
GPIO_PORTE_AMSEL_R |= 0x30; // 6) enable analog functionality on PE4 PE5
ADC0_PC_R &= ~0xF; // 8) clear max sample rate field
ADC0_PC_R |= 0x1; // configure for 125K samples/sec
ADC0_SSPRI_R = 0x3210; // 9) Sequencer 3 is lowest priority
ADC0_ACTSS_R &= ~0x0004; // 10) disable sample sequencer 2
ADC0_EMUX_R &= ~0x0F00; // 11) seq2 is software trigger
ADC0_SSMUX2_R = 0x0089; // 12) set channels for SS2
ADC0_SSCTL2_R = 0x0060; // 13) no TS0 D0 IE0 END0 TS1 D1, yes IE1 END1
ADC0_IM_R &= ~0x0004; // 14) disable SS2 interrupts
ADC0_ACTSS_R |= 0x0004; // 15) enable sample sequencer 2
}
//------------ADC_In------------
// Busy-wait Analog to digital conversion
// Input: none
// Output: 12-bit result of ADC conversion
// measures from PD2, analog channel 5
uint32_t ADC_In(void){
// 1) initiate SS3
// 2) wait for conversion done
// 3) read result
// 4) acknowledge completion
uint32_t result;
ADC0_PSSI_R = 0x0008; // 1) initiate SS3
while((ADC0_RIS_R&0x08)==0){}; // 2) wait for conversion done
result = ADC0_SSFIFO3_R&0xFFF; // 3) read result
ADC0_ISC_R = 0x0008; // 4) acknowledge completion
return 4095 - result;
}
//------------ADC_In89------------
// Busy-wait Analog to digital conversion
// Input: none
// Output: two 12-bit result of ADC conversions
// Samples ADC8 and ADC9
// 125k max sampling
// software trigger, busy-wait sampling
// data returned by reference
// data[0] is ADC8 (PE5) 0 to 4095
// data[1] is ADC9 (PE4) 0 to 4095
void ADC_In89(uint32_t data[2]){
ADC0_PSSI_R = 0x0004; // 1) initiate SS2
while((ADC0_RIS_R&0x04)==0){}; // 2) wait for conversion done
data[1] = ADC0_SSFIFO2_R&0xFFF; // 3A) read first result
data[0] = ADC0_SSFIFO2_R&0xFFF; // 3B) read second result
ADC0_ISC_R = 0x0004; // 4) acknowledge completion
}