Code which worked on the Arduino nano doesn't work on teensy

HeadLess

Member
I wrote a code for the Arduino nano to control the AD5932 and it worked fine. I transferred the code to the teensy and it stopped working.

Teensy 4.1 pinout
11-SData
13-SCLK
2-FSYNC
3-CTRL
4-Interrupt
5-Standby
GND-AGND (I connected all the grounds and it works on Arduino)


What happens if I execute the code on the teensy is that nothing happens.
I can see on my oscilloscope that I transfer 2x 8bit over pins 11 and 13. SCK is 935kHz but nothing happens.

AD5932 Datasheet:
https://www.analog.com/media/en/technical-documentation/data-sheets/AD5932.pdf
https://www.analog.com/media/en/technical-documentation/application-notes/AN-1044.pdf

GitHub with all the code as well
https://github.com/Tobias12343123/AD5932_Arduino_Teensy4.1.git

I also get the warnings but not always:

In file included from C:\Users\-------\AppData\Local\Temp\arduino_build_238584\sketch\Ad5932.cpp:3:0:
C:\Users\-------\AppData\Local\Temp\arduino_build_238584\sketch\Ad5932.h: In constructor 'Ad5932::Ad5932()':
Ad5932.h:27: warning: 'Ad5932::_oscillator' will be initialized after
float _oscillator; //variable oscillator
^
Ad5932.h:21: warning: 'uint8_t Ad5932::_fsync'
uint8_t _fsync; //pin fsync
^
Ad5932.cpp:6: warning: when initialized here
Ad5932::Ad5932(): _oscillator(0), _fsync(0), _ctrl(0), _interrupt(0), _standby(0),_controlRegister(), _frequency(5), _nbIncrement(2), _intervalDuration(0)
^
Memory Usage on Teensy 4.1:
FLASH: code:14072, data:3628, headers:8920 free for files:8099844
RAM1: variables:12992, code:11400, padding:21368 free for local variables:478528
RAM2: variables:12384 free for malloc/new:511904
 
The compiler warnings are simply complaining that in your constructor, the initializer are not in the same order as the member variables are defined, which they are supposed to be. Not sure it makes any difference....

What device are you using? How did you have it hooked up on NANO? that is I believe the NANO is a 5v processor and the T4.1 is a 3.3v. Does your device handle either? What power is provided to the AD5932?

Or are you going direct to a chip?

Note: These days I would avoid SPI stuff like:
Code:
  SPI.setBitOrder(MSBFIRST);            //MSB first
  SPI.setDataMode(SPI_MODE1);           //CPOL = 0, CPHA = 1
  SPI.setClockDivider(SPI_CLOCK_DIV128);//f/128
  //SPI_CLOCK_DIV4-SPI_CLOCK_DIV16-SPI_CLOCK_DIV32-SPI_CLOCK_DIV64
And go to using SPI.beginTransaction... https://www.arduino.cc/en/Reference/SPIbeginTransaction

You say SCK is 935khz... I believe the document says it can go up to 40mhz...

Might help to see differences in scope data to see if timings are off or the like....
 
Your loop is:
Code:
    gene.start();                       //setting the CTRL pin HIGH
    delay(200);

gene.start is:
Code:
void Ad5932::start(){
/* ************************************************************************** */
/* start - enabling CTRL pin from HIGH to LOW */
  digitalWrite(_ctrl, HIGH);            
  //delay(50);                            
  //digitalWrite(_ctrl, LOW);            
}

So it calls digitalWrite(_ctrl, HIGH); in 200ms intervals.
Not sure what should happen.
 
I'm using the teensy 4.1 and use SPI communication to the AD5932

The datasheet for the AD5932 states "Digital I/O Voltage to DGND −0.3 V to DVDD + 0.3 V" and "DVDD to DGND −0.3 V to +6.0 V" so I would assume that is not an issue.

I used the default teensy spi speed of 4 MHz and it also doesn't work well.
 
By the program, it first loads the registers in the void setup. Afterward, it should start sweeping every time _ctrl is high. The thing is it starts if I toggle it. But it starts with the default values. after it is done sweeping and I start it again it stops working.
 
Assuming teensy 4.1 is booting much faster then nano you might have a timing problem. AD5932 is not ready when teensy tries to write to registers
Maybe try to add
Code:
delay(5);
as first in setup()
 
Back
Top