driver/tca9555.h

79 lines
3.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __TCA9555_H__
#define __TCA9555_H__
#include "main.h"
#include "lib.h"
#define TCA9555_ADDRESS 0x20 /*!< I2C Address */
/************************** I2C Registers *************************************/
typedef enum
{
TCA9555_INPUT_REG0 = 0x00, /*!< 输入状态寄存器 */
TCA9555_INPUT_REG1 = 0x01, /*!< 输入状态寄存器 */
TCA9555_OUTPUT_REG0 = 0x02, /*!< 输出寄存器以更改输出状态的位。设置为1时设置为高电平 */
TCA9555_OUTPUT_REG1 = 0x03, /*!< 输出寄存器以更改输出状态的位。设置为1时设置为高电平 */
TCA9555_POLARITY_REG0 = 0x04, /*!< 极性反转寄存器。位'1'在寄存器0x00上反转输入极性 */
TCA9555_POLARITY_REG1 = 0x05, /*!< 极性反转寄存器。位'1'在寄存器0x00上反转输入极性 */
TCA9555_CONFIG_REG0 = 0x06, /*!< 配置寄存器。位'1'设置为输入,位'0'设置为输出 */
TCA9555_CONFIG_REG1 = 0x07 /*!< 配置寄存器。位'1'设置为输入,位'0'设置为输出 */
} tca9555_reg_t;
#define CONFIG_INPUT_VAL 0xFF
#define CONFIG_OUTPUT_VAL 0x00
#define WRITE_ADDRESS ((TCA9555_ADDRESS << 1) | 0) /*!< I2C主设备写入 */
#define READ_ADDRESS ((TCA9555_ADDRESS << 1) | 1) /*!< I2C主设备读取 */
#define ACK_CHECK_EN 0x1 /*!< I2C主设备检查从设备ACK */
#define ACK_CHECK_DIS 0x0 /*!< I2C主设备不检查从设备ACK */
#define ACK_VAL 0x0 /*!< I2C从设备ACK值 */
#define NACK_VAL 0x1 /*!< I2C从设备NACK值 */
struct tca9555_sbit
{
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
};
union tca9555_uinputport
{
uint8_t asint; /*!< port data as unsigned integer */
struct tca9555_sbit bit; /*!< port data as separate bits */
};
struct tca9555_sregister
{
union tca9555_uinputport p0;
union tca9555_uinputport p1;
};
typedef union
{
uint16_t asint; /*!< register data as unsigned integer */
struct tca9555_sregister port; /*!< register data as separate ports */
} tca9555_register;
void tca9555_init(void); // 初始化i2c总线
void tca9555_configure_input(tca9555_register *reg, uint8_t port, uint8_t pin); // 配置输入
uint8_t tca9555_read_single_register(tca9555_reg_t address); // 读取单个寄存器
void tca9555_write_single_register(tca9555_reg_t address, uint8_t reg_val); // 写入单个寄存器
void tca9555_read_struct(tca9555_register *reg, tca9555_reg_t reg_num); // 读取结构体
void tca9555_write_struct(tca9555_register *reg, tca9555_reg_t reg_num); // 写入结构体
void tca9555_write_output(tca9555_register *reg);
void tca9555_write_polarity(tca9555_register *reg);
void tca9555_write_config(tca9555_register *reg);
void tca9555_read_input(tca9555_register *reg);
void tca9555_read_output(tca9555_register *reg);
void tca9555_read_polarity(tca9555_register *reg);
void tca9555_read_config(tca9555_register *reg);
#endif // __TCA9555_H__