Embedded System - Arduino

 

 

 

 

Programming in C

 

In this page, I will show you how to program  Arduino in pure C code.  This is not about the C programming itself. I assume that the reader already has a certain degree of C programming experience or familiarity. If you are new to C language itself, you can find a lot of tutorials from the internet.  What I am going to talk about is how to make C program runnable on Arduino board. This is a kind of information that you would not get easily anywhere eslse. I have searched a lot and found some of useful informations but none of them worked smoothly as I expected. I don't think it is because of those documents... it is mainly because the information has been a little out-dated (as of Jul 2016) and Arduino programming environment has somewhat been changed. However, most of those information was very helpful. It just needed some of the modification depending on Arduino IDE version or type of operating system, but most of those documents were missing with any tips about what kind of modification you would need to make it work with your own specific environement.

I think the contents on this page would not be anything super better than any other materials that you can find in the internet. Also, the examples (except to C source code) may or may not work if you just blindly copy and past it into your environment. So try to follow along the overall logic rather than trying to blindly copy and paste the command / code printed in this page.

 

The first thing you need to know about programming in C for Arduino is to know how to compile and upload the binary code onto Arduino board. Following two sections about about this.

 

 

Compile and Uploading with Arduino IDE

 

Somehow I don't find much recent information about C programming in Arduino. Most of the information that I found (in Jul 2016) were thone posted a couple of years ago and seems to require several complicated steps to make it work. But I noticed that in recent Arduino IDE (e.g, Arduino 1.6.7) I don't need to do any special to write in C code. Just write down the C code in IDE and Press Start button exactly just like you are doing normal Arduino programming. It just worked.

 

 

 

Compile and Uploading with Command Window

 

What I am going through in this section would be the most common information that you may find from internet in terms of C programming process in arduino. Since C programming is possible directly from Arduino IDE, I would not use this method so often but going through this procedure would help you to understand exactly what's going on when press [Run] button in Arduino GUI. Basically this is what's going on in the background of the process illustrated in Build and Upload Process section of the Arduino Programming page. The environement for this example is Arduino IDE running on Windows 7.

 

Step 1 : Go to the directory where you put the C source code

 

First go to the directory where the C source file is located (I created a C source file name as Blink.c). The content of the directory is as shown below.

 

C:\Arduino\C\Blink>dir

 Volume in drive C is Windows

 Volume Serial Number is F8D3-F022

 

 Directory of C:\RyuCloud\Arduino\C\Blink

 

07/23/2016  01:45 AM    <DIR>          .

07/23/2016  01:45 AM    <DIR>          ..

07/23/2016  01:36 AM               373 Blink.c

               1 File(s)            373 bytes

               2 Dir(s)  63,801,536,512 bytes free

 

 

Step 2 : Comple the source code

 

Next step is to compile the C code. The complier program is avr-gcc. Since the location of the folder containing the avr-gcc is not added in the Windows PATH variable, you need to specify the full Path of the program as shown below.

 

C:\RyuCloud\Arduino\C\Blink>"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-gcc" -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o Blink.o Blink.c

 

NOTE 1 : The location of the compiler might be different depending on Arduino IDE version and installation directly. So before you try this and find out exact location of the folder. In Windows, you may use Windows File Explorer and search avr-gcc. In Linux, you may try 'ls -al -R | grep "avr-gcc"'

 

After the compliation is complete, check the contents of the folder again and you will notice that following file is created.

 

C:\Arduino\C\Blink>dir

 Volume in drive C is Windows

 Volume Serial Number is F8D3-F022

 

 Directory of C:\Arduino\C\Blink

 

07/23/2016  01:51 AM    <DIR>          .

07/23/2016  01:51 AM    <DIR>          ..

07/23/2016  01:36 AM               373 Blink.c

07/23/2016  01:51 AM               924 Blink.o

               2 File(s)          1,297 bytes

               2 Dir(s)  63,798,775,808 bytes free

 

 

Step 3 : Link the object code

 

Now link the object file as shown below.

 

C:\Arduino\C\Blink>"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-gcc" -mmcu=atmega328p Blink.o -o Blink

 

Now you have a new file created as shown below.

 

C:\Arduino\C\Blink>dir

 Volume in drive C is Windows

 Volume Serial Number is F8D3-F022

 

 Directory of C:\RyuCloud\Arduino\C\Blink

 

07/23/2016  01:53 AM    <DIR>          .

