Esp32 TCA9555 基于Arduino基于 microByte

承上,要读懂这一节的代码,要先懂得显示屏的显示要点,i2c 及wire.h 关于i2c 和 wire.h 在Arduino 上封装的实在是太好了,或者说太死了。

先上代码,(抄自例程colour_test TCA9555_digitalRead )
在这里插入图片描述
`
#include “TCA9555.h”
#include <SPI.h>
#include <Wire.h>

#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

TCA9555 TCA(0x20);

void setup(void) {

tft.init();

tft.fillScreen(TFT_BLACK);

Wire.begin();
TCA.begin();

}
}

void loop() {
tft.fillScreen(TFT_BLACK);
for (int pin = 0; pin < 16; pin++)
{
int val = TCA.digit

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于C语言的tca9555驱动代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <string.h> #include <unistd.h> #include <linux/i2c-dev.h> #define TCA9555_I2C_ADDR 0x20 #define INPUT_PORT_REG_ADDR 0x00 #define OUTPUT_PORT_REG_ADDR 0x01 #define POLARITY_INVERSION_REG_ADDR 0x02 #define CONFIG_REG_ADDR 0x03 #define NUM_PINS 16 int i2c_fd; void tca9555_write_reg(char reg, char value) { char buf[2]; buf[0] = reg; buf[1] = value; if (write(i2c_fd, buf, 2) != 2) { perror("write"); exit(1); } } char tca9555_read_reg(char reg) { char value; if (write(i2c_fd, &reg, 1) != 1) { perror("write"); exit(1); } if (read(i2c_fd, &value, 1) != 1) { perror("read"); exit(1); } return value; } void tca9555_set_direction(int pin, int is_input) { char config_reg = tca9555_read_reg(CONFIG_REG_ADDR); if (is_input) { config_reg |= (1 << pin); } else { config_reg &= ~(1 << pin); } tca9555_write_reg(CONFIG_REG_ADDR, config_reg); } void tca9555_set_output(int pin, int value) { char output_port_reg = tca9555_read_reg(OUTPUT_PORT_REG_ADDR); if (value) { output_port_reg |= (1 << pin); } else { output_port_reg &= ~(1 << pin); } tca9555_write_reg(OUTPUT_PORT_REG_ADDR, output_port_reg); } int tca9555_get_input(int pin) { char input_port_reg = tca9555_read_reg(INPUT_PORT_REG_ADDR); return (input_port_reg >> pin) & 1; } int main() { int i; char buf[2]; char input_port_reg; i2c_fd = open("/dev/i2c-1", O_RDWR); if (i2c_fd < 0) { perror("open"); exit(1); } if (ioctl(i2c_fd, I2C_SLAVE, TCA9555_I2C_ADDR) < 0) { perror("ioctl"); exit(1); } // Set all pins to output tca9555_write_reg(CONFIG_REG_ADDR, 0x00); // Turn on every other LED for (i = 0; i < NUM_PINS; i += 2) { tca9555_set_output(i, 1); } // Read all input pins and print their values input_port_reg = tca9555_read_reg(INPUT_PORT_REG_ADDR); printf("Input values: "); for (i = 0; i < NUM_PINS; i++) { printf("%d ", (input_port_reg >> i) & 1); } printf("\n"); close(i2c_fd); return 0; } ``` 该代码连接到i2c总线,使用 ioctl() 函数将i2c设备文件的文件描述符设置为TCA9555芯片的地址。然后,使用tca9555_write_reg()和tca9555_read_reg()函数向TCA9555芯片写入和读取寄存器值,并使用tca9555_set_direction()、tca9555_set_output()和tca9555_get_input()函数来控制芯片的输出和读取输入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值