Write Your First Code
1. Create a New Project
- Make sure you followed the steps in Creating a New Project to set up a new project
2. Write Your Code
- Make sure the main source file (e.g.,
main.c
) is opened - Replace content from
main.c
file with the simple code example below, which showcases blinking an LED
/**
* If hardware without pin `MIKROBUS_1_CS` is used,
* make sure to define adequate pin.
*/
#ifndef MIKROBUS_1_CS
#define MIKROBUS_1_CS NC
#endif
/* LED pin. */
#define LED MIKROBUS_1_CS
/* Digital output driver context structure. */
static digital_out_t output_pin;
int main(void) {
/* Set pin LED as digital output. */
digital_out_init( &output_pin, LED );
/* Infinite loop. */
while ( 1 ) {
/* Toggle pin digital state. */
digital_out_toggle( &output_pin );
/* 1 second delay. */
Delay_1sec();
}
return 0;
}
Let's break down and explain the code example that showcases a simple LED blinking using the mikroSDK 2.0 GPIO driver library.
Project Overview
-
The provided code demonstrates how to create a simple LED blinking application using the mikroSDK 2.0 framework
-
The example toggles a specified LED pin on a development board
-
Code Explanation
-
Library Inclusions
This code includes the necessary libraries:
**MikroSDK.Driver.GPIO.Out**
: The GPIO driver library for handling digital output-
**MikroSDK.Board**
: The board definition library -
Pin Definition
// If hardware without pin `MIKROBUS_1_CS` is used,
// make sure to define adequate pin.
#ifndef MIKROBUS_1_CS
#define MIKROBUS_1_CS NC
#endif
Defines the MIKROBUS_1_CS
pin if it is not already defined.
If your hardware doesn't have this specific pin, it defaults to NC
(not connected).
- LED Pin Definition
The LED pin is defined as MIKROBUS_1_CS
.
- Digital Output Context
A context structure output_pin
for the digital output driver is defined.
- Main Function
int main(void) {
/* Set pin LED as digital output. */
digital_out_init( &output_pin, LED );
/* Infinite loop. */
while ( 1 ) {
/* Toggle pin digital state. */
digital_out_toggle( &output_pin );
/* 1 second delay. */
Delay_1sec();
}
return 0;
}
The entry point of the program.
Initializes the LED
pin as a digital output using digital_out_init
.
Continuously toggles the state of the LED
pin and waits for 1 second using Delay_1sec()
. This creates the blinking effect.
This code example provides a simple way to blink an LED using mikroSDK 2.0's GPIO driver library.
It sets up the necessary hardware configurations, initializes the LED pin as a digital output, and toggles the LED state in an infinite loop with a delay of 1 second.
By following the steps to configure the Library Manager, you can ensure all necessary dependencies are included for successful compilation and execution.
For additional resources, check out:
3. Configure Library Manager
After copying the previous code into main.c
file of the Application type of a NECTO Studio project, you need to configure the necessary libraries in the Library Manager:
-
Open Library Manager
- Navigate to the Library Manager in NECTO Studio.
-
Check Required Libraries
- Make sure to check Board and Driver.GPIO.Out libraries
CONGRATULATIONS!
You have written your first code in NECTO Studio! Now, onto the Build Your Project
section!