Programming an Atmel328P

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Everyone,

So, I've been messing around with a atmel168, that had the bootloader already there. The device has failed, so I picked up a 328P, laoded with Arduino UNO bootloader. (http://www.mcmelectronics.com/pr...)

In short, I out this in place,and cannot seem to write any code to it. avrdude just hangs out waiting for comms. Now, I had a 14.47Mhz crystal attached to the 168, and am using it on the 328P. I thought this would be fine, but otherwise, I do not understand why this isn't working properly.

Thanks for any input!

Oh, I am doing this:
avrdude -c avr109 -p m328p -b 115200 -P COM5 -U flash:w:test.hex:a

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The UNO boot loader utilizes a 'subset of' stk500v1.
The UNO boot loader also expects a 16 Mhz clock.

So, you've got the wrong protocol (avr109) and a 10% baud rate error.

If your serial hardware can support it, you can try something like this...

avrdude -c stk500v1 -p m328p -b 104184 -P COM5 -U flash:w:test.hex:a

Let us know if that works!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the info! I did not realize that was the -c option. Now it makes more sense. My version of avrdude (5.6 - for windows) does allow a baud rate of 104184.
I grabbed a 5.11 binary and it accepted the baud rate but it didn't quite succeed.
avrdude.exe: stk500_getsync(): not in sync: resp=0x00

I'm interested in how you derived the baud rate based on the clock speed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

14.47 / 16.0 * 115200

avrdude does not care what baud rate you give it, it just passes that on to the serial driver.

Not all serial hardware can handle non-standard baud rates, and even if the hardware can handle it, not all drivers will work with it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks! I went ahead an ordered a couple 16Mhz crystals too. I appreciate the help. This is a new area of learning for me. (and I love it)

Avrdude does seem to care, at least version 5.6:
avrdude.exe: serial_baud_lookup(): unknown baud rate: 106128

Turns out my crystal is 14.74Mhz. So, I recalculated based on your equation. Thanks for that!

But, no dice! I will wait until the 16Mhz crystal arrives.

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Presumably you have a fully functional UNO as well as your 'bare-metal' mega328P chip.

If so, you simply rebuild the bootloader for a 14.7MHz crystal and burn it on the bare mega328P.

Any pre-programmed AVR will 'assume' a 16MHz UNO. But you can re-build for any frequency. Note that some frequencies can't achieve 115200 baud. (14.7456MHz is good)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do not have an UNO at all.. Just the 328P. Perhaps I am approaching this the wrong way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do you have the 328p connected to the PC?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Pins 2 and 3. Using a prolific USB serial device on the PC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Prolific based USB dongles are a pain and may not work with Win7.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Pins 2 and 3. Using a prolific USB serial device on the PC.

The Uno bootloader has a 1-second timeout after reset, before giving up on the bootload and running the sketch instead. That means it is counting on the Arduino's "auto-reset" feature, and trying to manually reset the chip at just the right time can be "difficult." (less so if you're running avrdude from the command line. Hold the reset button down, hit "enter" on the avrdude command line, immediately release the reset button.)

Also, are you sure you have your RX->TX, TX->RX wiring correctly? And is it a prolific "TTL Level" serial device, or an "rs232 port" device (jumpers/headers, or a DB9?) If it's designed to talk to rs232 devices (DB9), then the signals will be inverted WRT what the AVR requires (in addition to potentially being at the wrong voltage levels.)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the input. I was previously using an atmel168 without any issues, so the wiring and prolific device are fine and working great. I actually got an atmel168 replacement but didn't realize (or understand) at that the time that they have to have a bootloader to work. I was eager to work on my project, so I picked up a 328p locally with the arduino bootloader, thinking I could just drop it in and use it. Bad assumptions make for great learning, so thanks for all that. I am going to try the reset trick. Additionally, I ordered a programmer that should be here in a few days.. so all this learning will negated when I have it.. but this is still fun to me, and I want the understanding.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An Arduino would be better 'value' than a single-purpose programmer. And not much more money either!

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgauthier wrote:
Thanks! I went ahead an ordered a couple 16Mhz crystals too. I appreciate the help. This is a new area of learning for me. (and I love it)

Avrdude does seem to care, at least version 5.6:
avrdude.exe: serial_baud_lookup(): unknown baud rate: 106128

Turns out my crystal is 14.74Mhz. So, I recalculated based on your equation. Thanks for that!

But, no dice! I will wait until the 16Mhz crystal arrives.

Thanks!

a 17.456MHz is ideal as it is a multiple of the baud rates and permits low/no error at 115200 and up. A 16MHz crystal does not.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The bootloader is pre-programmed for 16MHz. You can just plug and go with a 16MHz crystal. Arduino sketches and hardware shields will work straight out of the box.

If you recompile the bootloader you can choose any baud rate and crystal that you like.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, progress. I received my 16mhz crystal, and hooked it up. I have to use the reset pin, and program within that one second window. I'm definitely getting a response now, it's just not the one I want.

avrdude -c stk500v1 -p m328p -b 115200 -P COM5 -U flash:w:bootpi.hex:a

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000
avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.

Any suggestions? (I am off to hit google)