Programming AVR 328p Inside a Arduino With MysmartUSB ISP
by nikkischulz6 in Circuits > Arduino
287 Views, 1 Favorites, 0 Comments
Programming AVR 328p Inside a Arduino With MysmartUSB ISP
If you ever have asked yourself if you can just plug in a ISP SPI Programm onto the ISP Pins on the Arduino Board - here is a test, that should show that it is possible.
1.) Write LED blink Test
2.) Start the toolchain
3.) Program
4.) Be happy with the result
Supplies
- Arduino UNO Board
- mySmartUSB light
- patiente
- a LED of course without resistance - because we want to see the LED be destroyed
- (By using a resistor to limit the current, the LED will be protected and the microcontroller will function correctly. )
- A PC
Write a Simple LED Blink Program for Avr328p
write the code into main.c
nano main.c
or with gedit or wathever
It will toglle all Pins on/off
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
int main(){
DDRB=0xFF; // output for all
while (1){
PORTB = 0xFF;
_delay_ms(250);
PORTB =0x00;
_delay_ms(250);
}
}
Program Bare- Metal Onto the 328p
Use this makefile.
you need to create a file called "makefile" and add this text inside.
Here are the files:
$ ls
main.c makefile
The Makefile content:
Variables are defined in the beginning
- PORT: First it defines Variable like the port where the mysmart usb is inside
- MCU: the target architecture! here atmega328p
- CFLAGS/LDFLAGS: Or the compiler flags , and linker flags.
- CC: it also defines which compiler is used: avr-gcc
- avr-gcc or $(CC) followed by flags means that it will use the compiler to create objects files out of the main.c
- avr-objcopy is there to transform the object file into a hex file
- avrdude is there to write the hex file into the atmega!
PORT=/dev/ttyUSB1
MCU=atmega328p
CFLAGS=-g -Wall -mcall-prologues -mmcu=$(MCU) -Os
LDFLAGS=-Wl,-gc-sections -Wl,-relax
CC=avr-gcc
TARGET=main
OBJECT_FILES=main.o
all: $(TARGET).hex
clean:
rm -f *.o *.hex *.obj *.hex
%.hex: %.obj
avr-objcopy -R .eeprom -O ihex $< $@
%.obj: $(OBJECT_FILES)
$(CC) $(CFLAGS) $(OBJECT_FILES) $(LDFLAGS) -o $@
program: $(TARGET).hex
avrdude -p $(MCU) -c stk500 -P $(PORT) -U flash:w:$(TARGET).hex
- Store the Makefile and run a console and type: make
- What happens is it will run avr-gcc and avr-objcpy
Here are the files:
$ls
main.c main.hex makefile
- Then type in make program
- And it will run the avrdude
$ make program
avrdude -p atmega328p -c stk500 -P /dev/ttyUSB1 -U flash:w:main.hex
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.01s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "main.hex"
avrdude: input file main.hex auto detected as Intel Hex
avrdude: writing flash (154 bytes):
Writing | ################################################## | 100% 0.07s
avrdude: 154 bytes of flash written
avrdude: verifying flash memory against main.hex:
avrdude: load data flash data from input file main.hex:
avrdude: input file main.hex auto detected as Intel Hex
avrdude: input file main.hex contains 154 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.09s
avrdude: verifying ...
avrdude: 154 bytes of flash verified
avrdude: safemode: Fuses OK (E:FD, H:DE, L:C2)
avrdude done. Thank you.
Result
No just connect the Arduino Uno with the standard USB B cable to check if the LED blinks..
In the video you see that it worked...