How to program ATmega 328P – PU

arduinoprogrammer

I want to upload lines of code (with c files, not sketches). But sadly I don't own any programmers and only possess the Arduino UNO board. Since I want to program in c code, I can't make use of the Arduino IDE. Is there any way can code can be uploaded to the ATmega IC without using any programmer?

Best Answer

You can use Arduino as ISP programmer. Check the Arduino IDE \$\Rightarrow\$ File \$\Rightarrow\$ Examples \$\Rightarrow\$ ArduinoISP and refer to ArduinoISP documentation.

I personally use Linux and a Makefile to run all the required commands.

In short these are the commands required (Linux, but Windows is pretty similar when the toolchain is installed [and it is if you have the ArduinoIDE on the system])

#edit
# use your favorite text editor to author the source file, then save as `project.cpp`

#variables
src=project
programmerType=arduino
programmerDevice=/dev/.......fill.this.in....
avrFreq=16000000
avrType=attiny45
baudrate=19200
cflags="-g -DF_CPU=$(avrFreq) -Wall -Os -Werror -Wextra -ffunction-sections -fdata-sections"

#compile to object
avr-gcc $cflags -mmcu=$avrType -Wa,-ahlmns=${src)}.lst -c -o ${src}.o ${src}.cpp

#compile to elf
avr-gcc $cflags -mmcu=$avrType -o ${src}.elf ${src}.o

#encode binary file to intelHex
avr-objcopy -j .text -j .data -O ihex ${src}.elf ${src}.flash.hex

#flash the controller
avrdude -p$avrType -c$programmerType -P$programmerDev $(baud) -v -U flash:w:${src}.flash.hex

Seeing the complexity to remember these commands, it really pays off to figure out how to use the linked above makefile on your system:

make help
make edit
make flash