-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.c
55 lines (48 loc) · 1.62 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
/*
* adc.c
*
* Created on: Mar 28, 2023
* Author: jtyost
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <inc/tm4c123gh6pm.h>
#include "lcd.h"
#include "Timer.h"
#include "math.h"
#include "adc.h"
void adc_init(void) {
SYSCTL_RCGCADC_R |= 0x1; // 1) activate ADC0
SYSCTL_RCGCGPIO_R |= 0x2; // 2) activate clock for Port B
while((SYSCTL_PRGPIO_R & 0x2) != 0x2){}; // 3 for stabilization
GPIO_PORTB_DIR_R &= ~0x10; // 4) make PB4 input
GPIO_PORTB_AFSEL_R |= 0x10; // 5) enable alternate function on PB4
GPIO_PORTB_DEN_R &= ~0x10; // 6) disable digital I/O on PB4
GPIO_PORTB_AMSEL_R |= 0x10; // 7) enable analog functionality on PB4
while((SYSCTL_PRADC_R & 0x0001) != 0x0001){};
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 += 10; // 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
}
uint16_t adc_read(void) {
uint32_t result;
ADC0_PSSI_R = 0x0008;
while((ADC0_RIS_R & 0x08) == 0) {};
result = ADC0_SSFIFO3_R & 0xFFF;
ADC0_ISC_R = 0x0008;
return result;
}
double adc_distance(void){
double scan;
double result;
scan = adc_read();
scan = (1/scan);
result = (10.07143 * scan);
return result;
}