Arduino UNO Programming using AVR-GCC on Linux

Arduino UNO Programming using AVR-GCC on Linux

oleh : Antonius (sw0rdm4n)

www.ringlayer.net

Partners www.indonesianbacktrack.or.id | www.jasaplus.com | www.cr0security.com /* hacking is not just about destruction / attack / spreading fear to others, hacking means to struggle to develop and create something usefull for human being, your power comes from unlimited passion */

This is basic guide for beginner on programming arduino uno using avr-gcc. We will be using Arduino uno board that based on Atmega 328.

uno

Before starting you need to have avrdude, avr-gcc, avr-objcopy installed, on ubuntu it’s easy to install them:

apt-get install gcc-avr avr-libc  binutils-avr gdb-avr avrdude 

Here’s a simple timing led to start.

/* timer led for arduino uno made by Antonius (Sw0rdm4n) http://ringlayer.net Some part of this source taken from http://www.javiervalcarce.eu/wiki/Program_Arduino_with_AVR-GCC */

#include <avr/io.h>
#include <util/delay.h>

void __timer_led(int num)
{
PORTB |= 1<<PB5;
_delay_ms(num);
PORTB &= ~(1<<PB5); _delay_ms(num);
}

int main (void) {
int i = 1;

DDRB = 1 << DDB5;
while (1) {
switch (i) {
case 1:
__timer_led(1000);
break;
case 2:
__timer_led(2000);
break;
case 3:
__timer_led(3000);
break;
case 4:
__timer_led(4000);
break;
case 5:
__timer_led(5000);
break;
}
i++;
if (i > 5)
i = 1;
}
return 0;
}

save it as : timer_led.c

On above example we use PB5, pb5 on atmega328 is pin 13 at arduino (arduino digital pin 8-13 goes to MCU port B). We set DDRB register to use bit DDB5 (in order to use pin 13). PORTB |= 1<<PB5; will set the led on, PORTB |= ~1<<PB5; will set the led off. Next we’re gonna compile it and upload it to our arduino uno. Prepare this simple shell scripting and save at the same folder with timer_led.c :

run.sh :

#!/bin/sh

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o $1.o $1.c

avr-gcc -mmcu=atmega328p $1.o -o $1 avr-objcopy -O ihex -R .eeprom $1 $1.hex

avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:$1.hex

To compile and upload to arduino uno is simple :

uno2

We’ve successfully writing to arduino flash memory. On success the timer led will works as mine here, it has interval 1 untill 5 seconds timer for each led operations :

uno3

 

thanks : Zico, Gunslinger,  Ega, Whitehat, Edward, Eki