【小华HC32F448测评】+这3个LED灯的控制不简单
[复制链接]
起初在进行LED的测试时发现开发板上还有其它的LED资源可供使用,见图1所示。
后来在使用时才发现,它并不是单纯用GPIO口就可控制的,它是挂靠在芯片TCA9539之下,见图2所示。
图1 LED电路
图2 控制电路
为此,要控制这3个LED灯并非是以GPIO口,而是通过以I2C方式扩展出的IO口。
TCA9539的引脚连接关系为:
SCL---PE1
SDA---PE0
INT---PB5
RST---PB15
TCA9539的初始化函数为:
static void BSP_TCA9539_I2C_Init(void)
{
stc_gpio_init_tstcGpioInit;
/* Configuration I2C GPIO */
(void)GPIO_StructInit(&stcGpioInit);
(void)GPIO_Init(BSP_TCA9539_I2C_SCL_PORT, BSP_TCA9539_I2C_SCL_PIN, &stcGpioInit);
(void)GPIO_Init(BSP_TCA9539_I2C_SDA_PORT, BSP_TCA9539_I2C_SDA_PIN, &stcGpioInit);
GPIO_SetFunc(BSP_TCA9539_I2C_SCL_PORT, BSP_TCA9539_I2C_SCL_PIN, BSP_TCA9539_I2C_SCL_FUNC);
GPIO_SetFunc(BSP_TCA9539_I2C_SDA_PORT, BSP_TCA9539_I2C_SDA_PIN, BSP_TCA9539_I2C_SDA_FUNC);
/* Enable I2C Peripheral*/
FCG_Fcg1PeriphClockCmd(BSP_TCA9539_I2C_FCG, ENABLE);
(void)BSP_I2C_Init(BSP_TCA9539_I2C_UNIT);
}
TCA9539是通过读写函数来收发数据,其函数分别为:
static void BSP_TCA9539_I2C_Write(const uint8_t *pu8Reg, const uint8_t *pu8Buf, uint32_t u32Len)
{
(void)BSP_I2C_Write(BSP_TCA9539_I2C_UNIT, BSP_TCA9539_DEV_ADDR, pu8Reg, BSP_TCA9539_REG_ADDR_LEN, pu8Buf, u32Len);
}
static void BSP_TCA9539_I2C_Read(const uint8_t *pu8Reg, uint8_t *pu8Buf, uint32_t u32Len)
{
(void)BSP_I2C_Read(BSP_TCA9539_I2C_UNIT, BSP_TCA9539_DEV_ADDR, pu8Reg, BSP_TCA9539_REG_ADDR_LEN, pu8Buf, u32Len);
}
由图2可知,TCA9539将扩展接口分为2部分,即P00~P07和P10~P17,共16个。
其中,LED0~LED2分别与P15~P17相连接,对其所作定义为:
#define EIO_LED_RED (TCA9539_IO_PIN5) /* Red LED, output */
#define EIO_LED_YELLOW (TCA9539_IO_PIN6) /* Yellow LED, output */
#define EIO_LED_BLUE (TCA9539_IO_PIN7) /* Blue LED, output */
TCA9539对扩展口的初始化函数为:
void BSP_IO_Init(void)
{
/* Configuration the low layer of TCA9539 */
stcTca9539Config.Init = BSP_TCA9539_I2C_Init;
stcTca9539Config.Write = BSP_TCA9539_I2C_Write;
stcTca9539Config.Read = BSP_TCA9539_I2C_Read;
stcTca9539Config.Reset = BSP_TCA9539_Reset;
stcTca9539Config.IntInit = NULL;
/* Configuration the TCA9539 */
(void)TCA9539_Init(&stcTca9539Config);
}
为了便于使用这些扩展口,特配置了相应的读写函数,其内容为:
void BSP_IO_WritePortPin(uint8_t u8Port, uint8_t u8Pin, uint8_t u8PinState)
{
(void)TCA9539_WritePin(&stcTca9539Config, u8Port, u8Pin, u8PinState);
}
uint8_t BSP_IO_ReadPortPin(uint8_t u8Port, uint8_t u8Pin)
{
uint8_t u8Value;
(void)TCA9539_ReadPin(&stcTca9539Config, u8Port, u8Pin, &u8Value);
return u8Value;
}
对LED所作的引脚定义为:
#define LED_PORT (EIO_PORT1)
#define LED_RED_PORT (EIO_PORT1)
#define LED_RED_PIN (EIO_LED_RED)
#define LED_YELLOW_PORT (EIO_PORT1)
#define LED_YELLOW_PIN (EIO_LED_YELLOW)
#define LED_BLUE_PORT (EIO_PORT1)
#define LED_BLUE_PIN (EIO_LED_BLUE)
故对LED的配置函数为:
void BSP_LED_Init(void)
{
BSP_IO_WritePortPin(LED_PORT, (LED_RED_PIN | LED_YELLOW_PIN | LED_BLUE_PIN), LED_OFF);
BSP_IO_ConfigPortPin(LED_PORT, (LED_RED_PIN | LED_YELLOW_PIN | LED_BLUE_PIN), EIO_DIR_OUT);
}
从而派生出的用于LED灯开关及闪烁的功能函数为:
void BSP_IO_TogglePortPin(uint8_t u8Port, uint8_t u8Pin)
{
(void)TCA9539_TogglePin(&stcTca9539Config, u8Port, u8Pin);
}
void BSP_LED_Toggle(uint8_t u8Led)
{
BSP_IO_TogglePortPin(LED_PORT, u8Led);
}
void BSP_LED_On(uint8_t u8Led)
{
BSP_IO_WritePortPin(LED_PORT, u8Led, LED_ON);
}
void BSP_LED_Off(uint8_t u8Led)
{
BSP_IO_WritePortPin(LED_PORT, u8Led, LED_OFF);
}
通过调用这些函数,就可轻松地控制这3个LED灯。
实现这3个LED灯闪烁的主程序十分简单,其内容为:
int32_t main(void)
{
LL_PERIPH_WE(EXAMPLE_PERIPH_WE);
BSP_CLK_Init();
BSP_IO_Init();
BSP_LED_Init();
LED_Init();
LL_PERIPH_WP(EXAMPLE_PERIPH_WP);
while(1)
{
BSP_LED_Toggle(LED_RED);
BSP_LED_Toggle(LED_YELLOW);
BSP_LED_Toggle(LED_BLUE);
DDL_DelayMS(DLY_MS);
}
}
图3 显示效果
掌握了以上的内容,再使用这些扩展口就容易了许多。
|