初学者
10 分钟

通过使用WS2812B-2020、EC12D1564402和STM32L496AG创建互动控制、视觉效果和精确位置显示

通过用户友好的旋钮界面传达精确的位置读数

Rotary RGB Click with Discovery kit with STM32L496AG MCU

已发布 7月 22, 2025

点击板

Rotary RGB Click

开发板

Discovery kit with STM32L496AG MCU

编译器

NECTO Studio

微控制器单元

STM32L496AG

适用于需要触觉和视觉反馈的应用,显示编码器设置的位置或水平,如音量控制、位置传感和用户界面控制。

A

A

硬件概览

它是如何工作的?

Rotary RGB Click基于由16个单独的RGB LED组成的LED环,这些LED来自Worldsemi的WS2812B-2020,以及高质量的ALPS旋转编码器EC12D1564402,可视化地表示编码器位置和更多功能。WS2812B-2020的内部配置包括智能数字端口数据锁存器和信号重塑放大驱动电路。它还包含一个精密的内部振荡器和一个电压可编程恒流控制部分,确保像素点光颜色高度一致。WS2812B-2020是一种具有低驱动电压(使用来自mikroBUS™电源轨的5V作为主要电源)、环保节能、高亮度、大散射角、良好一致性、低功耗、长寿命等优点的LED。此Click板™是开发各种有趣视觉效果的完美解决方案,适用

于任何应用,如灵活的位置、值指示器等。数据传输协议通过mikroBUS™插座的DO和DI引脚使用单一的NZR通信模式。在像素上电复位序列之后,WS2812B-2020的DI端口从主控制器接收数据;第一个像素收集初始24位数据,然后将其发送到内部数据锁存器,其他由内部信号重塑放大电路重塑的数据通过DO端口发送到下一个级联像素。在每个像素的传输之后,信号减少到24位。像素采用自动重塑传输技术,使像素级联数量不受信号传输的限制,只取决于信号传输的速度。EC12D1564402是一个15脉冲增量旋转编码器,带有一个按钮。该编码器具有独特的机械规格(其内部开关的去抖时间下降到2ms),并

且可以承受高达30,000次的开关循环。支持的去抖电路允许在触发输出之前让接触点稳定。旋转编码器时,它在两个mikroBUS™线上(ENA和ENB引脚)输出A和B信号(相互之间不同相位),并通过mikroBUS™插座的SW引脚输出按钮接触信号。四个德州仪器的SN74LVC1T45单比特总线收发器用于编码器和数据传输协议信号的逻辑电平转换。此Click板™可以通过VCC SEL跳线选择3.3V或5V逻辑电压电平运行。这样,既支持3.3V又支持5V的MCU可以正确使用通信线路。此外,此Click板™配备了一个包含易于使用的功能和示例代码的库,可用作进一步开发的参考。

Rotary RGB Click hardware overview image

功能概述

开发板

32L496GDISCOVERY Discovery 套件是一款功能全面的演示和开发平台,专为搭载 Arm® Cortex®-M4 内核的 STM32L496AG 微控制器设计。该套件适用于需要在高性能、先进图形处理和超低功耗之间取得平衡的应用,支持无缝原型开发,适用于各种嵌入式解决方案。STM32L496AG 采用创新的节能架构,集成

了扩展 RAM 和 Chrom-ART 图形加速器,在提升图形性能的同时保持低功耗,使其特别适用于音频处理、图形用户界面和实时数据采集等对能效要求较高的应用。为了简化开发流程,该开发板配备了板载 ST-LINK/V2-1 调试器/编程器,提供即插即用的调试和编程体验,使用户无需额外硬件即可轻松加载、调

试和测试应用程序。凭借低功耗特性、增强的内存能力以及内置调试工具,32L496GDISCOVERY 套件是开发先进嵌入式系统、实现高效能解决方案的理想选择。

Discovery kit with STM32L496AG MCU double side image

微控制器概述 

MCU卡片 / MCU

STM32L496AG Image

建筑

ARM Cortex-M4

MCU 内存 (KB)

1024

硅供应商

STMicroelectronics

引脚数

169

RAM (字节)

327680

使用的MCU引脚

mikroBUS™映射器

Encoder Output B
PA4
AN
Data OUT
PB2
RST
ID COMM
PG11
CS
Data IN
PI1
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Encoder Output A
PA0
PWM
Switch Output
PH2
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

Rotary RGB Click Schematic schematic

一步一步来

项目组装

Discovery kit with STM32H750XB MCU front image hardware assembly

从选择您的开发板和Click板™开始。以Discovery kit with STM32L496AG MCU作为您的开发板开始。

Discovery kit with STM32H750XB MCU front image hardware assembly
Thermo 21 Click front image hardware assembly
Prog-cut hardware assembly
Thermo 21 Click complete accessories setup image hardware assembly
Board mapper by product7 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Discovery kit with STM32H750XB MCU NECTO MCU Selection Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto image step 11 hardware assembly

软件支持

库描述

该库包含 Rotary RGB Click 驱动程序的 API。

