Despite the enormous baud rate, it actually kind of works. The biggest problem I have is that the 7th bit is stuck high. Reducing the baud rate kind of helps, but it makes a bunch of other things worse so I'm not considering it yet. I've tried outputting this bit on other pins, makes no difference. Any ideas on what's going on here?
Did some research on 328P baud rates and reducing it to exactly 500000 seems to have fixed the 7th bit issue. Maybe there is also a way to stabilize the execution speed? Despite the code being pretty straightforward digital outputs slightly stutter and degrade the audio.
If you look in the Atmega 328 datasheet you will see tables of typical baud rates and the timing errors associated with them. There is no timing error at 500,000 baud - or 1,000,000 baud. I use 500,000 baud regularly.
I wonder if all your digitalWrite()s are slowing things down for the high baud rate.
Try the digitalWriteFast.h library - it really is a lot faster but it must know the I/O pins at compile time - which you do.
Read about direct port adressing for even faster I/O. If you can choose your I/O pins to suit the I/O Ports on the nano you could probably write all 8 values with 2 or 3 instructions and without even needing bitRead(). Unfortunately none of the I/O ports on a nano has all 8 bits available so you would need to use at least 2 Ports.
Thanks everyone!
Nothing worked, and it really shouldn't have, because all along the real problem was Tera Term. As soon as I switched to the command line "copy" the glitches stopped.
Robin2:
Try the digitalWriteFast.h library
Thanks for the info! Even though regular digitalWrite would probably work fine, I ended up using digitalWriteFast anyways, just to be sure.