How to connect Raspberry Pi to ADC MCP 3008 using new SPI Simulink blocks?

25 ビュー (過去 30 日間)
I want to interface a RaspberryPi with an ADC MCP3008 using Simulink, in particular using the new block "SPI Register Read" available in the latest Simulink version (without writing C code or Matlab code). In this block is required a Register Address where SPI has to read the value from the slave. I don't know how to find it because I'm not sure if it's a MCP3008 register or it's an address always used from RPi to communicate with all the slaves. Can anyone help me?
  2 件のコメント
Bruder muss los
Bruder muss los 2022 年 8 月 10 日
Hi Pietro, as far as I understand it, the MCP3008 has no registers in that sense, so you can't use the "register read"-block for this particular chip.
Instead, as Sharan proposed, I used the "master transfer"-block and took a few more nights to figure out the rest of the required logic. Since the "master transfer"-block gives you three output Bytes separately, but in one signal path, you have to dearrange the Bytes (via "selector"-blocks) and add the significant Bytes to get the desired 10-Bit value from the SPI Bus.
I hope this arrangement does the trick for you, too.
Raspberry Pi Simulink MCP3008 SPI Bus
Madhura Pabudanan
Madhura Pabudanan 2023 年 7 月 19 日
I got the data from MCP 3008 from this method. I have an issue with the sample rate. I can not achive maximum sample rate from this method. How I overcome this problem ?

サインインしてコメントする。

採用された回答

Sharan Paramasivam Murugesan
Sharan Paramasivam Murugesan 2017 年 8 月 21 日
SPI Register read/write blocks are meant for memory devices such as EEPROM.
So in your case, following the example,
try using "SPI Master Transfer" block with a constant block having value [1, bin2dec('10000000'), 0] as input. Here in the binary value '10000000', the first 4 bits '1000' represents single-ended voltage measurement at CH0 is requested (refer to the datasheet for more info). To read from CH1, then '1001' should be used. To retrieve the voltage value from the output, say 'data', you have to model the logic mentioned in the above doc (also mentioned below) using Simulink blocks.
highbits = bitand(data(2), bin2dec('11'));
voltage = double(bitor(bitshift(highbits, 8), data(3)));
voltage = (3.3/1024) * voltage;
  1 件のコメント
Grissa Mohamed Amine
Grissa Mohamed Amine 2018 年 1 月 6 日
i did the same thing with mcp3208 and i customize the code but it does not work, do you have another Solution please ?

サインインしてコメントする。

その他の回答 (3 件)

Bikash Sah
Bikash Sah 2018 年 7 月 10 日
Hai Sharan, can you please explain the use of a constant block having value [1, bin2dec('10000000'), 0] as input to SPI Master Transfer. You wrote about the first four numeric values to control bits; what about the other four? What does the value "1" and "0" corresponds to. Also, a better explanation to understand the following code would be a great help.
clear mcp3008
mcp3008 = spidev(rpi, 'CE0');
data = uint16(writeRead(mcp3008,[1, bin2dec('10000000'), 0]));
highbits = bitand(data(2), bin2dec('11'));
voltage = double(bitor(bitshift(highbits, 8), data(3)));
voltage = (3.3/1024) * voltage;

Johannes Börner
Johannes Börner 2018 年 7 月 31 日
Hi, Im currently trying to read the data of an MCP3008 in the way described above. Though I can manage the readings from command line, I have no indication that the SPI Master Transfer is working in any way when running the model. Is there any Simulink example using this block? Did anyone recently manage to get readings from an MCP3008?
Kind regards
  1 件のコメント
Kiyo Akabori
Kiyo Akabori 2019 年 11 月 7 日
For me, placing a Data Type Conversion block of uint8 between the constant block of [1, bin2dec('10000000'), 0] and SPI Master Transfer block did the trick. Without the conversion block, an array of type double was fed to the SPI master block, which ended up outputing values very close to zero (about 1e-300).

サインインしてコメントする。


Abdo Be
Abdo Be 2019 年 6 月 18 日
Hi, After multiple trials to read data from MCP3008 using SPI read block, it finally works for the first analog input (CH0),
However, in order to read analog input from the other channels (CH1,..CH7), normally the SPI write block should be driven by a constant block, I want to know the source of this synthax "[1, bin2dec('10000000'), 0]" that Sharan Paramasivam Murugesan mentionned above, because when I tested this block it doesn't change the analog input.
  7 件のコメント
Bruder muss los
Bruder muss los 2022 年 8 月 10 日
編集済み: Bruder muss los 2022 年 8 月 10 日
Hey folks, as far as I understand it, the MCP3008 has no registers in that sense, so you can't use the "register read"-block for this particular chip.
Instead, as Sharan proposed, I used the "master transfer"-block and took a few more nights to figure out the rest of the required logic. Since the "master transfer"-block gives you three output Bytes separately, but in one signal path, you have to dearrange the Bytes (via "selector"-blocks) and add the significant Bytes to get the desired 10-Bit value from the SPI Bus.
I hope this arrangement does the trick for you, too.
Raspberry Pi Simulink MCP3008 SPI Bus
Madhura Pabudanan
Madhura Pabudanan 2023 年 7 月 20 日
I got the data from MCP 3008 from this method. I have an issue with the sample rate. I can not achive maximum sample rate from this method. How I overcome this problem ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRaspberry Pi Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by