打印
[研电赛技术支持]

单片机基于AW9523的多路LED驱动控制

[复制链接]
132|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tpgf|  楼主 | 2023-7-10 14:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在一些智能玩具上经常需要多路LED的控制,并且能够提供渐变渐亮呼吸效果。

aw9523是一款16路多功能LED驱动器和GPIO控制器,简单实用。

16个I/O端口中的任何一个都可以配置为LED驱动器模式或GPIO模式。

此外,任何GPIO都可以单独配置为输入或输出。

通电后,所有16个I/O端口配置为默认GPIO输出,

默认状态根据I2C器件地址选择脚AD0和AD1设置。

所有配置为输入的I/O端口都会持续监控状态变化。

状态更改由INTN输出指示。

16个多功能IO,支持GPIO模式或LED驱动(电流源调光),

LED驱动模式下256阶线性调光,

任意GPIO可配置为独立的输入或输出,

中断功能,8μs防抖,低电平有效,

标准I2C接口,AD1/AD0选择I2C器件地址,

SDA, SCL, RSTN以及所有GPIO均支持1.8V逻辑 电平,

具有关断模式,低电平有效,

2.5V~5.5V供电,

QFN4×4-24L。

采用gd32f4xx硬件i2c方式高效控制aw9523 ,aw9523.c如下所示

#ifndef __AW9523_H__
#define __AW9523_H__

#include "gd32f4xx.h"
#include "cmsis_os.h"
#include "comtypes.h"
#include "delay.h"
#include "i2c.h"


#define READ_TIMOUT                     (300)

#define I2C_ADDRESS                     (0xB6)

#define AW_I2C_RETRIES                  (5)
#define AW_I2C_RETRY_DELAY              (5)
#define AW_READ_CHIPID_RETRIES          (5)
#define AW_READ_CHIPID_RETRY_DELAY      (5)



#define INPUT_PORT0                     (0x00)//R   Equal to P0 Input_Port0 P0 port input state
#define INPUT_PORT1                     (0x01)//R   Equal to P1 Input_Port1 P1 port input state
#define OUTPUT_PORT0                    (0x02)//W/R Refer to table 1 Output_Port0 P0 port output state
#define OUTPUT_PORT1                    (0x03)//W/R Refer to table 1 Output_Port1 P1 port output state
#define CONFIG_PORT0                    (0x04)// W/R 00H Config_Port0 P0 port direction configure
#define CONFIG_PORT1                    (0x05)//W/R 00H Config_Port1 P1 port direction configure
#define INT_PORT0                       (0x06)//W/R 00H Int_Port0 P0 port interrupt enable
#define INT_PORT1                       (0x07)// W/R 00H Int_Port1 P1 port interrupt enable
#define ID                              (0x10)// R 23H ID ID register (read only)
#define GCR                             (0x11)//W/R 00H CTL Global control register
#define LED_MODE_SWITCH_P0              (0x12)//W/R FFH LED Mode Switch P0 port mode configure
#define LED_MODE_SWITCH_P1              (0x13)//W/R FFH LED Mode Switch P1 port mode configure
#define DIM0                            (0x20)// W 00H DIM0 P1_0 LED current control
#define DIM1                            (0x21)// W 00H DIM1 P1_1 LED current control
#define DIM2                            (0x22)// W 00H DIM2 P1_2 LED current control
#define DIM3                            (0x23)// W 00H DIM3 P1_3 LED current control
#define DIM4                            (0x24)// W 00H DIM4 P0_0 LED current control
#define DIM5                            (0x25)// W 00H DIM5 P0_1 LED current control
#define DIM6                            (0x26)// W 00H DIM6 P0_2 LED current control
#define DIM7                            (0x27)// W 00H DIM7 P0_3 LED current control
#define DIM8                            (0x28)// W 00H DIM8 P0_4 LED current control
#define DIM9                            (0x29)// W 00H DIM9 P0_5 LED current control
#define DIM10                           (0x2A)// W 00H DIM10 P0_6 LED current control
#define DIM11                           (0x2B)// W 00H DIM11 P0_7 LED current control
#define DIM12                           (0x2C)// W 00H DIM12 P1_4 LED current control
#define DIM13                           (0x2D)// W 00H DIM13 P1_5 LED current control
#define DIM14                           (0x2E)// W 00H DIM14 P1_6 LED current control
#define DIM15                           (0x2F)// W 00H DIM15 P1_7 LED current control
#define SOFT_RESET                      (0x7F)// W 00H SW_RSTN Soft reset

#define AW9523B_ID                      (0x23)



#define LED_HWRESET_CLK_ENABLE()        __HAL_RCC_GPIOA_CLK_ENABLE()

#define LED_HWRESET_PIN                 GPIO_PIN_5

#define LED_HWRESET_PORT                GPIOA

extern osMutexId mutex_i2c_id;



int AW9523_Init(void);

int AW9523_SetBrightness(uint8_t channel,uint8_t brightness);

int AW9523_SetAllBrightness(uint8_t brightness);

int AW9523_SoftReset(void);

void AW9523_HWReset(void);

int AW9523_Restart(void);


#endif




#include "aw9523.h"


