初学者
10 分钟

使用TLC5925和ATmega328为电子项目添加旋钮和视觉反馈

在各种应用中创建视觉效果和指示器

Rotary W 2 Click with Arduino UNO Rev3

已发布 6月 27, 2024

点击板

Rotary W 2 Click

开发板

Arduino UNO Rev3

编译器

NECTO Studio

微控制器单元

ATmega328

通过提供一个带有16个白色LED环的精密输入旋钮,并提供视觉反馈,来增强电子设计。

A

A

硬件概览

它是如何工作的?

Rotary W 2 Click基于德州仪器的TLC5925,这是一款低功率的16通道恒流LED沉降驱动器,结合了来自ALPS的高品质旋转编码器EC12D1564402,使您能够在设计中添加一个精密的输入旋钮。EC12D1564402增量旋转编码器被16个绿色LED环所包围,其中一个单独的旋转被分成15个离散步骤(与电位器相反,旋转编码器可以连续旋转)。驱动器可以单独控制每个LED,允许编程各种照明效果。编码器在两个mikroBUS™线上输出A和B信号(相位

差),同时旋钮按钮特性通过中断线输出。EC12D1564402是一个带有按钮的15脉冲增量旋转编码器。该编码器具有独特的机械规格(其内部开关的去抖时间为2ms),并且可以承受大量的切换周期,高达30,000次。支持去抖动电路允许接触在输出完全触发之前稳定下来。Rotary W 2 Click使用TLC5925 LED驱动器的标准4线SPI串行接口与主机MCU通信,支持最高30MHz的时钟频率。旋转编码器旋转时,在两个mikroBUS™线,即mikroBUS™

插座的ENA和ENB引脚上输出A和B信号(相位差),同时通过SW引脚(中断线)输出按钮接触。使用两个德州仪器的SN74LVC1T45单位双供电总线收发器进行逻辑电平转换。此Click板™可以通过VCC SEL跳线选择3.3V或5V逻辑电压电平。这样,既能使用3.3V又能使用5V的MCU都可以正确使用通信线路。此外,此Click板™配备了一个包含易于使用的函数和示例代码的库,可用作进一步开发的参考。

Rotary W 2 Click hardware overview image

功能概述

开发板

Arduino UNO 是围绕 ATmega328P 芯片构建的多功能微控制器板。它为各种项目提供了广泛的连接选项,具有 14 个数字输入/输出引脚,其中六个支持 PWM 输出,以及六个模拟输入。其核心组件包括一个 16MHz 的陶瓷谐振器、一个 USB 连接器、一个电

源插孔、一个 ICSP 头和一个复位按钮,提供了为板 子供电和编程所需的一切。UNO 可以通过 USB 连接到计算机,也可以通过 AC-to-DC 适配器或电池供电。作为第一个 USB Arduino 板,它成为 Arduino 平台的基准,"Uno" 符号化其作为系列首款产品的地

位。这个名称选择,意为意大利语中的 "一",是为了 纪念 Arduino Software(IDE)1.0 的推出。最初与 Arduino Software(IDE)版本1.0 同时推出,Uno 自此成为后续 Arduino 发布的基础模型,体现了该平台的演进。

Arduino UNO Rev3 double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

AVR

MCU 内存 (KB)

32

硅供应商

Microchip

引脚数

32

RAM (字节)

2048

你完善了我!

配件

Click Shield for Arduino UNO 具有两个专有的 mikroBUS™ 插座,使所有 Click board™ 设备能够轻松与 Arduino UNO 板进行接口连接。Arduino UNO 是一款基于 ATmega328P 的微控制器开发板,为用户提供了一种经济实惠且灵活的方式来测试新概念并构建基于 ATmega328P 微控制器的原型系统,结合了性能、功耗和功能的多种配置选择。Arduino UNO 具有 14 个数字输入/输出引脚(其中 6 个可用作 PWM 输出)、6 个模拟输入、16 MHz 陶瓷谐振器(CSTCE16M0V53-R0)、USB 接口、电源插座、ICSP 头和复位按钮。大多数 ATmega328P 微控制器的引脚都连接到开发板左右两侧的 IO 引脚,然后再连接到两个 mikroBUS™ 插座。这款 Click Shield 还配备了多个开关,可执行各种功能,例如选择 mikroBUS™ 插座上模拟信号的逻辑电平,以及选择 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换电压转换器使用任何 Click board™,无论 Click board™ 运行在 3.3V 还是 5V 逻辑电压电平。一旦将 Arduino UNO 板与 Click Shield for Arduino UNO 连接,用户即可访问数百种 Click board™,并兼容 3.3V 或 5V 逻辑电压电平的设备。

