I’m getting started with PlatformIO and I’m getting behavior that I don’t understand at all. My serial monitor outputs garbage characters. I’ve tried to follow the similar posts on this forum to no avail. By a stroke of luck, I was able to find one configuration that outputs successfully to the monitor, and that’s by setting Serial.begin(115200) and the serial monitor baud rate at 57600 and I would really like to understand what is going on
Here is the output when using Serial.begin(9600) and monitor baud rate at 9600 as well.
It should not be needed for this question but just in case, here is the test code
Upon further testing, it appears everything works as long as the Serial.begin value is twice the value of the monitor value. I’m not sure how to interpret this, sounds like maybe a board definition issue? (Sorry if I’m saying non-sensical things, I’m very new at all this)
If anybody has some insight, that would be greatly appreciated,
Thank you.
Then you have proven yourself that the clock rate on the microcontroller is off by exactly factor 2.
Because 115200 / 2 = 57600.
The code on the chip thinks it is running at a certain clock frequency, but in reality the clock to the microcontroller is only half that frequency. (That’s why Serial.begin(115200) results in 57600 serial signal instead.)
Most likely, you have a LGT8F328P board with a 16MHz crystal but the board definition is set for 32MHz.
What’s the reality on your board in regards to the quartz oscillator? Is there one, and if yes, what frequency does it have?
See their clock config options
The other possibility is that you have some fuse settings burned in your chip that activate a “div clock by 2”.
Okay, updated my code to this and the clocks are fine now. I’ll ferret around to see if there’s a way to fix that directly in the board definition or something but I suck at this.
By the way, after look at it again, with your original platformio.ini and the framework’s code, whatever clock divider the bootloader set should have survived. So the actual source of all your problems might be that bootloader on your chip is set for 16MHz, the framework code does not modify it (with F_OSC unset), and so your code also runs at 16MHz (if not counteracted against).
And given that this bootloader binary is in your chip
the 0xfc30 (decimal -976) is consistent with F_CPU = 16000000.
While the bootloader could be recompiled and reburned with F_CPU = 32MHz, I think this is more complicated than just accepting that the bootloader runs at whatever clock frequency / divider it wants to and that the Arduino core should just set the frequency divider to achieve its wanted frequency.
Awesome. Thank you for the issue, the answer and the insight. I’ll defer to your clearly superior knowledge as far as the bootloader goes, but for now, your ini fix suits me more than perfectly. Thank you again!
Also, come to think of it, I think this may have been by design. This MCU is meant to be an Atmega clone so maybe they were trying to avoid some potential timing issues for inplace replacements?