I am trying to interface Arduino with an AFE4300 (TI). In a nutshell, the AFE4300 requires an external clock and can be controlled through SPI. I have managed to interface with the IC and I am able to collect impedance data without any issues. However, for deterministic phase calculations I have to synchronize SPI with the clock.
I am using an Arduino Mega2560 for the project and I am using Timer 3 for creating a 1MHz clock signal.
pinMode(5,OUTPUT);
//select CTC mode on Timer 3
//set prescaler to 8
TCCR3A = _BV(COM3A0) ;
TCCR3B = _BV(WGM32) | _BV(CS31) ;
OCR3A = 0;
According to the ATMEGA2560 datasheet, in the CTC mode this should result in a PWM signal at fCLK_IO/2*8 . I confirmed the 1MHz output with an oscilloscope and I don't have any problem there.
SPI calls are made to program the registers. My SPI setting is as follows:
SPISettings(2000000, MSBFIRST, SPI_MODE1);
scoping SCK and the output PWM it is very evident that they are not in sync. I am confused by this. The SPI clock should be a prescaled value of the fOSC. I could be wrong here, but the prescaler should be set to 8 in this case.
Since I did not make any explicit changes to the system clock prescaler (i.e CLKPR) I am assuming that both SPI and Timer 3 uses the same clock.I think this is a safe assumption since
10.2.2 I/O Clock – clkI/O
The I/O clock is used by the majority of the I/O modules, like Timer/Counters, SPI, and USART
(The ATMEGA datasheet has way too many names used interchangeably )
Are the prescaler for SCK same as the prescaler for PWM? Is this is the reason for assynchrony? Is there a simple way to synchronize the timer and SCK?