Click Shield for Arduino UNO accessories 1 image

使用的MCU引脚

mikroBUS™映射器

Encoder Output B
PC0
AN
ID SEL
PD2
RST
SPI Select / ID COMM
PB2
CS
SPI Clock
PB5
SCK
SPI Data OUT
PB4
MISO
SPI Data IN
PB3
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Encoder Output A
PD6
PWM
Switch Output
PC3
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

Rotary W 2 Click Schematic schematic

一步一步来

项目组装

Click Shield for Arduino UNO front image hardware assembly

从选择您的开发板和Click板™开始。以Arduino UNO Rev3作为您的开发板开始。

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Board mapper by product8 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
Arduino UNO MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

软件支持

库描述

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

关键功能:

  • rotaryw2_set_led_pos - 设置Rotary W 2 LED位置的函数。

  • rotaryw2_set_led_data - 设置Rotary W 2 LED数据的函数。

  • rotaryw2_get_state_switch - 获取Rotary W 2开关状态的函数。

开源

代码示例

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

/*!
 * @file main.c
 * @brief Rotary W 2 Click example
 *
 * # Description
 * This library contains the API for the Rotary W 2 Click driver 
 * to control LEDs states and a rotary encoder position readings.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initialization of SPI module and log UART.
 * After the driver init, the app executes a default configuration and turn off all LEDs.
 *
 * ## Application Task
 * This example demonstrates the use of the Rotary W 2 Click board™.
 * The demo example shows the functionality of a rotary encoder used to control LEDs.
 *
 * @author Nenad Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "rotaryw2.h"

#define ROTARYW2_ONE_LED          ROTARYW2_SET_LED_DATA_1
#define ROTARYW2_TWO_LED          ROTARYW2_SET_LED_DATA_1  | ROTARYW2_SET_LED_DATA_9
#define ROTARYW2_FOUR_LED         ROTARYW2_SET_LED_DATA_1  | ROTARYW2_SET_LED_DATA_5  | \
                                  ROTARYW2_SET_LED_DATA_9  | ROTARYW2_SET_LED_DATA_13 
#define ROTARYW2_EIGHT_LED        ROTARYW2_SET_LED_DATA_1  | ROTARYW2_SET_LED_DATA_3  | \
                                  ROTARYW2_SET_LED_DATA_5  | ROTARYW2_SET_LED_DATA_7  | \
                                  ROTARYW2_SET_LED_DATA_9  | ROTARYW2_SET_LED_DATA_11 | \
                                  ROTARYW2_SET_LED_DATA_13 | ROTARYW2_SET_LED_DATA_15
#define ROTARYW2_EIGHT_LED_INV    ROTARYW2_SET_LED_DATA_2  | ROTARYW2_SET_LED_DATA_4  | \
                                  ROTARYW2_SET_LED_DATA_6  | ROTARYW2_SET_LED_DATA_8  | \
                                  ROTARYW2_SET_LED_DATA_10 | ROTARYW2_SET_LED_DATA_12 | \
                                  ROTARYW2_SET_LED_DATA_14 | ROTARYW2_SET_LED_DATA_16

static rotaryw2_t rotaryw2;
static log_t logger;

static uint8_t start_rot_status = 0;
static uint8_t led_demo_state = 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_data = 1;

/**
 * @brief Rotary W 2 select LED demo data function.
 * @details This function selects one of the four LED demo data 
 * based on the current state of the LED demo.
 * @return LED demo data:
 *         @li @c 0x0001 (ROTARYW2_ONE_LED)   - Turn ON LED[1],
 *         @li @c 0x0101 (ROTARYW2_TWO_LED)   - Turn ON LED[1,9],
 *         @li @c 0x0101 (ROTARYW2_FOUR_LED)  - Turn ON LED[1,5,9,13],
 *         @li @c 0x5555 (ROTARYW2_EIGHT_LED) - Turn ON LED[1,3,5,7,9,11,13,15].
 */
static uint16_t rotaryw2_sel_led_demo_data ( uint8_t led_demo_state );

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

/**
 * @brief Rotary W 2 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 rotaryw2_encoder_mechanism ( void );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    rotaryw2_cfg_t rotaryw2_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.
    rotaryw2_cfg_setup( &rotaryw2_cfg );
    ROTARYW2_MAP_MIKROBUS( rotaryw2_cfg, MIKROBUS_1 );
    if ( SPI_MASTER_ERROR == rotaryw2_init( &rotaryw2, &rotaryw2_cfg ) )
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( ROTARYW2_ERROR == rotaryw2_default_cfg ( &rotaryw2 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );
}

void application_task ( void )
{
    if ( ROTARYW2_OK == rotaryw2_set_led_data( &rotaryw2, led_data ) )
    {
        rotaryw2_switch_detection( );
        rotaryw2_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 uint16_t rotaryw2_sel_led_demo_data ( uint8_t led_demo_state ) 
{
    switch ( led_demo_state ) 
    {
        case 0: 
        {
            return ROTARYW2_ONE_LED;
            break;
        }
        case 1: 
        {
            return ROTARYW2_TWO_LED;
            break;
        }
        case 2: 
        {
            return ROTARYW2_FOUR_LED;
            break;
        }
        case 3: 
        {
            return ROTARYW2_EIGHT_LED;
            break;
        }
        default: 
        {
            return ROTARYW2_ONE_LED;
            break;
        }
    }
}

static void rotaryw2_switch_detection ( void )
{
    if ( rotaryw2_get_state_switch( &rotaryw2 ) ) 
    {
        new_state = 1;
        if ( (  1 == new_state ) && ( 0 == old_state ) ) 
        {
            old_state = 1;
            led_demo_state = ( led_demo_state + 1 ) % 5;
            if ( 4 == led_demo_state ) 
            {
                for ( uint8_t n_cnt = 0; n_cnt < 10; n_cnt++ )
                {
                    rotaryw2_set_led_data( &rotaryw2, ROTARYW2_EIGHT_LED_INV );
                    Delay_ms ( 100 );
                    rotaryw2_set_led_data( &rotaryw2, ROTARYW2_EIGHT_LED );
                    Delay_ms ( 100 );
                }
                
                for ( uint8_t led_p = ROTARYW2_SET_LED_POS_1; led_p <= ROTARYW2_SET_LED_POS_16; led_p++ ) 
                {
                    rotaryw2_set_led_pos( &rotaryw2, led_p );
                    Delay_ms ( 100 );
                }
                
                led_demo_state = 0;
                led_data = rotaryw2_sel_led_demo_data( led_demo_state );
            }
            else 
            {
                led_data = rotaryw2_sel_led_demo_data( led_demo_state );
            }
        }
    }
    else 
    {
        old_state = 0;
    }
}

static void rotaryw2_encoder_mechanism ( void )
{
    if ( rotaryw2_get_state_ena( &rotaryw2 ) == rotaryw2_get_state_enb( &rotaryw2 ) ) 
    {
        old_rot_state = 0;
        start_rot_status = rotaryw2_get_state_ena( &rotaryw2 ) && rotaryw2_get_state_enb( &rotaryw2 );
    }
    else 
    {
        new_rot_state = 1;
        if ( new_rot_state != old_rot_state ) 
        {
            old_rot_state = 1;
            if ( start_rot_status != rotaryw2_get_state_ena( &rotaryw2 ) ) 
            {
                led_data = ( led_data << 1 ) | ( led_data >> 15 );
            }
            else 
            {
                led_data = ( led_data >> 1 ) | ( led_data << 15 );
            }
        }
    }
}

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

额外支持

资源

喜欢这个项目吗?

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