How to program Arduino Due using ATMEL studio 7 | applied electronics engineering

Latest

How to program Arduino Due using ATMEL studio 7

By Applied Electronics - Friday, March 17, 2017 No Comments
In this tutorial you will learn how to program Arduino Due using ATMEL Studio 7. We will be using ASF(ATMEL Software Framework) for this because ASF makes it easier to write program for ARM based microprocessor that Arduino Due uses.

To illustrate how to do this, we will write a simple LED blinking program.

1. Create a new GCC C ASF Board Project

First we will create a new project using File > New Project. In the new project window, we select a new GCC C ASF Board Project. We provide some name for the project like LEDBlink in this case and save in some folder.


In the next window choose select by board option and in the search field type arduino. You will see Arduino Due/C ATSAM3X8E. Choose that and click on OK.


Now the Solution/Project will be created. Next click on the src folder then main.c. You should see bare minimum code in the code editor pane.


The minimum code is as follows.

#include <asf.h>

int main(void)
{
    board_init();
}

In this project setup using predefined board couple of services has already been added. To view the services available to you click on the ASF folder, then services as shown below.





You can see the clock, gpio and ioport folders. Inside these we can see header files that allows us later in the program to use the clock setting service, the gpio services and ioport services.





2. Add Delay Service provided by ASF

The benefit of using ASF is using its services that allows us to write program code faster. In this program, we want to create LED blinking program. So we some delay between the ON and OFF state. We can either create our own delay or we can use ASF service. Here we will use the Delay service provided by ASF.

To add the Delay Service, click on ASF > ASF Wizard. Then in the service search and import window, type delay in the search field. You should see Delay Routines(services).


Click on the Add>> button.

Now the Delay routines(services) will be added to the Selected Modules window.






Click Apply and then OK. Now you can close the ASF Wizard window.

Now if you look at the ASF > common > services again you should see the delay has been added.



Now we can use the function delay_ms() defined in the delay.h header file.

3. Writing the LED blinking Program

Now we all the required functions via services/module to write the LED blinking Program.The program code is below.

#include <asf.h>

int main (void)
{
    /* Insert system clock initialization code here (sysclk_init()). */
    sysclk_init();
    board_init();

    /* Insert application code here, after the board has been initialized. */
    while (1) {
        ioport_toggle_pin_level(LED0_GPIO);
        delay_ms(400);
    }
}

See this in the figure below.


 So the first thing we did is to add sysclk_init() function. This is clock initialization is required. Then in the main while(1) loop we have add ioport_toggle_pin_level(LED0_GPIO). This ioport_toggle_pin_level() function is used to toggle the pin output between 1 and 0. The function is defined in the IOPORT service which had been already added during the project creation. This function requires the name of the pin . The name of the pin is called LED0_GPIO which is the digital pin 13 on the Arduino Due board. Now to check where to find the pin name, you have to see the board header file called arduino_due_x.h located in the folder ASF > SAM > boards > arduino_due_x.






Open the arduino_due_x.h header file and search for LED0. You should see that PIO_PB27_IDX is defined as LED0_GPIO which is the required name of the digital pin 13.


Next we have used the delay_ms(400). This function is available to us because we added the delay service using the ASF wizard earlier. The 400 as parameter means 400ms. This means that there is 400ms delay between the LED ON and OFF state.

Now if you Build/Compile and upload the program to the Arduino Due, a LED connected to the digital pin 13 should blink.

Tags:

No Comment to " How to program Arduino Due using ATMEL studio 7 "