Scott Atta
New member
I've used this code on an Arduino UNO and it works perfectly and outputs a 12 bit value from 0-4095 via serial.print.
When I load it into my Teensy 3.1 I get a erratic stream of values, see screenshot.
Rotating the Encoder position doesn't affect the Serial.print values.
CUI Amt203 datasheet.
Can someone please point out what I'm doing wrong???
#include <SPI.h>
const int CS = 15; //Chip or Slave select
uint16_t ABSposition = 0;
uint16_t ABSposition_last = 0;
uint8_t temp[2];
void setup()
{
pinMode(CS,OUTPUT);//Slave Select
digitalWrite(CS,HIGH);
SPI.begin();
Serial.begin(9600);
delay(1000);
Serial.println("starting");
}
uint8_t SPI_T (uint8_t msg) //Repetive SPI transmit sequence
{
uint8_t msg_temp = 0; //vairable to hold recieved data
digitalWrite(CS,LOW); //select spi device
msg_temp = SPI.transfer(msg); //send and recieve
digitalWrite(CS,HIGH); //deselect spi device
return(msg_temp); //return recieved byte
}
void loop()
{
uint8_t recieved = 0xA5; //just a temp vairable
ABSposition = 0; //reset position vairable
SPI.begin(); //start transmition
digitalWrite(CS,LOW);
SPI_T(0x10); //issue read command
recieved = SPI_T(0x00); //issue NOP to check if encoder is ready to send
while (recieved != 0x10) //loop while encoder is not ready to send
{
recieved = SPI_T(0x00); //check again if encoder is still working
delay(20); //wait a bit
}
temp[0] = SPI_T(0x00); //Recieve MSB
temp[1] = SPI_T(0x00); // recieve LSB
digitalWrite(CS,HIGH); //just to make sure
SPI.end(); //end transmition
temp[0] &=~ 0xF0; //mask out the first 4 bits
ABSposition = temp[0] << 8; //shift MSB to correct ABSposition in ABSposition message
ABSposition += temp[1]; // add LSB to ABSposition message to complete message
if (ABSposition != ABSposition_last) //if nothing has changed dont wast time sending position
{
ABSposition_last = ABSposition; //set last position to current position
Serial.println(ABSposition); //send position in degrees
}
delay(10); //wait a bit till next check
}
When I load it into my Teensy 3.1 I get a erratic stream of values, see screenshot.
Rotating the Encoder position doesn't affect the Serial.print values.
CUI Amt203 datasheet.
Can someone please point out what I'm doing wrong???
#include <SPI.h>
const int CS = 15; //Chip or Slave select
uint16_t ABSposition = 0;
uint16_t ABSposition_last = 0;
uint8_t temp[2];
void setup()
{
pinMode(CS,OUTPUT);//Slave Select
digitalWrite(CS,HIGH);
SPI.begin();
Serial.begin(9600);
delay(1000);
Serial.println("starting");
}
uint8_t SPI_T (uint8_t msg) //Repetive SPI transmit sequence
{
uint8_t msg_temp = 0; //vairable to hold recieved data
digitalWrite(CS,LOW); //select spi device
msg_temp = SPI.transfer(msg); //send and recieve
digitalWrite(CS,HIGH); //deselect spi device
return(msg_temp); //return recieved byte
}
void loop()
{
uint8_t recieved = 0xA5; //just a temp vairable
ABSposition = 0; //reset position vairable
SPI.begin(); //start transmition
digitalWrite(CS,LOW);
SPI_T(0x10); //issue read command
recieved = SPI_T(0x00); //issue NOP to check if encoder is ready to send
while (recieved != 0x10) //loop while encoder is not ready to send
{
recieved = SPI_T(0x00); //check again if encoder is still working
delay(20); //wait a bit
}
temp[0] = SPI_T(0x00); //Recieve MSB
temp[1] = SPI_T(0x00); // recieve LSB
digitalWrite(CS,HIGH); //just to make sure
SPI.end(); //end transmition
temp[0] &=~ 0xF0; //mask out the first 4 bits
ABSposition = temp[0] << 8; //shift MSB to correct ABSposition in ABSposition message
ABSposition += temp[1]; // add LSB to ABSposition message to complete message
if (ABSposition != ABSposition_last) //if nothing has changed dont wast time sending position
{
ABSposition_last = ABSposition; //set last position to current position
Serial.println(ABSposition); //send position in degrees
}
delay(10); //wait a bit till next check
}
Attachments
Last edited: