rotaryswitch  2.1.0.0
Main Page

Rotary Switch click

PRVIH_PAR_RECENICA_SA_PRODUCT_PAGE_DA_ISPRATE_CELINU

[click Product page](CLICK_PRODUCT_PAGE_LINK)


Click library

  • Author : Stefan Filipovic
  • Date : Jul 2024.
  • Type : I2C type

Software Support

We provide a library for the Rotary Switch Click as well as a demo application (example), developed using MikroElektronika compilers. The demo can run on all the main MikroElektronika development boards.

Package can be downloaded/installed directly from NECTO Studio Package Manager(recommended way), downloaded from our LibStock™ or found on Mikroe github account.

Library Description

This library contains API for Rotary Switch Click driver.

Standard key functions :

Example key functions :

Example Description

This example demonstrates the use of Rotary Switch click board by reading and displaying the switch position on the USB UART.

The demo application is composed of two sections :

Application Init

Initializes the driver and performs the click default configuration.

void application_init ( void )
{
log_cfg_t log_cfg;
rotaryswitch_cfg_t rotaryswitch_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
rotaryswitch_cfg_setup( &rotaryswitch_cfg );
ROTARYSWITCH_MAP_MIKROBUS( rotaryswitch_cfg, MIKROBUS_1 );
if ( I2C_MASTER_ERROR == rotaryswitch_init( &rotaryswitch, &rotaryswitch_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
if ( ROTARYSWITCH_ERROR == rotaryswitch_default_cfg ( &rotaryswitch ) )
{
log_error( &logger, " Default configuration." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}

Application Task

Reads the switch position every 20ms and displays it on the USB UART on position change.

void application_task ( void )
{
static uint8_t old_position = 0xFF;
uint8_t position = 0;
if ( ( ROTARYSWITCH_OK == rotaryswitch_get_position ( &rotaryswitch, &position ) ) &&
( position != old_position ) )
{
Delay_ms ( 20 );
// Double-check for debouncing
if ( ( ROTARYSWITCH_OK == rotaryswitch_get_position ( &rotaryswitch, &position ) ) &&
( position != old_position ) )
{
old_position = position;
log_printf ( &logger, " Switch position: %.1X\r\n", ( uint16_t ) position );
}
}
Delay_ms ( 20 );
}

The full application code, and ready to use projects can be installed directly from NECTO Studio Package Manager(recommended way), downloaded from our LibStock™ or found on Mikroe github account.

Other Mikroe Libraries used in the example:

  • MikroSDK.Board
  • MikroSDK.Log
  • Click.RotarySwitch

Additional notes and informations

Depending on the development board you are using, you may need USB UART click, USB UART 2 Click or RS232 Click to connect to your PC, for development systems with no UART to USB interface available on the board. UART terminal is available in all MikroElektronika compilers.


rotaryswitch_init
err_t rotaryswitch_init(rotaryswitch_t *ctx, rotaryswitch_cfg_t *cfg)
Rotary Switch initialization function.
rotaryswitch_cfg_t
Rotary Switch Click configuration object.
Definition: rotaryswitch.h:159
rotaryswitch_t
Rotary Switch Click context object.
Definition: rotaryswitch.h:145
rotaryswitch_read_reg
err_t rotaryswitch_read_reg(rotaryswitch_t *ctx, uint8_t reg, uint8_t *data_out)
Rotary Switch read reg function.
application_task
void application_task(void)
Definition: main.c:64
rotaryswitch_write_reg
err_t rotaryswitch_write_reg(rotaryswitch_t *ctx, uint8_t reg, uint8_t data_in)
Rotary Switch write reg function.
rotaryswitch_get_position
err_t rotaryswitch_get_position(rotaryswitch_t *ctx, uint8_t *position)
Rotary Switch get position function.
rotaryswitch_default_cfg
err_t rotaryswitch_default_cfg(rotaryswitch_t *ctx)
Rotary Switch default configuration function.
application_init
void application_init(void)
Definition: main.c:28
ROTARYSWITCH_ERROR
@ ROTARYSWITCH_ERROR
Definition: rotaryswitch.h:175
ROTARYSWITCH_MAP_MIKROBUS
#define ROTARYSWITCH_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition: rotaryswitch.h:133
rotaryswitch_cfg_setup
void rotaryswitch_cfg_setup(rotaryswitch_cfg_t *cfg)
Rotary Switch configuration object setup function.
ROTARYSWITCH_OK
@ ROTARYSWITCH_OK
Definition: rotaryswitch.h:174