关键功能:

  • rotaryrgb_set_led_pos_color - 此函数为选定的LED位置设置所需的颜色。

  • rotaryrgb_set_all_leds_data - 此函数使用GPIO协议写入16个元素的数据数组以控制所有LED。

  • rotaryrgb_get_state_switch - 此函数返回旋转编码器开关信号,即SW(INT)引脚的状态。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * @file main.c
 * @brief Rotary RGB Click Example.
 *
 * # Description
 * This library contains the API for the Rotary RGB Click driver 
 * to control LEDs states and a rotary encoder position readings.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initialization of GPIO module and log UART.
 * After the driver init, the app turn off all LEDs.
 *
 * ## Application Task
 * This example demonstrates the use of the Rotary RGB Click board.
 * The demo example shows the functionality of a rotary encoder used to control RGB LEDs.
 * The switch controls the application of the colors,
 * and the encoder mechanism controls the state of the LEDs.
 *
 * ## Additional Function
 * - static void rotaryrgb_logic_zero ( void )
 * - static void rotaryrgb_logic_one ( void )
 * - static void rotaryrgb_switch_detection ( void )
 * - static void rotaryrgb_encoder_mechanism ( void )
 *
 * @note
 * Make sure the logic delays are defined for your system in the rotaryrgb_delays.h file.
 *
 * @author Nenad Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "rotaryrgb.h"
#include "rotaryrgb_delays.h"

static rotaryrgb_t rotaryrgb;   /**< Rotary RGB Click driver object. */
static log_t logger;            /**< Logger object. */

static uint8_t start_rot_status = 0;
static uint8_t led_color_sel = 0;
static uint8_t old_state = 0;
static uint8_t new_state = 1;
static uint8_t old_rot_state = 0;
static uint8_t new_rot_state = 1;
static uint16_t led_pos = 1;
static uint32_t demo_color_table[ 8 ] = 
{ 
    ROTARYRGB_COLOR_WHITE_50, 
    ROTARYRGB_COLOR_RED_50, 
    ROTARYRGB_COLOR_GREEN_50, 
    ROTARYRGB_COLOR_BLUE_50, 
    ROTARYRGB_COLOR_LIGHT_BLUE_50, 
    ROTARYRGB_COLOR_YELLOW_50, 
    ROTARYRGB_COLOR_PURPLE_50, 
    ROTARYRGB_COLOR_OFF 
};

/**
 * @brief Rotary RGB logic zero function.
 * @details This function generates a logic zero sequence char 
 * to control the LED light source.
 * @return Nothing.
 */
static void rotaryrgb_logic_zero ( void );

/**
 * @brief Rotary RGB logic one function.
 * @details This function generates a logic one sequence char 
 * to control the LED light source.
 * @return Nothing.
 */
static void rotaryrgb_logic_one ( void );

/**
 * @brief Rotary RGB switch detection function.
 * @details This function is used for the switch state detection.
 * @return Nothing.
 */
static void rotaryrgb_switch_detection ( void );

/**
 * @brief Rotary RGB encoder mechanism function.
 * @details This function is used to control the state of the LEDs 
 * by detecting the rotation direction of the rotary encoder.
 * @return Nothing.
 */
static void rotaryrgb_encoder_mechanism ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    rotaryrgb_cfg_t rotaryrgb_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    rotaryrgb_cfg_setup( &rotaryrgb_cfg, &rotaryrgb_logic_zero, &rotaryrgb_logic_one );
    ROTARYRGB_MAP_MIKROBUS( rotaryrgb_cfg, MIKROBUS_1 );
    if ( DIGITAL_OUT_UNSUPPORTED_PIN == rotaryrgb_init( &rotaryrgb, &rotaryrgb_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    rotaryrgb_set_all_led_color( &rotaryrgb, ROTARYRGB_COLOR_OFF );
    Delay_ms ( 100 );

    log_info( &logger, " Application Task " );
    Delay_ms ( 100 );
}

void application_task ( void ) 
{
    rotaryrgb_set_led_pos_color( &rotaryrgb, led_pos % 17, demo_color_table[ led_color_sel ] );
    rotaryrgb_switch_detection( );
    rotaryrgb_encoder_mechanism( );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void rotaryrgb_logic_zero ( void )
{
    hal_ll_gpio_set_pin_output( &rotaryrgb.di_pin.pin );
    DELAY_TOH;
    hal_ll_gpio_clear_pin_output( &rotaryrgb.di_pin.pin );
    DELAY_TOL;
}

static void rotaryrgb_logic_one ( void )
{
    hal_ll_gpio_set_pin_output( &rotaryrgb.di_pin.pin );
    DELAY_T1H;
    hal_ll_gpio_clear_pin_output( &rotaryrgb.di_pin.pin );
    DELAY_T1L;
}

static void rotaryrgb_switch_detection ( void )
{
    if ( rotaryrgb_get_state_switch( &rotaryrgb ) ) 
    {
        new_state = 1;
        if ( ( 1 == new_state ) && ( 0 == old_state ) ) 
        {
            old_state = 1;
            led_color_sel++;
            if ( 7 < led_color_sel ) 
            {
                led_color_sel = 0;
            }
        }
    }
    else 
    {
        old_state = 0;
    }
}

static void rotaryrgb_encoder_mechanism ( void )
{
    if ( rotaryrgb_get_state_enb( &rotaryrgb ) == rotaryrgb_get_state_ena( &rotaryrgb ) ) 
    {
        old_rot_state = 0;
        start_rot_status = rotaryrgb_get_state_enb( &rotaryrgb ) && rotaryrgb_get_state_ena( &rotaryrgb );
    }
    else 
    {
        new_rot_state = 1;
        if ( new_rot_state != old_rot_state ) 
        {
            old_rot_state = 1;
            if ( start_rot_status != rotaryrgb_get_state_enb( &rotaryrgb ) ) 
            {
                led_pos++;
            }
            else 
            {
                led_pos--;
            }

            if ( 0 == led_pos % 17 )
            {
                Delay_ms ( 1 );
                rotaryrgb_set_all_led_color( &rotaryrgb, ROTARYRGB_COLOR_OFF );
            }
        }
    }
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。