From c8ccb70ef36b3a4ef294aaf5e7b8bc4bec52e1dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Tue, 23 Apr 2024 00:20:56 +0200 Subject: [+] MCP23017 as I2C screen controller and test --- main/CMakeLists.txt | 3 +- main/Kconfig.projbuild | 148 +++++++++++++++++++++++++++++++ main/main.c | 235 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 302 insertions(+), 84 deletions(-) create mode 100644 main/Kconfig.projbuild (limited to 'main') diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cf2c455..46af1ea 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "main.c" - INCLUDE_DIRS ".") + INCLUDE_DIRS "." + REQUIRES esp_driver_gpio esp_driver_i2c) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..18b0727 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,148 @@ +menu "Configuration of the Nixie Screen" + + orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps" + + config MCP_I2C_ADDR + hex "I2C address of mcp23017" + default 0x20 + help + I2C address of `mcp23017`. `mcp23017` has three address pins (`A0`, + `A1`, and `A2`). The address starts from `0x20` (all pins are + grounded), which is the default, and ends at `0x27`. See "3.3.1 + ADDRESSING I2C DEVICES (MCP23017)" in the datasheet. + + config SDA_PIN + int "SDA pin number" + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX + default 6 + help + Pin number for SDA pin. + + config SCL_PIN + int "SCL pin number" + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX + default 7 + help + Pin number for SLC pin. + + config MIN + int + default 0 + + config MAX + int + default 7 + + config N0_P0 + int "N0_P0 pin number" + range MIN MAX + default 4 + help + Pin number for N0_P0. + + config N0_P1 + int "N0_P1 pin number" + range MIN MAX + default 5 + help + Pin number for N0_P1. + + config N0_P2 + int "N0_P2 pin number" + range MIN MAX + default 6 + help + Pin number for N0_P2. + + config N0_P3 + int "N0_P3 pin number" + range MIN MAX + default 7 + help + Pin number for N0_P3. + + config N1_P0 + int "N1_P0 pin number" + range MIN MAX + default 0 + help + Pin number for N1_P0. + + config N1_P1 + int "N1_P1 pin number" + range MIN MAX + default 1 + help + Pin number for N1_P1. + + config N1_P2 + int "N1_P2 pin number" + range MIN MAX + default 2 + help + Pin number for N1_P2. + + config N1_P3 + int "N1_P3 pin number" + range MIN MAX + default 3 + help + Pin number for N1_P3. + + config N2_P0 + int "N2_P0 pin number" + range MIN MAX + default 4 + help + Pin number for N2_P0. + + config N2_P1 + int "N2_P1 pin number" + range MIN MAX + default 5 + help + Pin number for N2_P1. + + config N2_P2 + int "N2_P2 pin number" + range MIN MAX + default 6 + help + Pin number for N2_P2. + + config N2_P3 + int "N2_P3 pin number" + range MIN MAX + default 7 + help + Pin number for N2_P3. + + config N3_P0 + int "N3_P0 pin number" + range MIN MAX + default 0 + help + Pin number for N3_P0. + + config N3_P1 + int "N3_P1 pin number" + range MIN MAX + default 1 + help + Pin number for N3_P1. + + config N3_P2 + int "N3_P2 pin number" + range MIN MAX + default 2 + help + Pin number for N3_P2. + + config N3_P3 + int "N3_P3 pin number" + range MIN MAX + default 3 + help + Pin number for N3_P3. + +endmenu \ No newline at end of file diff --git a/main/main.c b/main/main.c index c44352c..a7f2019 100644 --- a/main/main.c +++ b/main/main.c @@ -1,18 +1,11 @@ #include #include #include "driver/gpio.h" +#include "driver/i2c_master.h" -struct Nixie -{ - uint8_t digit; - uint8_t D; - uint8_t C; - uint8_t B; - uint8_t A; -}; - -struct Nixie screen[4]; - +#define MCP_I2C_ADDR CONFIG_MCP_I2C_ADDR +#define SDA_PIN CONFIG_SDA_PIN +#define SCL_PIN CONFIG_SCL_PIN #define N0_P0 CONFIG_N0_P0 #define N0_P1 CONFIG_N0_P1 #define N0_P2 CONFIG_N0_P2 @@ -29,52 +22,116 @@ struct Nixie screen[4]; #define N3_P1 CONFIG_N3_P1 #define N3_P2 CONFIG_N3_P2 #define N3_P3 CONFIG_N3_P3 -#define GPIO_OUTPUT_PIN_SEL ((1ULL< 1) reg = REG_IODIRB; + mcp_set_mode(reg, screen[i].D, MCP_GPIO_OUTPUT); + mcp_set_mode(reg, screen[i].C, MCP_GPIO_OUTPUT); + mcp_set_mode(reg, screen[i].B, MCP_GPIO_OUTPUT); + mcp_set_mode(reg, screen[i].A, MCP_GPIO_OUTPUT); + } + + + printf("MCP GPIO Configured!\n"); return; } @@ -82,81 +139,93 @@ void set_digit(struct Nixie* n) { switch (n->digit) { case 0: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 0); - gpio_set_level(n->B, 0); - gpio_set_level(n->A, 0); + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 0); + mcp_set_level(n->reg, n->B, 0); + mcp_set_level(n->reg, n->A, 0); break; case 1: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 0); - gpio_set_level(n->B, 0); - gpio_set_level(n->A, 1); + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 0); + mcp_set_level(n->reg, n->B, 0); + mcp_set_level(n->reg, n->A, 1); break; case 2: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 0); - gpio_set_level(n->B, 1); - gpio_set_level(n->A, 0); + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 0); + mcp_set_level(n->reg, n->B, 1); + mcp_set_level(n->reg, n->A, 0); break; case 3: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 0); - gpio_set_level(n->B, 1); - gpio_set_level(n->A, 1); + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 0); + mcp_set_level(n->reg, n->B, 1); + mcp_set_level(n->reg, n->A, 1); break; case 4: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 1); - gpio_set_level(n->B, 0); - gpio_set_level(n->A, 0); - break; - case 5: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 1); - gpio_set_level(n->B, 0); - gpio_set_level(n->A, 1); - break; - case 6: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 1); - gpio_set_level(n->B, 1); - gpio_set_level(n->A, 0); + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 1); + mcp_set_level(n->reg, n->B, 0); + mcp_set_level(n->reg, n->A, 0); + break; + case 5: + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 1); + mcp_set_level(n->reg, n->B, 0); + mcp_set_level(n->reg, n->A, 1); + break; + case 6: + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 1); + mcp_set_level(n->reg, n->B, 1); + mcp_set_level(n->reg, n->A, 0); break; case 7: - gpio_set_level(n->D, 0); - gpio_set_level(n->C, 1); - gpio_set_level(n->B, 1); - gpio_set_level(n->A, 1); + mcp_set_level(n->reg, n->D, 0); + mcp_set_level(n->reg, n->C, 1); + mcp_set_level(n->reg, n->B, 1); + mcp_set_level(n->reg, n->A, 1); break; case 8: - gpio_set_level(n->D, 1); - gpio_set_level(n->C, 0); - gpio_set_level(n->B, 0); - gpio_set_level(n->A, 0); + mcp_set_level(n->reg, n->D, 1); + mcp_set_level(n->reg, n->C, 0); + mcp_set_level(n->reg, n->B, 0); + mcp_set_level(n->reg, n->A, 0); break; case 9: - gpio_set_level(n->D, 1); - gpio_set_level(n->C, 0); - gpio_set_level(n->B, 0); - gpio_set_level(n->A, 1); + mcp_set_level(n->reg, n->D, 1); + mcp_set_level(n->reg, n->C, 0); + mcp_set_level(n->reg, n->B, 0); + mcp_set_level(n->reg, n->A, 1); break; default: + mcp_set_level(n->reg, n->D, 1); + mcp_set_level(n->reg, n->C, 1); + mcp_set_level(n->reg, n->B, 1); + mcp_set_level(n->reg, n->A, 1); break; return; } } -void app_main(void) +void test_screen(void) { - printf("N1_P3: %i\n", N1_P3); + printf("Starting up...\n"); configure_screen(); + printf("Screen Configured!\n"); + while(1){ for (uint8_t i = 0; i <= 9; ++i) { - printf("digit: %i\n", i); - screen[0].digit = i; // Update the digit - set_digit(&screen[0]); // Call set_digit function - sleep(1); // Sleep for 2 seconds + for (int j = 0; j < 4; ++j) { + screen[j].digit = (i + j) % 10; // Update the digit cyclically + set_digit(&screen[j]); // Call set_digit function + } + sleep(1); } } } + +void app_main(void) +{ + test_screen(); +} \ No newline at end of file -- cgit v1.2.3