Arduino IDE code to simulink for SPI communication

5 views (last 30 days)
Hello,
I have arduino IDE program to read the data from a sensor in SPI. The code works perfectly well with IDE. I want to use simulink to do the same job. I checked on mathworks website and documentation, but did not any help from that. Please help me to do the same job in simulink.
For your clear understanding of my task, I am uploading my IDE program below:
%% My IDE program for task
#include <SPI.h>
int chipSelectPin1=10;
int chipSelectPin2=9;
int chipSelectPin3=8;
//end calibration values
//////////////////////////////////////////////
//*****************************************************
void setup()
//*****************************************************
{
Serial.begin(9600);
pinMode(chipSelectPin1, OUTPUT);
pinMode(chipSelectPin2, OUTPUT);
pinMode(chipSelectPin3, OUTPUT);
digitalWrite(chipSelectPin1, HIGH);
digitalWrite(chipSelectPin2, HIGH);
digitalWrite(chipSelectPin3, HIGH);
LS7366_Init();
delay(100);
}
//*****************************************************
void loop()
//*****************************************************
{
long encoder1Value;
long encoder2Value;
long encoder3Value;
encoder1Value = getEncoderValue(1);
Serial.print("Encoder X= ");
Serial.print(encoder1Value);
encoder2Value = getEncoderValue(2);
Serial.print(" Encoder Y= ");
Serial.print(encoder2Value);
encoder3Value = getEncoderValue(3);
Serial.print(" Encoder Z= ");
Serial.print(encoder3Value);
Serial.print("\r\n");
delay(100);
}//end loop
//*****************************************************
long getEncoderValue(int encoder)
//*****************************************************
{
unsigned int count1Value, count2Value, count3Value, count4Value;
long result;
selectEncoder(encoder);
SPI.transfer(0x60); // Request count
count1Value = SPI.transfer(0x00); // Read highest order byte
count2Value = SPI.transfer(0x00);
count3Value = SPI.transfer(0x00);
count4Value = SPI.transfer(0x00); // Read lowest order byte
deselectEncoder(encoder);
result= ((long)count1Value<<24) + ((long)count2Value<<16) + ((long)count3Value<<8) + (long)count4Value;
return result;
}//end func
//*************************************************
void selectEncoder(int encoder)
//*************************************************
{
switch(encoder)
{
case 1:
digitalWrite(chipSelectPin1,LOW);
break;
case 2:
digitalWrite(chipSelectPin2,LOW);
break;
case 3:
digitalWrite(chipSelectPin3,LOW);
break;
}//end switch
}//end func
//*************************************************
void deselectEncoder(int encoder)
//*************************************************
{
switch(encoder)
{
case 1:
digitalWrite(chipSelectPin1,HIGH);
break;
case 2:
digitalWrite(chipSelectPin2,HIGH);
break;
case 3:
digitalWrite(chipSelectPin3,HIGH);
break;
}//end switch
}//end func
// LS7366 Initialization and configuration
//*************************************************
void LS7366_Init(void)
//*************************************************
{
// SPI initialization
SPI.begin();
//SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI at 1Mhz (on 16Mhz clock)
delay(10);
digitalWrite(chipSelectPin1,LOW);
SPI.transfer(0x88);
SPI.transfer(0x03);
digitalWrite(chipSelectPin1,HIGH);
digitalWrite(chipSelectPin2,LOW);
SPI.transfer(0x88);
SPI.transfer(0x03);
digitalWrite(chipSelectPin2,HIGH);
digitalWrite(chipSelectPin3,LOW);
SPI.transfer(0x88);
SPI.transfer(0x03);
digitalWrite(chipSelectPin3,HIGH);
}//end func
Thank you,
Please help !
  2 Comments
Aishwarya
Aishwarya on 8 Nov 2023
Edited: Aishwarya on 8 Nov 2023
Hi Aakash,
Can you provide more information about what have you tried on Simulink and what did not work.
aakash dewangan
aakash dewangan on 9 Nov 2023
I did Not get clear idea about the implementation of SPI communication in simulink after reading the mathworks document. Please let me know If you have suggestions.
Thank you,

Sign in to comment.

Answers (1)

Aishwarya
Aishwarya on 9 Nov 2023
Hi Aakash,
As per my understanding, you wish to receive data from LS7366R in SPI mode from Arduino through Simulink.
The MathWorks documentation below provides an example implementation for communicating with SPI based EEPROM using Arduino: https://www.mathworks.com/help/supportpkg/arduino/ref/communicating-with-an-spi-based-eeprom-using-arduino-hardware.html
Here are some suggestions that might help customize the above example to other applications:
1. SPI WriteRead block: It takes vector of bytes (typically specified by uint_8 data types) for data input. Typically, this vector consists of three parts:
  • Opcode - what is the operation the device will be doing.
  • Register - what part of memory will the device be operating on.
  • Data - what will the device be writing, or how many bytes of data will be read.
The details that define these parts depend on the specific device to which we are communicating with.
2. Output of “SPI WriteRead” Block:
  • It is vector of same type and size as input to the block.
  • The first few bytes will be a repeat of the opcode and register. The next few bytes depend on if it was a read or a write operation.
  • Write operations repeat the input data.
  • Read operations replace the input zeros with the data held in the register.
4. Make sure the opcodes and registers are the right byte values. It is typically best to send them as "uint_8" decimal numbers.
5. Make sure the SS pin is set to the right pin number.
6. Make sure Simulink is setup for the correct hardware board.
7. Check that SPI settings are correct in Configuration Parameters > Hardware Implementation under Target Hardware Resources:
SPI clock out frequency:
  • Check device datasheet to see what is supported.
  • Try lower frequencies if possible (sometimes, signal lines themselves do not support high frequencies).
  • If having timing issues, try changing the impedance of the line.
SPI Mode: Check device datasheet to see what is supported.
Bit Order: Usually, it is MSB first.
I hope this help!

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!