C Programming

To write your first C program for the ATmega2560 microcontroller open a new design project in AVR Studio 5 by selecting File>New>Project to bring up the following dialogue window.

New project dialogue C

Under installed templates choose AVR GCC>C and select the C executable project, enter a name for your project e.g. HelloWorldC and click OK. The device selector window will then appear. Select the ATmega2560 as before and click OK.

This will now create your project and open a blank HelloWorldC.c file in the editor.

Paste the following Hello World C code into the editor. This code causes the LED attached to pin 13 of the Arduino board to blink at about 1Hz.

 * AVRGCC_Blink.c
 * Created: 03/11/2011 08:58:43
 * Author: Benn Thomsen

#define F_CPU 16000000UL /* 16 MHz Crystal Oscillator */

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

/* function prototypes */
void delay_ms(uint16_t ms);

void main(void)
 DDRB=0xFF; // Enable all pins as output on port B3:
 PORTB= 0xFF; // Set Port B all HIGH.
 PORTB= 0x00; // Set Port B all LOW.

/* function for long delay */
void delay_ms(uint16_t ms)
 while ( ms )

To compile the code select Build>Build Solution and then simulate it by choosing Debug>Start debugging and break. The contents of the IO registers can be seen in the IO View Pane that appears. If you wish to see the contents of the internal registers then you can select this in Debug>Windows.

2 thoughts on “C Programming

  1. When “#define F_CPU 1000000” and “_delay_ms(1000);” gives a delay of 1000 milli second or 1 second but if we use a external crystal of 16 MHz and write “”#define F_CPU 16000000” and “_delay_ms(1000);” then also we get 1000 milli second delay or other delay value, please explain.

    • _delay_ms() is a macro defined in util/delay.h which is included as part of the avr-gcc and msp-gcc compilers. It provides a blocking delay by looping for a number of clock cycles equivalent to the delay given in ms. For the function to work correctly the CPU clock must first be specified i.e. “#define F_CPU 1000000″.

      If the correct CPU clock rate is specified then the delay should always be approximately as requested i.e. 1000ms in your question.

      For further details of this function see http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html

      Note that using this delay function will only give an approximate delay, interrupts, compiler optimisation and other things can effect the actual delay. It is also wasteful in that it causes the CPU to loop continuously while doing nothing. To get accurate delays or timing it is better to use a timer and interrupts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s