Your first application¶
In this section you will learn:
- Creating a mikroSDK application project
- Writing the LED blinking application with mikroSDK
After you successfully installed mikroSDK package following these steps, you can start developing a portable mikroSDK application.
Creating mikroSDK application project¶
Before we start writing any sort of code, we need to create our project in
NECTO Studio. Follow these
steps in order to
see how to create a basic mikroSDK application project from the template.
Project template creates basic mikroSDK application structure with main()
function. Choose a profile which contains mikroSDK v2.
More information about profiles in NECTO Studio can be found here.
Writing the LED blinking application with mikroSDK¶
Basic mikroSDK application includes board.h
header. This header comes with
mikroSDK package and it defines your development board. It helps by mapping
board pins to the target MCU. For example, you can use macro pin
name MIKROBUS_1_CS
to identify CS pin on mikroBUS 1 slot, without needing to
know exact pin name on the target MCU.
Before we start writing the LED blinking code, we need to include
Digital Out Driver in our project.
The basic mikroSDK application template selects MikroSDK.Driver
library for
you, so only thing you need to do is to include the driver header.
Please refer to this page to see more details about selected libraries as well as learn how to include other ones.
#include "drv_digital_out.h"
The digital out driver needs an instance to store its state, so let us define it
for our GPIO pin used to drive a LED, and call it led
. Add this line after the
include statements.
static digital_out_t led;
Before we change state for our GPIO pin we need to initialize our driver
instance. Add this function call to application_init()
function body.
void application_init()
{
digital_out_init(&led, LED1);
}
Second parameter tells the driver which pin we want to associate our driver instance with.
Attention
If your board doesn't define LED1
pin, change that to a pin defined on
your board.
Info
To see which pins are defined by your board, find the include "board.h"
line
in your code, position your cursor on it, right click and select "Goto
Definition".
Main logic of our LED blinking application we will implement in
application_task()
function body. For that purpose we will use
digital_out_toggle()
driver function to toggle the state of digital output pin.
For delay we will use a mikroC built-in function.
void application_task()
{
digital_out_toggle(&led);
Delay_ms(1000);
}
To create an executable file, start build in NECTO Studio, and that's it. We created our first LED blink application.
Complete code listing¶
/**
* @file main.c
* @brief MikroSDK base application.
*/
#include "board.h"
#include "drv_digital_out.h"
static digital_out_t led;
/// @brief Application init function.
void application_init ()
{
digital_out_init(&led, LED1);
}
/// @brief Application task.
void application_task ()
{
digital_out_toggle(&led);
Delay_ms(1000);
}
/// @brief Application main function.
void main (void)
{
application_init();
while (1)
{
application_task();
}
}