2

I'm trying to use the serial communication to work between a naked AVR Atmega328p and a computer (OS X and Linux Ubuntu).

I can program the atmega (using a pololu programmer), I make it blink a LED on a breadboard, it works fine.

I am using the Arduino libraries to facilitate the task. And they seem to work fine (using delay, digitalRead, digitalWrite, etc).

I use a USB-to-TTL CH340 chip bought on internet. The CH340 works fine, it is recognised both on Ubuntu and OS X, and the loopback test works (Put a jumper between the TX and RX line, launch screen, type something and it comes back in the terminal), so I assume the CH340 is properly recognise and works.

Then, I plug the CH340 on my board. The 5V on the + column of the breadboard, the GND to the - column. The circuit works, my LED blinks as requested. The loop function looks like this:

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  digitalWrite(9, HIGH);   // sets the LED on
  delay(30);                     // waits for a second
  digitalWrite(9, LOW);    // sets the LED off
  delay(70);                     // waits for a second
  Serial.println("loop end");
}

As you can see, at the send of the loop I send a string on the serial line. Now, I see the blue LED blinking on the USB-to-TTL converter, so it is definitely receiving data. But when I try to connect like so:

screen /dev/cu.wchusbserialfd110 9600

on OS X, or

screen /dev/ttyUSB0 9600

on Ubuntu, nothing is displayed. I just get a black terminal.

Is the Arduino Serial works with Atmega328 ? I suppose so, as it is the same microcontroller than on the Arduino. Any idea of what I am doing wrong?

Update:

More details to answer Christ Stratton question:

My low fuse is 62: 01100010. So, according to the specifications, the internal clock is used and set at 1MHz.

I programmed the Atmega using my own test bed, a Pololu programmer (as described in the book Practical AVR Microcontrollers: Games, Gadgets, and Home Automation with the Microcontroller Used in Arduino) and avrdude (through the use of platformio). This is the command line used by platformio to flash the ROM:

/Users/[user]/.platformio/packages/tool-avrdude/avrdude -v -p atmega328p -C /Users/[user]/.platformio/packages/tool-avrdude/avrdude.conf -c avrispv2 -B 10 -e -b 115200 -P "/dev/cu.usbmodem00149121" -U flash:w:.pioenvs/pololu/firmware.hex

According to avrdude documentation, -B 10 means bitclock is set to 10us (microseconds) so a clock at speed 100Khz.

Now if I set this -B option to 1 (1Mhz), I can't program the Atmega anymore. Avrdude just timeout:

avrdude stk500v2_ReceiveMessage(): timeout

1 Answers1

1

The clock rate of your board must match the clock rate defined in the board file you used to compile your sketch. If it does not, the baud rate will differ by the same factor.

If the actual baud rate so achieved is a valid one, you can temporarily achieve communication by setting the computer to that baud rate, but all delays and other clock-rate related functions will be off.

You should use or create a board file entry that matches your board, or use a standard clock source / clock divier setting for which there already is an entry.

Chris Stratton
  • 5,411
  • 20
  • 40