-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadc.c
More file actions
83 lines (70 loc) · 1.98 KB
/
adc.c
File metadata and controls
83 lines (70 loc) · 1.98 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
/*
* adc.c
*
* Created on: May 5, 2017
* Author: Zack Teasdale, Ian Taylor
*/
#include "adc.h"
// GPIO variables
uintptr_t ctrl_handle;
uintptr_t data_handle_a;
uintptr_t data_handle_b;
// GPIO setup
void gpio_setup(void){
int permission_err;
// Setup GPIO
permission_err = ThreadCtl(_NTO_TCTL_IO, NULL);
if(permission_err == -1){
fprintf(stderr, "Can't get root permissions\n");
return;
}
// Map handle for control register, data register, and setup data register for output
ctrl_handle = mmap_device_io(1, GPIO_CONTROL_ADDRESS);
out8(ctrl_handle, 0x00);
data_handle_a = mmap_device_io(1, GPIO_A_ADDRESS);
data_handle_b = mmap_device_io(1, GPIO_B_ADDRESS);
}
// ADC setup
void adc_setup(void){
// set input channel to channel 1
out8(BASE+2, 0x11);
// set input range to +/- 10V with resolution 305uV and gain of 1
out8(BASE+3, 0x00);
// set polarity to bipolar
out8(BASE+1, 2);
out8(BASE+13, 0x00);
}
void check_adc(void){
uint8_t AD_LSB;
uint8_t AD_MSB;
// Actually capture into ADC
// wait for analog input circuit to settle
while(in8(BASE+3) & 0x20); // wait for ADWAIT to go low,BASE+3 bit 5
// perform a/d conversion on current channel
out8(BASE, 0x80);
// wait for conversion to finish
//while(in8(BASE+3) & 0x80); // wait for ADBUSY to go low,BASE+3 bit 7
check_adc_status();
// read data from the board
AD_LSB = in8(BASE);
AD_MSB = in8(BASE+1);
//ad = (int16_t) ((AD_MSB * 256) + AD_LSB);
//ad = AD_MSB;
//ad_out = ad >> 8;
// send data out via port A and port B
// A is MSByte, B is LSByte
if(AD_MSB < 128) {
out8(data_handle_a, (127 + (AD_MSB)));
printf("%d\n", (127 + (AD_MSB)));
} else {
out8(data_handle_a, (AD_MSB) - 127);
printf("%d\n", (AD_MSB) - 127);
}
}
// OR USE
int check_adc_status(void){
int i;
for (i = 0; i < 20000; i++)
if(!(in8(BASE+3) & 0x80)) return(0); // conversion completed
return(-1); // conversion did not complete
}