Wednesday, 31 May 2017

Programming the Arduino Uno without using Arduino IDE

https://github.com/RealTimeEngineers/Arduino_programming_without_Arduino_IDE


It is possible to program the Arduino Uno without the use of Arduino IDE.

Download the WinAVR tools from the below link

https://sourceforge.net/projects/winavr/files/latest/download

After installation you can find the AVR tools in C:\WinAVR-20100110\bin



Open Notepad and save the below code as blinky.c and place it in some folder say 'test_blinky'


/*
 * Blinks LED on connected to pin 13 (PB5 on Atmega328P)
 *
 * Authors : Jabez Winston C, Hariharan K
 *
 */

#define F_CPU 16e6  // Arduino Uno's ATmega328P runs at 16MHz 

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

int main()
{
DDRB|=(1<<5); //Set PB5 as output by setting 5th bit DDRB register  

 while(1)
 {
 PORTB|=(1<<5);  //Set pin 13 (PB5)- High
 _delay_ms(100); //Wait for 100 ms
 PORTB&=~(1<<5); //Set pin 13 (PB5)- Low
 _delay_ms(100); //Wait for 100ms
 }
return 0; 
//Refer Atmega328P datasheet for details regarding DDRB and PORTB
}


Inside the folder 'test_blinky' ,with SHIFT key pressed ,right click and select Open command window here


Type the following commands on command prompt

avr-gcc -O2 -mmcu=atmega328p blinky.c 
avr-objcopy -O ihex a.out a.hex

The first command compiles the program with optimization level 2 (O2) and


To flash the HEX file (a.hex) to Arduino ,use the below command
avrdude -patmega328p -carduino -PCOM25 -b115200 -D -Uflash:w:a.hex:i

Replace COM25 port by the COM port for your Arduino by looking into the device manager
-Uflash:w:a.hex:i tells the avrdude utility to (w)rite (i)ntel hex file (a.hex) to flash memory.
-b115200 tells at what baud rate avrdude should communicate with the bootloader. Here it is 115200.Changing it to some other value won't work.


To automate the 3 steps, a batch file is written which does the process of compiling,HEX file conversion and flashing the HEX file.Save the below code as build_and_flash.bat in test_blinky folder and run it.

del *.hex *.out

avr-gcc -O2 -mmcu=atmega328p blinky.c 
avr-objcopy -O ihex a.out a.hex
avrdude -patmega328p -carduino -PCOM25 -b115200 -D -Uflash:w:a.hex:i

pause


Complete work is found in this github link
https://github.com/RealTimeEngineers/Arduino_programming_without_Arduino_IDE

How was it found that it works this way ?

Go to File -> Preferences in Arduino IDE



































Enable verbose output during compilation and upload


Arduino IDE showing the commands executed in background during compilation and upload



Arduino's AVR tools can be found in C:\Program Files (x86)\Arduino\hardware\tools\avr\bin . Those tools can also be used instead of WinAVR tools.


The below batch file makes use Arduino AVR tools. Save as build_and_flash_arduino_tools.bat and make  use of it.

@echo off

::Add 'C:\Program Files (x86)\Arduino\hardware\tools\avr\bin' to environment variable PATH
set PATH=C:\Program Files (x86)\Arduino\hardware\tools\avr\bin;%PATH%
 
:: Delete all .hex and .out files 
del *.hex *.out

:: Compile 'blinky.c' for ATmega328p chip using 'avr-gcc' to generate 'a.out' 
avr-gcc -O2 -mmcu=atmega328p blinky.c 

:: Convert 'a.out' to Intel HEX file 'a.hex' using 'avr-objcopy'
avr-objcopy -O ihex a.out a.hex

:: Burn the HEX file(a.hex) to ATmega328 chip on Arduino Uno using 'avrdude'
:: Replace COM25 by the COM port detected on your PC
avrdude -C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf"  -patmega328p -carduino -PCOM25 -b115200 -D -Uflash:w:a.hex:i

::Don't close console after execution
pause