Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Teensy 3.2 with AD7606

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    4

    Teensy 3.2 with AD7606

    Hi,

    I have a problem with my little project
    In a small analog to CAN interface , I use a AD7606 with a arduino mega.
    Now I want to update to a teensy for some more processing capacity and of course the onboard CAN .

    The problem is , I can not get the AD7606 to run on my Teensy
    I tried with a 2nd one but also not running.
    I have the feeling it is a SPI problem.

    I use the following sketch to test :

    Code:
    #include <SPI.h>
    
    #define SCALE_FACTOR 0.000152587890625
    
    // 10/(2^16) = 0.000152587890625
    
    #define BUSY 3
    #define RESET 4
    #define START_CONVERSION 5
    #define CHIP_SELECT 10
    #define MISO 12
    #define LED 13
    #define TOTAL_RAW_BYTES 16
    
    int bytesToRead = TOTAL_RAW_BYTES;  
    byte raw[TOTAL_RAW_BYTES];  
    signed long parsed[8];
    
    void setup() {  
      pinMode(BUSY, INPUT);
      pinMode(RESET, OUTPUT);
      pinMode(LED, OUTPUT);
      pinMode(START_CONVERSION, OUTPUT);
      pinMode(MISO, INPUT);
    
      Serial.begin(115200);
      SPI.begin();
    
      digitalWrite(START_CONVERSION, HIGH);  
      digitalWrite(CHIP_SELECT, HIGH);
      digitalWrite(RESET, HIGH);
      delay(1);
      digitalWrite(RESET, LOW);
    }
    
    void loop() {  
      int i;
    
      digitalWrite(START_CONVERSION, LOW);
      delayMicroseconds(10);
      digitalWrite(START_CONVERSION, HIGH);
    
      while (digitalRead(BUSY) == HIGH) {
        // wait for conversion to complete
      }
      digitalWrite(CHIP_SELECT, LOW);
      while (bytesToRead > 0) {
        raw[TOTAL_RAW_BYTES - bytesToRead] = SPI.transfer(0x00);
        bytesToRead--;
      }
      digitalWrite(CHIP_SELECT, HIGH);
      bytesToRead = TOTAL_RAW_BYTES;
    
      parseRawBytes();
    
      //Serial.write(raw, 16);
    
      for(i=0; i<8; i++) {
        Serial.print((float)parsed[i] * SCALE_FACTOR, 5);
        Serial.print(",");
      }
      Serial.print("\r\n");
      delay(1000);
    }
    
    void parseRawBytes() {
      parsed[0] = (raw[0] << 8) + raw[1];
      parsed[1] = (raw[2] << 8) + raw[3];
      parsed[2] = (raw[4] << 8) + raw[5];
      parsed[3] = (raw[6] << 8) + raw[7];
      parsed[4] = (raw[8] << 8) + raw[9];
      parsed[5] = (raw[10] << 8) + raw[11];
      parsed[6] = (raw[12] << 8) + raw[13];
      parsed[7] = (raw[14] << 8) + raw[15];
    }
    I suppose I,m doing something wrong for a teensy ?
    the same AD7606 and sketch is working with arduino Mega so I,m very certain the wiring is correct (SPI).
    I had a pullup resistor over the Cs pin 10 .
    The SPI seems to work as I have tested with a Max31855 on the SPI bus.

    Regards Klass

  2. #2
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    735
    This is a different ADC, but the AD7689_read_array() below should be helpful as a model.

    https://gitlab.com/our-sci/reflectan...src/AD7689.cpp

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    That code has SPI transactions implemented,
    Tried that in my code above but it made no difference (it has only the AD7606 on the SPI so no noise from others)
    the code above works on Arduino Mega (but very slow) but I thougt the code itself was more or less ok ?

  4. #4
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    studying that AD7689 now , the complete conversion protocol is a bit different as it uses no busy ?

  5. #5
    Senior Member
    Join Date
    Feb 2018
    Location
    Corvallis, OR
    Posts
    257
    Is it possible that the T3.2 and the Arduino Mega have different default settings for SPI clock phase and polarity?

  6. #6
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    I have this code working now, thanks for all the help.

    Code:
    /*
       Teensy 3.2  AD7606 SPI
    */
    #include <SPI.h>
    
    uint32_t start, stop;
    
    #define SCALE_FACTOR 0.000152587890625
    
    #define BUSY 3
    #define RESET 4
    #define START_CONVERSION 5
    #define CHIP_SELECT 10
    #define MISO 12
    #define MOSI 11
    #define LED 13
    #define TOTAL_RAW_BYTES 16
    
    SPISettings _spiSettings;
    
    int bytesToRead = TOTAL_RAW_BYTES;
    byte raw[TOTAL_RAW_BYTES];
    uint16_t parsed[8];
    int x = 0;
    
    void setup()
    {
      pinMode(BUSY, INPUT);
      pinMode(RESET, OUTPUT);
      pinMode(START_CONVERSION, OUTPUT);
      pinMode(CHIP_SELECT, OUTPUT);
    
      Serial.begin(115200);
    
      SPI.begin();
      digitalWrite(START_CONVERSION, HIGH);
      digitalWrite(CHIP_SELECT, HIGH);
      digitalWrite(RESET, HIGH);
      delay(100);
      digitalWrite(RESET, LOW);
      delay(100);
    }
    
    void loop()
    {
      start = micros();
      int i;
      digitalWrite(START_CONVERSION, LOW);
      delayMicroseconds(100);
      digitalWrite(START_CONVERSION, HIGH);  
      while (digitalRead(BUSY) == HIGH) {
        delayMicroseconds(1);
      }
      SPI.beginTransaction(_spiSettings);
      digitalWrite(CHIP_SELECT, LOW);
      while (bytesToRead > 0) {
        raw[TOTAL_RAW_BYTES - bytesToRead] = SPI.transfer(0x00);
        bytesToRead--;
      }
      digitalWrite(CHIP_SELECT, HIGH);
      SPI.endTransaction();
    
      bytesToRead = TOTAL_RAW_BYTES;
    
      parseRawBytes();
    
      for (int i = 0; i < 8; i++) {
        Serial.print((float)parsed[i] * SCALE_FACTOR, 5);
        Serial.print(",");
      }
      Serial.print("\r\n");
      stop = micros();
    
      Serial.print("time:\t\t");
      Serial.println(stop - start);
    }
    
    void parseRawBytes() {
      parsed[0] = (raw[0] << 8) + raw[1];
      parsed[1] = (raw[2] << 8) + raw[3];
      parsed[2] = (raw[4] << 8) + raw[5];
      parsed[3] = (raw[6] << 8) + raw[7];
      parsed[4] = (raw[8] << 8) + raw[9];
      parsed[5] = (raw[10] << 8) + raw[11];
      parsed[6] = (raw[12] << 8) + raw[13];
      parsed[7] = (raw[14] << 8) + raw[15];
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •