-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADC.c
69 lines (58 loc) · 1.76 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
// ADC.c
// Runs on LM4F120/TM4C123
// Provide functions that initialize ADC0
// Last Modified: 5/2/2019
// Student names: Deepanshi Sharma and Danica Corbita
// Last modification date: change this to the last modification date or look very silly
#include <stdint.h>
#include "../inc/tm4c123gh6pm.h"
// ADC initialization function
// Input: none
// Output: none
// measures from PD2, analog channel 5
void ADC_Init(void){ volatile int delay;
//*** students write this ******
SYSCTL_RCGCGPIO_R |= 0x08 ; // enable Port D
delay = 1;
delay = 2;
delay= 3;
delay=4;
delay=5;
delay = 0;
GPIO_PORTD_DIR_R &= ~0x04 ; // Port D is input
GPIO_PORTD_AFSEL_R |= 0x04;
GPIO_PORTD_DEN_R &= ~0x04 ; // disable Port D digital
GPIO_PORTD_AMSEL_R |= 0x04; // enable analog
SYSCTL_RCGCADC_R |= 0x01 ; // enable ADC clock
delay = 1; // stabilize
delay = 2;
delay = 3;
delay = 4;
delay = 5;
delay = 6;
delay = 0;
delay = 0;
delay = 0;
ADC0_PC_R |= 0x01; // 125k HZ priority
ADC0_SSPRI_R = 0x0123; // seq3 is highest priority
ADC0_ACTSS_R &= ~0x08; // disable seq3
ADC0_EMUX_R &= ~0xF000; // seq3 is software trigger
ADC0_SSMUX3_R = (ADC0_SSMUX3_R & 0xFFFFFFF0) + 5; // Ain5, PD2
ADC0_SSCTL3_R = 0x0006;
ADC0_IM_R &= ~0x08;
ADC0_ACTSS_R |= 0x08;
}
//------------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){
//*** students write this ******
uint32_t data;
ADC0_PSSI_R = 0x0008 ; // start ADC
while ((ADC0_RIS_R&0x0008) == 0) {}
data = ADC0_SSFIFO3_R&0xFFF;
ADC0_ISC_R = 0x0008 ;
return data;
}