Friday 13 April 2012

A simple Arduino program in Eclipse


Here is a guide to create a simple example project to flash an LED on the Arduino Uno. Once this is working it is a small step to do something with the Game Pack as a whole.

Create an empty project

First create a new AVR project:

  1. File → New → C++ Project
  2. Project name → FlashLED
  3. Project type → AVR Cross Target Application
  4. Toolchains → AVR-GCC Toolchain
  5. Next
  6. Next
  7. AVR Target Hardware Properties

The program we are creating will be run on an Arduino Uno board. This uses an Atmega328P micro controller which operates at 16MHz. Set the following config:

    MCU Type → Atmega328P
    MCU Frequency → 16000000

We now have a blank project set up to compile C++ code into hex code that will run on the Arduino Uno board.

Write some code

If you have previously used one of the Arduino IDEs you will have referenced functions like digitalWrite, pinMode etc. to control the components attached to the board. We do not have access to these functions now (unless we were to copy the code from the IDE into our project) so I have written a simple set of classes to provide some basic functionality to control components attached to the Arduino. You can download these classes here.

(These classes only have very limited functionality as I only implemented what I needed at the time. Using the datasheet for the chip and the arduino schematic to map pins to registers you can extend the functionality as you want.)


The main class you will interact with is ArduinoUno. This class has the following methods:

setDigitalPinMode - sets a pin to either input or output mode.

digitalWrite - sends a high or low signal to the specified pin.

digitalRead - reads a high or low value from the specified pin.

analogueRead - reads a value between 0-255 from an analogue input pin.

startSerial - starts serial communications specifying the pin that will receive data and the pin that will send data. This is useful later for the Game Pack as we will send the button presses and joystick movement data over serial to the TouchShield for processing.

sendSerialData - 2 versions to send a single character or character array over the serial connection created in startSerial.



Copy the 3 folders into your project directory and refresh the project in Eclipse so they show up in the Project Explorer.

Create a new source file in the root directory of the project called 'FlashLED.cpp'

Add the following content to it to flash an LED on and off with an interval of 1 second on pin number 13:

 #include <util/delay.h>  
 #include "hardware/ArduinoUno.h"  
   
 using namespace hardware;  
   
 #define LED_PIN 13  
   
 ArduinoUno arduino;  
   
 int main() {  
   
   arduino.setDigitalPinMode(LED_PIN, OUTPUT);  
   
   while (true) {  
   
     arduino.digitalWrite(LED_PIN, HIGH);  
     _delay_ms(1000);  
     arduino.digitalWrite(LED_PIN, LOW);  
     _delay_ms(1000);  
   }  
   
   return 1;  
 }  


Your project should now look something like:


Build the project

Ensure the FlashLED project is selected in the Project Explorer and click the arrow next to the hammer icon on the menu bar to select 'Release'. This build mode will generate the hex output we will upload to the Arduino. Assuming the build goes ok, the console output should end in 'Build Finished'.

Now we have our code ready to upload to the Arduino, but we need to configure a few settings first.

Configure the upload settings

Right click the FlashLED project in the Project Explorer and select 'Properties'. Expand 'AVR' and click on 'AVRDude'. This is the utility that will upload the code to the Arduino. Check here for more information about AVRDUDE.
  1. Under 'Programmer configuration' select 'New'
  2. Set Configuration name to 'Arduino Uno'
  3. Select 'Arduino' from the Programmer Hardware list
  4. Fill in 'Override default port' with the port ID you will be using.
  5. Click OK
  6. Select your new Arduino Uno configuration
  7. Click OK

Note: If you are unsure which port ID to enter:
  1. Connect your Arduino to your computer with the USB cable
  2. In windows explorer right click 'Computer' and select 'Manage'
  3. Click on 'Device Manager'
  4. Expand 'Ports (COM & LPT)'

You should see 'Communications Port (XXX) listed here. The XXX will be the port ID (e.g. COM3). You can confirm it is the one linked to your Arduino by unplugging the cable and seeing it disappear.

Upload and run the program

Ok now we're good to go. Unplug your Arduino from your Game Pack setup and connect an LED (with an appropriate resistor) to pin 13 and then connect it to your computer via the USB cable.

hardware setup for the FlashLED program


In Eclipse select 'AVR' from the top menu and 'Upload project to target device'. The console output will show avrdude uploading the hex file to the arduino. The LED on the board should now be flashing as the program executes.

No comments:

Post a Comment