简体   繁体   English

两线ADC,SPI读取

[英]two wire ADC, SPI read

I use Arduino UNO (Atmega328) and a 12bit ADC component (AD7893ANZ-10), datasheet available on https://www.analog.com/media/en/technical-documentation/data-sheets/AD7893.pdf我使用 Arduino UNO (Atmega328) 和一个 12 位 ADC 组件 (AD7893ANZ-10),数据表可从https://www.analog.com/media/en/technical-documentation/data-sheets/AD7893.pdf 获得

The problem: I tried already few different codes but the read value is wrong always, even when the SDATA pin of ADC is not connected to MISO pin of Arduino, I get the same values ( See figure1 here ).问题:我已经尝试了几种不同的代码,但读取值总是错误,即使 ADC 的 SDATA 引脚未连接到 Arduino 的 MISO 引脚,我也得到相同的值(请参阅此处的图 1 )。 I simulated it in proteus( See figure2 here ) and used the virtual serial monitor in proteus.我在 proteus 中模拟了它(参见这里的图 2 )并在 proteus 中使用了虚拟串行监视器。 The MOSI and SS pin of Arduino are not connected but I set SS pin in code to LOW and HIGH to fullfill libraries requirements. Arduino 的 MOSI 和 SS 引脚未连接,但我将代码中的 SS 引脚设置为低和高以满足库要求。 More information about the timing of ADC is added as comments into the code below.有关 ADC 时序的更多信息作为注释添加到下面的代码中。 Or availabe in the datasheet.或在数据表中提供。 I would be thanksfull if you take a look on it due I cant figure out what I did wrong.如果您查看一下,我将不胜感激,因为我无法弄清楚我做错了什么。

PS: The ADC has just to pins to communicate with SPI: 1.SDATA(slaveout) and 2.SCLK. PS:ADC 只需引脚与 SPI 通信:1.SDATA(slaveout) 和 2.SCLK。 The pin CONVST on ADC is to initiate a conversion. 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
}

First the the return variable should be an unsigned int instead of a signed int.首先,返回变量应该是无符号整数而不是有符号整数。 Also the CONVST should only be low for a short period as conversion is started afterwards.此外,CONVST 应该只在短时间内处于低电平,因为转换是在之后开始的。 See Timing sequence .请参阅时序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM