MATLAB Answers

Arduino Hardware Package for Simulink: If I want to read my encoder Ls7366r via SPI, I only get the value 255

3 views (last 30 days)
Frederik Sappert
Frederik Sappert on 29 Nov 2018
Edited: Frederik Sappert on 29 Nov 2018
I do not understand why it does not work.
I want my encoder counter value from the ls7366r
query via SPI.
The hardware works and is connected correctly because it works in the ArduinoIDE.
Is it correct that I enter the commands in Hex?
Or do I have to change something because I get a 4-byte message back?
For help, I would be very grateful
This is the basic construction I copied from the EEPROM example
This is the one time initialization
This is the Execution Loop
About MDR0 and MDR1 I configure the encoder and with the hex command 60 what is in 01100000 binary asks for the counter status
Here is the working ArduinoIDE code
#include <SPI.h>
#include <Arduino.h>
#define CLR B00000000
#define RD B01000000
#define WR B10000000
#define LOAD B11000000
#define MDR0 B00001000
#define MDR1 B00010000
#define DTR B00011000
#define CNTR B00100000
#define OTR B00101000
#define STR B00110000
// filter factor 1
// async index
// no index
// free-running
// 4x quadrature
#define MDR0_CONF B00000011
// no flag
// enabled
// 32 bits
#define MDR1_CONF B00000000
void setup() {
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
SPI.transfer(WR | MDR0);
digitalWrite(10, HIGH);
digitalWrite(10, LOW);
SPI.transfer(WR | MDR1);
digitalWrite(10, HIGH);
digitalWrite(10, LOW);
SPI.transfer(CLR | CNTR);
digitalWrite(10, HIGH);
long count = 0;
void loop() {
digitalWrite(10, LOW);
byte b = SPI.transfer((byte) RD | CNTR);
count = SPI.transfer(0x00);
count <<= 8;
count |= SPI.transfer(0x00);
count <<= 8;
count |= SPI.transfer(0x00);
count <<= 8;
count |= SPI.transfer(0x00);
digitalWrite(10, HIGH);

Answers (0)




Community Treasure Hunt

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

Start Hunting!