Sunday, February 18, 2018

Programming an ATMEGA328PB with avr-gcc

Atmel/Microchip have recently released the ATMEGA328PB, a microcontroller that's very similar to the ATMEGA328B commonly found in Arduinos.  The new features are:

  • Slightly lower price
  • Two more good timers
  • Extra UART, SPI, and I2C
  • 4 more PWMs
There are some major drawbacks though
  • No more full-swing crystal oscillator
  • Didn't work with the Arduino IDE when I tried
  • Most toolchains don't support it yet out of the box
To program it with avr-gcc and avrdude requires a few extra steps.  These steps are written for a Linux development computer and a usbasp programmer.

The first step is to download and install the usual versions of avrdude, avr-gcc, avr-objcopy, avr-binutils, and avr-libc.  On Ubuntu, these are all packages that can be installed with apt.

The version of avrdude I have (6.2) didn't know about the 328pb, so I had to modify the avrdude.conf file.  On my install of Ubuntu 16.04, this was in /etc/avrdude.conf.  At the bottom, I added the lines:


part parent "m328" id = "m328pb"; desc = "ATmega328PB"; signature = 0x1e 0x95 0x16; ocdrev = 1; memory "efuse" size = 1; min_write_delay = 4500; max_write_delay = 4500; read = "0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0", "x x x x x x x x o o o o o o o o"; write = "1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 0", "x x x x x x x x x x x x i i i i"; ; ;


which I got from here: https://lists.nongnu.org/archive/html/avrdude-dev/2016-06/msg00010.html

This is the same as the normal 328b, but has the correct device signature for the 328pb.  As far as I can tell, the device signature is the only difference between the two.

Next, we need to download a "pack" from Atmel's that tells gcc about the 328pb.  I downloaded the Atmel ATmega Series Device Support Pack from packs.download.atmel.com 

I had to copy all the *.h files from within the pack to /usr/lib/avr/include to get the avr io headers to work properly.  I also put all the other files from the pack into a folder called `packs` in my project folder.  Inside the packs folder should be folder like "gcc, include, templates..."

Now we can program the microcontroller:

First, compile the code with

avr-gcc test.c -DF_CPU=1000000 -mmcu=atmega328pb -c -B packs/gcc/dev/atmega328pb/

then, create an ELF then intel hex file with

avr-gcc -o test.elf test.o -DF_CPU=1000000 -mmcu=atmega328pb -c -B packs/gcc/dev/atmega328pb/

avr-objcopy -O ihex test.elf test.hex

finally, program the microcontroller.  This command attempts to set a very low programming clock speed to be safe.

avrdude -c usbasp -p atmega328pb -B 60 -U flash:w:test.hex






11 comments:

  1. hy,

    do you know a way to program 328pb in windows by using usbasp and arduino?

    ReplyDelete
    Replies
    1. hy, maybe i can answer your question.
      You can program 328pb with custom board or using the arduino uno as board. if you will to upload code into your IC, can use AVRdude to flash it or use the arduino IDE, select programmer to USB ASP.

      Delete
    2. hy,

      have you considered using the LINUX?

      Delete
  2. After study a few of the weblog posts on your web site now, and I actually like your approach of blogging. I bookmarked it to my bookmark web site record and shall be checking back soon. Pls take a look at my web page as nicely and let me know what you think. best blog engine for developers

    ReplyDelete
  3. Hello, I followed all your steps on a mac but when i go to compile i get the following error:

    avr-gcc: error: unrecognized argument in option '-mmcu=atmega328pb'

    I'm not sure where to go next.

    ReplyDelete
  4. Substantially, the article is in reality the freshest on this noteworthy topic. I concur with your conclusions and will certainly eagerly look forward to your forthcoming updates. Saying thanks definitely will not just be sufficient, for the perfect lucidity in your writing. I can right away grab your rss feed to stay abreast of any updates. Fabulous work and also much success in your business endeavors! python string comparison

    ReplyDelete
  5. It?s hard to find knowledgeable people on this topic, but you sound like you know what you?re talking about! Thanks esfileexplorerapkz.info

    ReplyDelete
  6. Hello, Thanks for your post on 328PB. I am getting below error. Any idea what would be wrong? I've checked the connections and I am using the Xplained mini 328PB EVB from Microchip with USBasp connected to ISP 6 pin header. I cannot use the eMDBG port for programming and need to resolve this to use USBasp.

    Thank you.

    ---------------------------------------------

    avrdude.exe: warning: cannot set sck period. please check for usbasp firmware up
    date.
    avrdude.exe: error: programm enable: target doesn't answer. 1
    avrdude.exe: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.


    avrdude.exe done. Thank you.

    ReplyDelete
  7. fuse bit for AVR dude atmega328pb

    ReplyDelete
  8. This ColdFusion probably won't be as cool as chilly combination atomic vitality, yet it introduced an energizing innovation which hold a functioning engineer network together up right up 'til the present time.high price programming in La Habra

    ReplyDelete