Last active
October 3, 2015 17:47
-
-
Save nodtem66/bdf53f052b9ce3f5f8be to your computer and use it in GitHub Desktop.
ADC Serial print raw 16 bit binary. for MATLAB processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clc; close all; clear; | |
LIMIT = 500; | |
N_CHANNEL = 6; | |
% clear opened usb port | |
if ~isempty(instrfind) | |
fclose(instrfind); | |
delete(instrfind); | |
end | |
% open serial port sent by arduino | |
arduino = serial('COM36', 'BaudRate', 19200); | |
running = true; | |
is_start = false; | |
temp = -1; | |
current_channel = 1; | |
x = zeros(LIMIT+1, N_CHANNEL); | |
t = length(x); | |
timeout = tic; | |
delay_plot = tic; | |
fopen(arduino); | |
% loop read value from arduino | |
while running | |
% if cannot read any signal in 6 seconds, stop the loop. | |
if toc(timeout) > 6 | |
fprintf('timeout!!\n'); | |
break; | |
end | |
% read bytes everytime buffer has more than 40bytes | |
if arduino.BytesAvailable > 40 | |
val = fread(arduino); | |
% join the old value from previous iteration to array (val) | |
if (temp >= 0) | |
val = [temp;val]; | |
temp = -1; | |
end | |
% header detection; detect 0xABCD and deny all data before them. | |
if (~is_start) | |
index = strfind(val, [171,205]); | |
% if header found cut ony data behind them | |
if (~isempty(index)) | |
val = val(index(1)+2:end); | |
is_start = true; | |
fprintf('start!!\n'); | |
% play start sound | |
[sound,sampling] = wavread('C:\\Users\\Chang\\Desktop\\Dropbox\\work\\chang emg 8ch\start'); | |
wavplay(sound(1:end),sampling*3) ; | |
else | |
continue; | |
end | |
end | |
q=q+1; | |
len = length(val); | |
if mod(len, 2) == 1 | |
temp = val(end); | |
val(end) = []; | |
len = len - 1; | |
end | |
val = reshape(val, 2, len/2)'; | |
val = sum(val * [256;1], 2); | |
% pre-addition | |
if (current_channel ~= 1) | |
x(end, current_channel:N_CHANNEL) = ... | |
val(1:N_CHANNEL-current_channel+1); | |
val = val(N_CHANNEL-current_channel+2:end); | |
current_channel = 1; | |
end | |
len = length(val); | |
i_end = mod(len, N_CHANNEL); | |
end_val = val(end-i_end+1:end); | |
val = val(1:end-i_end); | |
% main-addition | |
if x(1,:)==0 | |
x = reshape(val, N_CHANNEL, floor(len/N_CHANNEL))'; | |
end | |
x = [x;reshape(val, N_CHANNEL, floor(len/N_CHANNEL))']; | |
% post-addition | |
if (i_end ~= 0) | |
x = [x; ones(1, N_CHANNEL)*5555]; | |
x(end, 1:i_end) = end_val'; | |
current_channel = current_channel + i_end; | |
end | |
data=x; | |
[size_dataM size_dataN]=size(data); | |
if data(size_dataM,size_dataN)==5555 | |
data1=data; | |
data = zeros(size_dataM-1,size_dataN); | |
data=data1(1:size_dataM-1,:); | |
end | |
timeout = tic; | |
end | |
end | |
% close serial port | |
fclose(arduino); | |
delete(arduino); | |
close all; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define PIN_KEY 9 | |
#define PIN_RESET 10 | |
// Arrays to save our results in | |
unsigned long val,val1,val2,val3,val4,val5; | |
unsigned long time = 0; | |
// Define various ADC prescaler | |
const unsigned char PS_16 = (1 << ADPS2); | |
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0); | |
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1); | |
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); | |
// Setup the serial port and pin 2 | |
void setup() { | |
ADCSRA &= ~PS_128; | |
ADCSRA |= PS_64; | |
pinMode(A0, INPUT); | |
pinMode(A1, INPUT); | |
pinMode(A2, INPUT); | |
pinMode(A3, INPUT); | |
pinMode(A4, INPUT); | |
pinMode(A5, INPUT); | |
Serial.begin(19200); | |
Serial.write(0xAB); | |
Serial.write(0xCD); | |
} | |
void loop() { | |
val = analogRead(A0); | |
val1 = analogRead(A1); | |
val2 = analogRead(A2); | |
val3 = analogRead(A3); | |
val4 = analogRead(A4); | |
val5 = analogRead(A5); | |
Serial.write(val >> 8); | |
Serial.write(val & 0xFF); | |
Serial.write(val1 >> 8); | |
Serial.write(val1 & 0xFF); | |
Serial.write(val2 >> 8); | |
Serial.write(val2 & 0xFF); | |
Serial.write(val3 >> 8); | |
Serial.write(val3 & 0xFF); | |
Serial.write(val4 >> 8); | |
Serial.write(val4 & 0xFF); | |
Serial.write(val5 >> 8); | |
Serial.write(val5 & 0xFF); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment