-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathADC_live_plot.c
101 lines (70 loc) · 2.25 KB
/
ADC_live_plot.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
/********************** ADC Live Plotting & Logging Code ****************************
Sends ADC value from pin F0 in hex from the M2 microcontroller [http://medesign.seas.upenn.edu/index.php/Guides/MaEvArM].
By Nick McGill [nmcgill.com]
************************************************************************************/
// header files
#include "m_general.h"
#include "m_usb.h"
// subroutines
void set_ADC(void);
void update_ADC(void);
// global variables
#define DEBUG 1
#define CLOCK 0
int f0val = 0;
int main(void){
m_red(ON);
m_green(ON);
if (DEBUG){
m_usb_init(); // connect usb
while(!m_usb_isconnected()){}; //wait for connection
}
m_red(OFF);
m_green(OFF);
char rx_buffer; //computer interactions
set_ADC();
while(1){
update_ADC();
while(!m_usb_rx_available()); //wait for an indication from the computer
rx_buffer = m_usb_rx_char(); //grab the computer packet
m_usb_rx_flush(); //clear buffer
if(rx_buffer == 1) { //computer wants ir data
//write ir data as concatenated hex: i.e. f0f1f4f5
m_usb_tx_hex(f0val);
m_usb_tx_char('\n'); //MATLAB serial command reads 1 line at a time
}
if (ADC > 512){
m_green(ON);
m_red(OFF);
}
else{
m_red(ON);
m_green(OFF);
}
}
}
//_______________________________________ Subroutine for setting ADCs
void set_ADC(void){
//****************** set ADC values
clear(ADMUX, REFS1); // voltage Reference - set to VCC
set(ADMUX, REFS0); // ^
//clear(ADMUX, REFS1); // voltage Reference - set to Vref, the Aref pin, 3.4V
//clear(ADMUX, REFS0); // ^
set(ADCSRA, ADPS2); // set the ADC clock prescaler, divide 16 MHz by 128 (set, set, set)
set(ADCSRA, ADPS1); // ^
set(ADCSRA, ADPS0); // ^
set(DIDR0, ADC0D); // disable the f0 digital input
}
//_______________________________________ Subroutine for updating ADCs
void update_ADC(){ //update to current ADC values, set to ADC_F0, B4
//-------------------> set pin F0 to read ADC values
clear(ADCSRB, MUX5); // single-ended channel selection
clear(ADMUX, MUX2); // ^
clear(ADMUX, MUX1); // ^
clear(ADMUX, MUX0); // ^
set(ADCSRA, ADEN); // start conversion process
set(ADCSRA, ADSC); // ^
while(!check(ADCSRA,ADIF));
f0val = ADC;
set(ADCSRA, ADIF); // sets flag after conversion
}