Timer and USART not working paralelly

I am attempting wireless communication via IR. I am using USART to send my messages. My transmitting microcontroller has two functions:

  • Generate 38kHz pulse on OC1A pin
  • Send a USART message(in the present case, send ‘a’)

This is the problem:
USART does not work when the timer is set.
However, USART works when the timer is not set.

Timer details:
I am using Timer one in CTC Toggle mode

Arduino details:
I have checked this code on an Arduino Uno and an Arduino Mega 2650. It did not work on both.

How did I check if USART was working?
I observed the “TX” led on the Arduino board. It did not glow at all for the code below.

//Function 1. generate 38kHz pulse on pin 9
//Function 2. send usart 'a' on tx repearedly
//i am creating a delay without using 'delay'

#define myOutputPin 9
long previousMillis = 0;

void setup ()
{
 pinMode (myOutputPin, OUTPUT); 
 cli();
 TCCR1A = 0;
 TCCR1B = 0;
 TCNT1  = 0;
 OCR1A = 25;   // toggle after counting to 25
 TCCR1A |= (1 << COM1A0);   // Toggle OC1A on Compare Match.
 TCCR1B |= (1 << WGM12);    // CTC mode
 TCCR1B |= (1 << CS11);     // clock on, prescalar 8
 
  Serial.begin(9600);        //initializing USART at 9600 baud
}
void loop () 
{
 unsigned long currentMillis = millis();
 
 if(currentMillis - previousMillis > 1000)
 {
   previousMillis = currentMillis;
   
   Serial.println('a');
 }
 
}

Your code is disabling all interrupts with a cli(); command.

Perhaps you want to enable interrupts again using sti(); once upon a time?

Most likely before "Serial.begin"?

It probably doesn't help that in setup() you turn off interrupts but never turn them back on....

Regards, Ray L.

FYI: You may get better performance at a lower BAUD rate....2,400 bps or lower

@9600 the individual bit duration is 104 uSecs and most IR receivers can change the timings up to +/- 100uSecs.

Sending 4 or less characters at a time may also improve performance.

(If you use the standard NEC IR protocols, you can send 32 bits or 4 bytes with each signal)