大佬教程收集整理的这篇文章主要介绍了带绝对编码器的 SPI Arduino 接口,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试通过 Arduino Uno 通过 SPI 从 RLS 绝对旋转编码器检索数据。我已经设法学习了 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) – @R_403_4612@ request (@R_255_10586@l: 3 bytes) + 4 for multi-turn
Command "3" (0x33) – short @R_403_4612@ request (@R_255_10586@l: 2 bytes) + 4 for multi-turn
Command "d" (0x64) – @R_403_4612@ request + detailed status (@R_255_10586@l: 4 bytes) + 4 for multi-turn
Command "t" (0x74) – @R_403_4612@ request + temperature (@R_255_10586@l: 5 bytes) + 4 for multi-turn
Command "v" (0x76) – serial number (@R_255_10586@l: 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;
}
以上是大佬教程为你收集整理的带绝对编码器的 SPI Arduino 接口全部内容,希望文章能够帮你解决带绝对编码器的 SPI Arduino 接口所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。