[英]two wire ADC, SPI read
我使用 Arduino UNO (Atmega328) 和一个 12 位 ADC 组件 (AD7893ANZ-10),数据表可从https://www.analog.com/media/en/technical-documentation/data-sheets/AD7893.pdf 获得
问题:我已经尝试了几种不同的代码,但读取值总是错误,即使 ADC 的 SDATA 引脚未连接到 Arduino 的 MISO 引脚,我也得到相同的值(请参阅此处的图 1 )。 我在 proteus 中模拟了它(参见这里的图 2 )并在 proteus 中使用了虚拟串行监视器。 Arduino 的 MOSI 和 SS 引脚未连接,但我将代码中的 SS 引脚设置为低和高以满足库要求。 有关 ADC 时序的更多信息作为注释添加到下面的代码中。 或在数据表中提供。 如果您查看一下,我将不胜感激,因为我无法弄清楚我做错了什么。
PS:ADC 只需引脚与 SPI 通信:1.SDATA(slaveout) 和 2.SCLK。 ADC 上的引脚 CONVST 用于启动转换。
#include <SPI.h>
//source of code https://www.gammon.com.au/spi
void setup() {
Serial.begin (115200);
pinMode(7, OUTPUT); // this pin is connected to convst on adc to initiate conversion
// Put SCK, MOSI, SS pins into output mode (introductions from source)
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
SPCR = (1<<MSTR);
SPI.begin ();
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1)); // set the clk frequency; take the MSB first;
//mode1 for CPOL=0 and CPHA=1 according to datasheet p.9 "CPOL bit set to a logic zero and its CPHA bit set to a logic one."
}
int transferAndWait (const byte what) //methode to read data
{
int a = SPI.transfer(what); //send zero(MOSI not connected)and read the first 8 bits and save
Serial.println (a); // show the value, in serial monitor -1
a =(a << 8); //shift the saved first 8 bits to left
Serial.println (a); // show the value, in serial monitor 255
a = a | SPI.transfer(what); //read and save the last 8 bits
Serial.println (a); // show the value, in serial monitor -256
delayMicroseconds (10);
return a;
}
void loop() {
int k;
digitalWrite(7, HIGH); //set pin high to initiate the conversion
delayMicroseconds(9); //the conversion time needed, actually 6 mikroseconds
digitalWrite(SS, LOW); // enable Slave Select to get the library working
k = transferAndWait (0); //call the method
delayMicroseconds(1);
digitalWrite(7, LOW);
digitalWrite(SS, HIGH); //disable chip select
delay(2000); //delay just to keep the overview on serial monitor
Serial.println (k); // show the value, in serial monitor -1
}
首先,返回变量应该是无符号整数而不是有符号整数。 此外,CONVST 应该只在短时间内处于低电平,因为转换是在之后开始的。 请参阅时序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.