/**
LED1--16 一一对应DIM0--DIM15
*/
static const uint8_t LED2DIM[] = {DIM0,DIM1,DIM2,DIM3,DIM4,DIM5,DIM6,DIM7,DIM8,DIM9,DIM10,DIM11,DIM12,DIM13,DIM14,DIM15};



static __INLINE HAL_StatusTypeDef Local_Hal_I2C_Read(uint8_t regaddr,uint8_t* buf,uint8_t length)
{
        osMutexWait(mutex_i2c_id,osWaitForever);
        gd32_i2c_read(buf,I2C_ADDRESS,regaddr,length);
        osMutexRelease(mutex_i2c_id);
       
        return HAL_OK;
}




static __INLINE HAL_StatusTypeDef Local_Hal_I2C_Write(uint8_t regaddr,uint8_t* buf,uint8_t length)
{
      osMutexWait(mutex_i2c_id,osWaitForever);
      gd32_i2c_write(buf,I2C_ADDRESS,regaddr,length);   
      osMutexRelease(mutex_i2c_id);
       
      return HAL_OK;
}



/*
芯片初始化
*/
int AW9523_Init(void)
{
    uint8_t val = 0;
    uint8_t ret = 0;


    /***
     * AW9523B support 3 reset mode: power on reset, hardware reset,
     * software reset. Each reset mode can reset registers to default value
     */
    if(AW9523_SoftReset() < 0) return -1;


    //read chip id
    ret         = Local_Hal_I2C_Read(ID,&val,1);
       
    if(ret != HAL_OK)     return -1;
    if(val != AW9523B_ID) return -1;

    //Global control register config gpio mode & dim setp P0 special
    val         = 0x12;               
    ret         = Local_Hal_I2C_Write(GCR,&val,1);
    if(ret != HAL_OK) return -1;

    //led mode switch
    //P0 to led
    val         = 0x00;
    ret         = Local_Hal_I2C_Write(LED_MODE_SWITCH_P0,&val,1);
    if(ret != HAL_OK) return -1;

    //P1 to led
    val         = 0x00;
    ret         = Local_Hal_I2C_Write(LED_MODE_SWITCH_P1,&val,1);
    if(ret != HAL_OK) return -1;
       
    //关闭电流
    AW9523_SetAllBrightness(0);

       
    //16脚上电默认输出状态由AD1/AD0决定,故上电后全导通
    //P0 led breakover
    val         = 0x00;
    ret         = Local_Hal_I2C_Write(OUTPUT_PORT0,&val,1);
    if(ret != HAL_OK) return -1;

    //P1 led breakover
    val         = 0x00;
    ret         = Local_Hal_I2C_Write(OUTPUT_PORT1,&val,1);
    if(ret != HAL_OK) return -1;
       
               
    return 0;               
}




/**
单通道独立亮度调节
channel:   0----15
brightness:0----255
*/
int AW9523_SetBrightness(uint8_t channel,uint8_t brightness)
{       
    uint8_t val = 0;
    uint8_t ret = 0;

    val         = brightness;
    ret         = Local_Hal_I2C_Write(LED2DIM[channel],&val,1);
    if(ret != HAL_OK) return -1;

    return 0;
}




/**
多通道亮度调节,统一控制
brightness:0----255
*/
int AW9523_SetAllBrightness(uint8_t brightness)
{
    uint8_t i;
    uint8_t ret = 0;
       
    for(i = DIM0;i <= DIM15;i++)
    {
         ret  = Local_Hal_I2C_Write(i,&brightness,1);
             if(ret != HAL_OK) return -1;                               
    }
               
    return 0;
}



//软件复位
int  AW9523_SoftReset(void)
{
    uint8_t val;
    uint8_t ret = 0;

    val         = 0x00;
    ret         = Local_Hal_I2C_Write(SOFT_RESET,&val,1);
    if(ret != HAL_OK) return -1;

    return 0;       
}



//硬件复位
void AW9523_HWReset(void)
{
     GPIO_InitTypeDef  GPIO_InitStruct;
                       
     LED_HWRESET_CLK_ENABLE();
                                               
     GPIO_InitStruct.Pin       = LED_HWRESET_PIN;
     GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull      = GPIO_NOPULL;
     GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
                       
     HAL_GPIO_Init(LED_HWRESET_PORT, &GPIO_InitStruct);
          
     //RSTN
     HAL_GPIO_WritePin(LED_HWRESET_PORT,LED_HWRESET_PIN,GPIO_PIN_RESET);
     Delay_MS(1);
     HAL_GPIO_WritePin(LED_HWRESET_PORT,LED_HWRESET_PIN,GPIO_PIN_SET);
     Delay_MS(1);
}



//防止该芯片意外复位
int AW9523_Restart(void)
{
     uint8_t val = 0xFF;
       
     //理论上GCR寄存器复位了,为判断依据
     while(Local_Hal_I2C_Read(GCR,&val,1) != HAL_OK){}
                         
     if(val != 0x00) return -1;
       
     while(AW9523_Init());
                         
     return 0;
}









————————————————
版权声明:本文为CSDN博主「麦德泽特」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44470384/article/details/131421108

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1364

主题

13994

帖子

8

粉丝