I recently flashed bootloader of Arduino to Atmega8. Unfortunately I stumbled on a problem I can't solved.
I am testing SerialEvent example on Atmega8. I modified it but started debugging it on my code (I think the same problem stops SerialEvent example from working).
I think the Atmega8 reads the characters sent through serial wrongly. That's why SerialEvent 'if' condition doesn't work also. Because it never gets '\n' symbol (it receives it but wrongly interprets it).
boolean prevState = true;
boolean OnOff = false;
boolean stringComplete = false;
String inputString = "";
String OnString = "on\n";
String OffString = "off\n";
void setup() {
Serial.begin(1200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// reserve 200 bytes for the inputString
inputString.reserve(200);
DDRD |= 0b00001100;
PORTD |= _BV(PD2);
// synchronization info for RPi
Serial.println("ARD - OK\n");
}
void loop() {
// turn on
if (OnOff == true && prevState == false){
PORTD |= _BV(PD2);
prevState = OnOff;
}
// turn off
if (OnOff == false && prevState == true){
PORTD &= ~_BV(PD2);
prevState = OnOff;
}
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
Serial.println(inputString);
if (inChar == '\n') {
stringComplete = true;
Serial.println("test\n");
}
if (stringComplete && inputString.equals(OnString)){
OnOff = true;
Serial.println("Turning on.\n");
}
else if (stringComplete && inputString.equals(OffString)){
OnOff = false;
Serial.println("Turning off.\n");
}
}
}
I tested a lot of behaviors. Below I attach one of the tests from Realterm:
First I reset the board - it responded with 'ARD - OK\n' - expected behavior. Then I sent 'on\n' and it responded with two lines but the characters aren't the same as 'on\n'. Then I sent 'test123\n' but once again got wrong characters. It prints multiple lines because it sends the string in which it accumulates the data every time SerialEvent() is fired.
Also sending for example variabl OnString works ok.
I really don't know why is it acting like that... Help would be appreciated :).
2 Answers 2
You have chosen the wrong build configuration which mismatches your hardware.
An ATmega8 with an 8 MHz oscillator works just fine, but all timing aspects of your code must be complied based on knowledge of that clock rate. If they are built for 16 MHz, then everything clock related will take twice as long as intended, for example baud rates will be half of what was intended.
In the Arduino world code is built for a clock rate specified in the chosen boards.txt file entry or in a dropdown menu for recent versions and boards with multiple clock variants. So the answer is that you have chosen the wrong build configuration for your hardware.
Ok I found a solution (rather workaround).
I uploaded the same program to Arduino Uno. It worked perfectly. I transplanted the Atmega328 from Uno to breadboard. Still worked BUT the Atmega328 is using external 16Mhz crystal (which I added to the breadboard).
So what if I uploaded bootloader for 16Mhz external crystal on Atmega8? It makes it work properly.
So in short: Atmega8 with 8Mhz internal oscillator doesn't work properly. Atmega8 with 16Mhz external oscillator works properly.
Don't know why does it make it work. If anyone can think of a reason I would be glad to know it. Thanks for all your help.
-
Not true. An ATmega8 with an 8 MHz oscillator works just fine, but all timing aspects of your code must be complied based on knowledge of that clock rate. If they are built for 16 MHz, then everything clock related will take twice as long as intended, for example baud rates will be half of what was intended. In the Arduino world code is built for a clock rate specified in the chosen boards.txt file entry or in a dropdown menu for recent versions and boards with multiple clock variants. So the answer is that you have chosen the wrong build configuration.Chris Stratton– Chris Stratton2016年10月11日 14:44:28 +00:00Commented Oct 11, 2016 at 14:44
void loop(){if(Serial.available()) Serial.write(Serial.read());}
.