Counter 2 Click
Counter 2 Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.
Click Library
- Author : Stefan Filipovic
- Date : May 2025.
- Type : I2C type
Software Support
Example Description
This example demonstrates the use of the Counter 2 Click board. The application reads the current pulse count from the board and handles rollover events using the loop counter mechanism. It logs the total number of detected pulses, including those beyond the register's capacity.
Example Libraries
- MikroSDK.Board
- MikroSDK.Log
- Click.Counter2
Example Key Functions
- counter2_cfg_setup This function initializes Click configuration structure to initial values.
void counter2_cfg_setup(counter2_cfg_t *cfg)
Counter 2 configuration object setup function.
Counter 2 Click configuration object.
Definition counter2.h:142
- counter2_init This function initializes all necessary pins and peripherals used for this Click board.
err_t counter2_init(counter2_t *ctx, counter2_cfg_t *cfg)
Counter 2 initialization function.
Counter 2 Click context object.
Definition counter2.h:122
- counter2_reset_counter This function performs a hardware counter reset by toggling the RST pin and synchronizing with the LOOP pin.
void counter2_reset_counter(counter2_t *ctx)
Counter 2 reset counter function.
- counter2_get_counter This function reads the current 24-bit counter value via I2C.
err_t counter2_get_counter(counter2_t *ctx, uint32_t *counter)
Counter 2 get counter value function.
- counter2_check_loop This function checks if the LOOP pin state has changed since the last call.
uint8_t counter2_check_loop(counter2_t *ctx)
Counter 2 check loop toggle function.
Application Init
Initializes the logger and the Click board driver and resets the internal pulse counter.
{
log_cfg_t log_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
if ( I2C_MASTER_ERROR ==
counter2_init( &counter2, &counter2_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}
#define COUNTER2_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition counter2.h:108
void application_init(void)
Definition main.c:34
Application Task
Periodically reads the counter value and checks for rollover (loop) events. Logs the total pulse count every second.
{
static uint8_t loop_cnt = 0;
uint32_t counter = 0;
{
log_printf ( &logger, " Counter: %lu\r\n\n",
Delay_ms ( 1000 );
}
}
@ COUNTER2_OK
Definition counter2.h:160
#define COUNTER2_LOOP_THRESHOLD
Definition counter2.h:83
void application_task(void)
Definition main.c:66
Note
Ensure that the input signal to the counting channel is within electrical and timing specifications to avoid missed or incorrect counts.
Application Output
This Click board can be interfaced and monitored in two ways:
- Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
- UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.
Additional Notes and Information
The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.