2

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:

enter image description here

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 :).

asked Jul 12, 2016 at 14:52
8
  • Does a simple "echo" program work? E.g. void loop(){if(Serial.available()) Serial.write(Serial.read());}. Commented Jul 12, 2016 at 15:13
  • Same behavior. It sends string variable normally but returns gibberish. It seems like there is problem with reading then.. ? Commented Jul 12, 2016 at 15:31
  • I can't understand your answer: there is no string variable in the simple echo program above. Can you try another terminal emulator? Commented Jul 12, 2016 at 15:50
  • I just added one simple variable to test it. My bad I didn't say it specifically. I tried Realterm and built in Arduino terminal. I don't think another one will bring different results. Commented Jul 12, 2016 at 15:57
  • Gibberish usually means wrong baud rate, but those seem to match between the arduino code and realterm. Commented Jul 12, 2016 at 16:03

2 Answers 2

-1

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.

answered Oct 11, 2016 at 14:46
1

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.

answered Jul 13, 2016 at 11:06
1
  • 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. Commented Oct 11, 2016 at 14:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.