Programming Arduino Nano in C Language

 

Objective

 

Environment

 

Install or Update FT232R Driver for WIndows 7



Programming ATmega328P Bootloader


Programming the internal application flash memory area of ATmega328P

Now, it's time to program the internal application flash memory area of the ATmega328p that has the brand new bootloader. We will develop an application program in C language with AtmelStudio 6.2 and program the ATmega328P flash memory using avrdude 6.0.1 instead of Arduino IDE. The avrdude 6.0.1 is installed with Arduino IDE and its executable file location and name are




1. Start AtmelStudio 6.2 IDE.


2. Select File -> New -> Project...menu item.




3. In the New Project dialog box, select C/C++ in the Installed Templates, select GCC C Executable Project, give the project Name and select your project folder Location.



Click OK button to make a project.


4. In the Device Selection dialog box, select megaAVR, 8-bit in the Device Family combo box and select ATmega328P in the device list.


Click OK button once you selected the device.


5. In the Solution Explorer pane, click the current project name Blinky to expand the tree and click the source file name Blinky.c.

The source file pane Blinky.c will be shown in the left side. Make a simple program in C language, which toggles the LED connected to bit 5 of PORTB and transmits a simple message through the USART port.



Example Program in C Language

/*
 * GccApplication1.c
 *
 * Created: 2015-03-17 오후 7:35:34
 *  Author: admin
 */ 

#define F_CPU 16000000UL  // 16 MHz

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

int TxChar(char data, FILE *stream);
char RxChar(FILE *stream);

FILE device = FDEV_SETUP_STREAM(TxChar, NULL, _FDEV_SETUP_WRITE);
FILE device2 = FDEV_SETUP_STREAM(NULL, RxChar, _FDEV_SETUP_READ);

int main(void)
{
	stdout = &device; //printf
	stdin = &device2; //getchar
	
	PORTB=0x00;
	DDRB=0x20;
	
	// USART initialization
	// Communication Parameters: 8 Data, 1 Stop, No Parity
	// USART Receiver: On
	// USART Transmitter: On
	// USART0 Mode: Asynchronous
	// USART Baud Rate: 9600
	UCSR0A=0x00;
	UCSR0B=0x18;
	UCSR0C=0x06;
	UBRR0H=0x00;
	UBRR0L=0x67;
	
    while(1)
    {
        PORTB |= 0b00100000;
        _delay_ms(20);
        
        PORTB &= ~0b00100000;
        _delay_ms(500);
        
        printf("Flash memory was programmed by avrdude.\n"); 
    }
}

int TxChar(char data, FILE * stream)
{
	while(!(UCSR0A & (1 << UDRE0)));
	UDR0 = data;
	return 0;
}

char RxChar(FILE * stream)
{
	while((UCSR0A & 0x80) == 0x00);  
	return UDR0;
}
	

6. Click the Build Blinky icon or select Build -> Build Blink menu item to build the current project.

You can see a build success message after few seconds in the Output pane at the bottom side.



7. Connect the mini USB connector of the Arduino Nano module which has brand new bootloader to the PC using USB cable. Check the COM port number for the future.

avrdude needs one DLL file libusb0.dll and one configuration file avrdude.conf. All of these three files were installed when Arduino IDE was installed but each file is in different folders.

For our convenience it is better to copy three files in one folder - in this example, these files were copied in the following folder.

Select Tools -> External Tools... menu item to let AtmelStudio 6.2 know that we are going to use avrdude as a programming tool.



8. In the External Tools dialog box, enter your favorite avrdude nickname in the Title:, enter the whole path of avrdude.exe folder in the Command:, enter whole argument list needed for using avrdude in the Arguments:, check Use Output window and uncheck Treat output as Unicode.


Click Apply button and click OK button.



9. Select Tools -> avrdude_COM8 menu item.



10. The application's hex file is downloaded to Arduino Nano module and programmed into the internal application flash memory area of ATmega328P.

You can check whether the downloading is OK or not by checking the messages displayed in the Output pane during dwonloading.



11. After downloading completed, you can see the LED is blinking and the message is displayed on Terminal program (9600, 8, N, 1).



NOTE: The connection between Arduino Nano module and the Terminal program must be disconnected before re-programming because the COM port (in this example, COM8) is shared by AtmelStudio 6.2 and the Terminal program.


Congratulations!