Raspberry pi AD Converter using mcp3002

2 次查看(过去 30 天)
Alex83
Alex83 2016-12-29
回答: Alex83 2016-12-31
Hello! I am quite new to MATLAB/Simulink connecting to raspberry Pi 2.
My intention is to measure a voltage by a analogue digital converter. In order to do so, I am using a gertboard (the first model). I have already found the examples for usage of the mcp3008 SPI device in the help files of matlab and tried it on the gertboard internal mcp3002 device. It did not work: even if 3.3 V and GND are connected to AD pins of the gertboard it still shows 0 V. Is there any simple method for noobs to get the AD reading running ? Thanks in Advance, Alex

回答(1 个)

Alex83
Alex83 2016-12-31
hello everybody, With a little patience I found a solution:
rpi = raspi('IP-ADRESS', 'pi', 'raspberry');
global A;
clear A;
n=20;
A = zeros(0,0);
global mcp3008;
global data;
global highbits;
global voltage;
for i = 1:n
clear mcp3008;
mcp3008 = spidev(rpi, 'CE0',0);
data = uint16(writeRead(mcp3008,[1, bin2dec('10000000'), 1]));
highbits = bitand(data(2), bin2dec('11'));
voltage = double(bitor(bitshift(highbits, 8), data(3)));
voltage = (3.3/1024)*voltage;
pause(0.1);
A =[A voltage];
end
My problem is now: how do I get this M-File in Simulink for a continuous voltage measurement. I have tried using interpreted Matlab function, but it always says that it didn't found my variables or the commands (for example writeRead). Does anybody know how you can run an M file form Simulink?
For the function block I of course removed "for i = 1:n" and "end" as well as the "rpi = ...." lines.
Thank you for your answers and helpful ideas in advance. Alex

类别

Help CenterFile Exchange 中查找有关 Raspberry Pi Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by