Hi guys and girls,
Hopefully you can help me. Ive been trying all day to solve this and i think im currently stuck.
Ive got a teensy 3.5 on spi1 linked to a LS7366R and it should have a 4 byte counter. but no matter what i do it only ever seems to count on the first 2 bytes and nothing more (other than rolling over). Does anyone have any experience with this chip to tell me where im going wrong at all?
Ive tried lots of different examples and they all act exactly the same, this is the values coming back from each of the 4 bytes returned and no matter what the first two stay at 0, 0 or 255, 255 if it rolls over.
Thanks in advance Trev
Hopefully you can help me. Ive been trying all day to solve this and i think im currently stuck.
Ive got a teensy 3.5 on spi1 linked to a LS7366R and it should have a 4 byte counter. but no matter what i do it only ever seems to count on the first 2 bytes and nothing more (other than rolling over). Does anyone have any experience with this chip to tell me where im going wrong at all?
Ive tried lots of different examples and they all act exactly the same, this is the values coming back from each of the 4 bytes returned and no matter what the first two stay at 0, 0 or 255, 255 if it rolls over.
Code:
255, 255, 4, 224 Reading, 4294903008 Status, 4
255, 255, 3, 20 Reading, 4294902548 Status, 4
255, 255, 2, 13 Reading, 4294902285 Status, 4
255, 255, 0, 9 Reading, 4294901769 Status, 4
255, 255, 254, 9 Reading, 4294966793 Status, 4 << why did this happen its like the first two never change
255, 255, 252, 15 Reading, 4294966287 Status, 4
255, 255, 250, 11 Reading, 4294965771 Status, 4
Code:
#include <SPI.h>
#define SS1 24
// These hold the current encoder count.
signed long encoder1count = 0;
void initEncoders() {
// Set slave selects as outputs
pinMode(SS1, OUTPUT);
// Raise select pins
// Communication begins when you drop the individual select signsl
digitalWrite(SS1, HIGH);
SPI1.begin();
// Initialize encoder 1
// Clock division factor: 0
// Negative index input
// free-running count mode
// x4 quatrature count mode (four counts per quadrature cycle)
// NOTE: For more information on commands, see datasheet
digitalWrite(SS1, LOW); // Begin SPI conversation
SPI1.transfer(0x88); // Write to MDR0
SPI1.transfer(0x03); // Configure to 4 byte mode
digitalWrite(SS1, HIGH); // Terminate SPI conversation
}
long readEncoder() {
// Initialize temporary variables for SPI read
unsigned int count_1, count_2, count_3, count_4;
long count_value;
digitalWrite(SS1, LOW); // Begin SPI conversation
SPI1.transfer(0x60); // Request count
count_1 = SPI1.transfer(0x00); // Read highest order byte
count_2 = SPI1.transfer(0x00);
count_3 = SPI1.transfer(0x00);
count_4 = SPI1.transfer(0x00); // Read lowest order byte
digitalWrite(SS1, HIGH); // Terminate SPI conversation
// Calculate encoder count
count_value = (count_1 << 8) + count_2;
count_value = (count_value << 8) + count_3;
count_value = (count_value << 8) + count_4;
Serial.print(count_1);
Serial.print(" ");
Serial.print(count_2);
Serial.print(" ");
Serial.print(count_3);
Serial.print(" ");
Serial.print(count_4);
Serial.print("\n");
return count_value;
}
void clearEncoderCount() {
// Set encoder1's data register to 0
digitalWrite(SS1, LOW); // Begin SPI conversation
// Write to DTR
SPI1.transfer(0x98);
// Load data
SPI1.transfer(0x00); // Highest order byte
SPI1.transfer(0x00);
SPI1.transfer(0x00);
SPI1.transfer(0x00); // lowest order byte
digitalWrite(SS1, HIGH); // Terminate SPI conversation
delayMicroseconds(100); // provides some breathing room between SPI conversations
// Set encoder1's current data register to center
digitalWrite(SS1, LOW); // Begin SPI conversation
SPI1.transfer(0xE0);
digitalWrite(SS1, HIGH); // Terminate SPI conversation
}
void setup() {
Serial.begin(9600); // Serial com for data output
initEncoders(); Serial.println("Encoders Initialized...");
clearEncoderCount(); Serial.println("Encoders Cleared...");
}
void loop() {
delay(200);
// Retrieve current encoder counters
encoder1count = readEncoder();
Serial.print("Enc1: "); Serial.print(encoder1count);
Serial.println();
}
Thanks in advance Trev