Hello everyone. I am try to convert some code from my Arduino Uno projects to a breadboarded ATMega1284P using MightyCore (code programmed using a TL866CS using binaries from "Export compiled binaries" without bootloader). I have a project where I used tone and serial at the same time; however, for some reason, on the ATMeag1284P, I can't use Serial and Tone at the same time. If I send anything to the serial pin, the tone glitches and if I continue to send serial, the tone will eventually stop completely until I reset the chip and start over. If I use Serial1 instead, the problem disappears. I could just use Serial1 instead, but I'd like to make some modifications to the code where I need both serial ports.
This is very stripped down code that still demonstrates the glitch:
byte test = 0;
void setup() {
Serial.begin(256000); //Somewhat atypical serial rate but the connected device uses this which is out of my control
}
void loop() {
if (Serial.available() >= 1) {
test = Serial.read();
}
tone(0, 4500);
}
//Replace Serial. with Serial1. and the glitch disappears
I don't know a whole lot about the lower-level workings of these chips, so it could be some issue that I just don't know how to detect (like using the same timer or something like that.)
Anybody know what could be causing this and how I could fix it?
According to the source code I found (you forgot the link) the used core doesn't have special source code for the HardwareSerial class as well as for the Tone library. As there is no special handling code for the ATmega1284 the same routines as for the ATmega328 are used. Tone uses timer 2 and HardwareSerial use the USART hardware. I cannot find a dependency between the two in the datasheet.
What clock rate do you have for the 1284?
Can you explain in more detail what glitches you have on the tone?
pylon:
According to the source code I found (you forgot the link) the used core doesn't have special source code for the HardwareSerial class as well as for the Tone library. As there is no special handling code for the ATmega1284 the same routines as for the ATmega328 are used. Tone uses timer 2 and HardwareSerial use the USART hardware. I cannot find a dependency between the two in the datasheet.
What clock rate do you have for the 1284?
Can you explain in more detail what glitches you have on the tone?
I am using a 16Mhz crystal with capacitors for a clock; I also tried a 20Mhz crystal but the results were essentially the same.
I don't really know how to explain the glitch, but I attached an audio sample of what the tone does as I send serial. The tone stops at the end of the audio file because the chip quit putting it out, not because I stopped it. You'll notice the first glitch at 2 seconds is just a small dropout, then whatever weirdness happens at 3 seconds, and lastly the tone completely stopping at 7 seconds. Each of things occurred when I sent a serial message.
I'm not really surprised that the tone is changed that way if there is serial traffic, I'm surprised that it doesn't happen on Serial1.
Both the serial interface (USART) and the Tone library (timer 2) are using interrupts to do their job. During such an interrupt service routine all other interrupts are blocked. You have serial connection with a very high baud rate (250000 in reality as the clock divider doesn't allow for 256000) which interrupts the processor every 30µs to get a character and store it into a buffer or to send a character which resides in the outgoing buffer.
Additionally you restart the tone timer really fast which also might influence that. Have you tried moving the tone call to the setup()? Does that make a difference?
As I wrote, the strange thing is that it doesn't happen on Serial1.
Did you check if the ATmega1284 freezes if the tone stops? Does it still react on other stuff? How much data do you send by the serial interface?