0
\$\begingroup\$

I bought couple ATMEGA328P-PU as a replacement for my Arduino Uno. The problem is that I don't have any cables to program to 328 directly and I'm trying to avoid using Arduino and it's bootloader. I want to write code in C, but I don't know where to start..

I'm trying to figure out two things:

  1. How to build my own USB cable to write bootloader & code to the ATMEGA328P-PU?
  2. What software do I use to get started with the bootloader (Linux)?

Any help would be much appreciated, thanks!

\$\endgroup\$
  • \$\begingroup\$ Do you have anything other than an arduino? Somethink like this perhaps? fischl.de/usbasp \$\endgroup\$ – bitshift Jul 18 '16 at 19:53
  • \$\begingroup\$ What country are you located in? \$\endgroup\$ – FiddyOhm Jul 18 '16 at 21:56
  • \$\begingroup\$ Note that you don't have to use anything else from Arduino in order to make use of its bootloader to get a bare-metal C program onto the chip. \$\endgroup\$ – Chris Stratton Jul 19 '16 at 3:37
1
\$\begingroup\$

This is not intended to be a full answer to your question, but hopefully it's enough to get you going in the right direction. I mention some specific pieces of software in here, but I'm sure there are alternatives available for everything I mention.

Atmel AVRs (most, anyway) are programmed via a mechanism known as In-circuit Serial Programming, or ISP. To program your chip (which includes installing a bootloader), you need an ISP programmer.

If you just want to use your chip as a replacement Arduino and program it using the Arduino language (which is C++ by the way, not C), you can easily do this using your Arduino with the ArduinoISP sketch. You need to hook up your chip to the Arduino in a specific way, and then you should be able to burn the Arduino bootloader to it using ArduinoISP. More details are on the Arduino site.

If you want to program the AVR as a stand-alone unit, you'll need to program it using ISP for the main program. You can also use your Arduino with the ArduinoISP sketch for that, but you'll have to use a programming tool such as avrdude (the actual AVR programming tool that Arduino uses) directly. You don't need a bootloader on your chip for this.

I ended up with something like this:

avrdude -p m328p -c stk500v1 -e -P /dev/ttyUSB0 -b 19200 -U flash:w:yourprogram.hex:i

The yourprogram.hex files are compiled binary files, you can generate them by running your C programs through avr-gcc. You could also use the Arduino IDE (which uses avr-gcc internally) to generate them for you. It dumps them somewhere in /tmp/ where you can snatch them.

In either case, you'll have to learn about fuse bits, and how to program them. There are plenty of fuse calculators out there that can help you determine what fuse settings you need, and you can actually program them using avrdude.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for the detailed answer! if I want to use avrdude directly, would the 'programmer' be USB or Serial port? \$\endgroup\$ – K666 Jul 18 '16 at 23:35
  • 1
    \$\begingroup\$ The USB is used to emulate a serial port, so as far as avrdude is concerned it is a serial program, not a USB one. \$\endgroup\$ – Chris Stratton Jul 19 '16 at 3:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.