MATLAB Answers

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

4 views (last 30 days)
Frederik Sappert
Frederik Sappert on 29 Nov 2018
Edited: Frederik Sappert on 29 Nov 2018
Hello
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() {
Serial.begin(9600);
SPI.begin();
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
SPI.transfer(WR | MDR0);
SPI.transfer(MDR0_CONF);
digitalWrite(10, HIGH);
digitalWrite(10, LOW);
SPI.transfer(WR | MDR1);
SPI.transfer(MDR1_CONF);
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);
Serial.print(count);
Serial.println();
delay(100);
}

  0 Comments

Sign in to comment.

Answers (0)

Sign in to answer this question.

Products


Release

R2018a