Hello,
I'm trying to synchronise arduino micro (32u4) timers 1 and 3.
Both those timers are used in CTC mode, toggle output on compare.
I first made this code :
// Enable CTC mode, timers stopped
TCCR1A = 0x40;
TCCR1B = 0x08;
TCCR3A = 0x40;
TCCR3B = 0x08;
// Set phase
TCNT1 = TCNT_value;
TCNT3 = 0;
// Set frequency
OCR1A = OCR1A_value;
OCR3A = OCR3A_value;
if((PINB&0x20)!=00) TCCR1C=0x80;
if((PINC&0x40)!=00) TCCR3C=0x80;
if(TCNT_value == OCR1A_value)
{
TCCR3C=0x80;
TCNT1 = 0;
}
// Start timers
bitWrite(TCCR1B, CS10, 1);
bitWrite(TCCR3B, CS30, 1);
But I noted a delay of approximately 0.99 µs when I input a null phase ( TCNT = 0 ).
Then I read a bit on the internet and of course the atmega32u4 user manual and tried to use GTCCR, which resulted in following code :
// Enable CTC mode, timer stopped
TCCR1A = 0x40;
TCCR1B = 0x09;
TCCR3A = 0x40;
TCCR3B = 0x09;
GTCCR = 0x83;
// Set phase
TCNT1 = TCNT_value;
TCNT3 = 0;
// Set frequency
OCR1A = OCR1A_value;
OCR3A = OCR3A_value;
if((PINB&0x20)!=00) TCCR1C=0x80;
if((PINC&0x40)!=00) TCCR3C=0x80;
if(TCNT_value == OCR1A_value)
{
TCCR3C=0x80;
TCNT1 = 0;
}
// Start timers
GTCCR = 0;
And again, ~ 1µs delay as you can see on that logic analyser picture :
I'm using frequency of ~ 8-12 khz. 1 µs is a quite significant phase offset at 12 khz.
Do anyone have an idea of why timers 1 and 3 are not synchronising ?
King regards
Aurélien