07/23/2016  01:53 AM    <DIR>          ..

07/23/2016  01:53 AM             2,184 Blink

07/23/2016  01:36 AM               373 Blink.c

07/23/2016  01:51 AM               924 Blink.o

               3 File(s)          3,481 bytes

               2 Dir(s)  63,795,249,152 bytes free

 

 

Step 4 : Convert the linked file into .hex format

 

Now you convert the linked file into a binary file that can be directly loaded to the EEPROM of the Arduino board as shown below.

 

C:\Arduino\C\Blink>"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objcopy" -O ihex -R .eeprom Blink Blink.hex

 

Now you see the new file in hex format created as shown below.

 

C:\Arduino\C\Blink>dir

 Volume in drive C is Windows

 Volume Serial Number is F8D3-F022

 

 Directory of C:\Arduino\C\Blink

 

07/23/2016  01:56 AM    <DIR>          .

07/23/2016  01:56 AM    <DIR>          ..

07/23/2016  01:53 AM             2,184 Blink

07/23/2016  01:36 AM               373 Blink.c

07/23/2016  01:56 AM               508 Blink.hex

07/23/2016  01:51 AM               924 Blink.o

               4 File(s)          3,989 bytes

               2 Dir(s)  63,795,404,800 bytes free

 

 

Step 5 : Upload the hex file into Arduino board

 

Now the last step is to upload the hex file onto the board and this was the step that gave me a lot of trouble and it took me long time for me to find the solution to make this work.  The problem at the beginning turned out to be because I missed out the option  -C "C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" . So don't forget to specify this option. Also keep in mind that the location of the file avrdude.conf may be different in your environement. Find the exact location of this file first before you try this command.  If you are really motivated, open up this file and try to decipher the meaning of the all the information in the file.

 

C:\RyuCloud\Arduino\C\Blink>"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude" -C "C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" -F -V -c arduino -p ATMEGA328P -P COM3 -b 115200 -U flash:w:Blink.hex

 

If all the options typed in the command line is correct, you will get the print outs as shown below, otherwise you may get some error message. Once you get the print out as shown below, you will see the program running on the board. (In this example, Blink.c has the code blinking the onboard LED connected to PIN 13 on Arduino Uno).

 

avrdude: AVR device initialized and ready to accept instructions

 

Reading | ################################################## | 100% 0.01s

 

avrdude: Device signature = 0x1e950f

avrdude: NOTE: "flash" memory has been specified, an erase cycle will be perform

ed

         To disable this feature, specify the -D option.

avrdude: erasing chip

avrdude: reading input file "Blink.hex"

avrdude: input file Blink.hex auto detected as Intel Hex

avrdude: writing flash (176 bytes):

 

Writing | ################################################## | 100% 0.05s

 

avrdude: 176 bytes of flash written

 

avrdude: safemode: Fuses OK (H:00, E:00, L:00)

 

avrdude done.  Thank you.

 

 

 

Example 1 >

 

Blink ===============================================================

 

#include <avr/io.h>

#include <util/delay.h>

 

#define BLINK_DELAY_MS 2000

 

int main (void)

{

 DDRB |= _BV(DDB5);

 

 while(1) {

  PORTB |= _BV(PORTB5);

  _delay_ms(BLINK_DELAY_MS);

 

  PORTB &= ~_BV(PORTB5);

  _delay_ms(BLINK_DELAY_MS);

 }

}

 

 

Blink ================================================================

 

#include <avr/io.h>

#include <util/delay.h>

 

#define BLINK_DELAY_MS 2000

 

int main (void)

{

 

 DDRB |= (1 << 5);

 

 while(1) {

 

   PORTB |= (1 << 5);

  _delay_ms(BLINK_DELAY_MS);

 

  PORTB &= ~(1 << 5);

  _delay_ms(BLINK_DELAY_MS);

 }

}

 

 

Blink =================================================================

 

#include <avr/io.h>

#include <util/delay.h>

 

#define BLINK_DELAY_MS 1000

 

int main (void)

{

 

 DDRB |= (0x20);

 

 while(1) {

  PORTB |= (0x20);

  _delay_ms(BLINK_DELAY_MS);

 

  PORTB &= 0xDF; // you can do PORTB &= ~(0x20)

  _delay_ms(BLINK_DELAY_MS);

 }

}

 

 

Reference :

 

[1] Programming Arduino Uno in pure C