带有绝对编码器的SPI Arduino接口



我正在尝试从RLS检索数据绝对旋转编码器通过SPI通过Arduino Uno。我已经设法学习了SPI的基础知识,但我似乎无法做到这一点。我一直打印传输数据,结果一直是255。

我也使用Arduino Uno的推荐引脚:https://www.arduino.cc/en/reference/SPI

这里是绝对编码器数据表的链接:https://resources.renishaw.com/en/details/data-sheet-orbis-true-absolute-rotary-encoder--97180

任何帮助都是感激的。

这是我的代码,我一直在尝试:

#include <SPI.h>
unsigned int data;
int CS = 10; //Slave Select Pin
// The SS pin starts communication when pulled low and stops when high
void setup() {
Serial.begin(9600);
RTC_init();
}
int RTC_init() {
pinMode(CS, OUTPUT);
SPI.begin();
SPI.setBitOrder(LSBFIRST); // Sets Bit order according to data sheet (LSBFIRST)
SPI.setDataMode(SPI_MODE2); // 2 or 3, Not sure (I have tried both)
digitalWrite(CS, LOW); // Start communications
/* 
Commands List:
Command "1" (0x31) – position request (total: 3 bytes) + 4 for multi-turn
Command "3" (0x33) – short position request (total: 2 bytes) + 4 for multi-turn
Command "d" (0x64) – position request + detailed status (total: 4 bytes) + 4 for multi-turn
Command "t" (0x74) – position request + temperature (total: 5 bytes) + 4 for multi-turn
Command "v" (0x76) – serial number (total: 7 bytes)
*/
unsigned int data = SPI.transfer(0x33); // Data
digitalWrite(CS, HIGH); // End communications 
return(data);
}
void loop() {
Serial.println(RTC_init());
}

首先,您忘记将CS引脚设置为输出。那没用的。

void setup()
{
Serial.begin(9600);
digitalWrite(CS, HIGH);      // set CS pin HIGH
pinMode(CS, OUTPUT);         // then enable output.
}

SPI是一个双向主/从同步链路,时钟由主发送的字节驱动。这意味着当在SPI上进行事务处理时,您希望发送多少字节就发送多少字节。

如数据表第14页的时序图所示,您应该使用该命令发送一个字节,然后发送响应所需的空字节数减去1。

该图显示时钟在空闲时为低电平,当时钟变低时输入位稳定,即SPI模式1。

读取位置,用于非多匝编码器。

unsigned int readEncoderPos()
{
// this initialization could be moved to setup(), if you do not use
// the SPI to communicate with another peripheral.
// Setting speed to 1MHz, but the encoder can probably do better
// than that. When having communication issues, start slow, then set
// final speed when everything works.  
SPI.beginTransaction(SPISettings(1'000'000, MSBFIRST, SPI_MODE1));
digitalWrite(CS, LOW);
// wait at least 2.5us, as per datasheet.
delayMicroseconds(3);
unsigned int reading = SPI.transfer16(0x3300);  // 0x3300, since we're sending MSB first.
digitalWrite(CS, HIGH);
// not needed if you moved SPI.beginTransaction() to setup. 
SPI.endTransaction();
// shift out the 2 status bits
return reading >> 2;
}

最新更新