|
| 1 | +/* |
| 2 | +© [2025] Microchip Technology Inc. and its subsidiaries. |
| 3 | +
|
| 4 | + Subject to your compliance with these terms, you may use Microchip |
| 5 | + software and any derivatives exclusively with Microchip products. |
| 6 | + You are responsible for complying with 3rd party license terms |
| 7 | + applicable to your use of 3rd party software (including open source |
| 8 | + software) that may accompany Microchip software. SOFTWARE IS ?AS IS.? |
| 9 | + NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS |
| 10 | + SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, |
| 11 | + MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT |
| 12 | + WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, |
| 13 | + INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY |
| 14 | + KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF |
| 15 | + MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE |
| 16 | + FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S |
| 17 | + TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT |
| 18 | + EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR |
| 19 | + THIS SOFTWARE. |
| 20 | +*/ |
| 21 | +/** |
| 22 | + * I2C_HOST EXAMPLE Generated Driver File |
| 23 | + * |
| 24 | + * @file i2c_host example.c |
| 25 | + * |
| 26 | + * @ingroup i2c_host example |
| 27 | + * |
| 28 | + * @version I2C_HOST EXAMPLE Example Version 1.0.0 |
| 29 | + * |
| 30 | + * @brief Generated file for |
| 31 | + * Example: 3. I2C IO Expander 2 - LEDs and Buttons |
| 32 | + * Implementation: Polled |
| 33 | + * Visualization: Printf |
| 34 | + * MCU Device family: AVR |
| 35 | +*/ |
| 36 | + |
| 37 | +#include "mcc_generated_files/system/system.h" |
| 38 | + |
| 39 | +// Note: MCP23008 - 8-Bit I2C I/O Expander with I2C (& SPI) Serial Interface(s) |
| 40 | +// Reference to the MCP23008 data sheet: https://www.microchip.com/DS20001919 |
| 41 | +// MCP23008 register addresses are in Table 1-2 (page 5) of the data sheet |
| 42 | +// The Curiosity Nano Explorer (EV58G97A) has 2 x MCP23008 IO Expanders |
| 43 | + |
| 44 | +#define MCP23008_1_I2C_ADDRESS 0x25 |
| 45 | +#define MCP23008_2_I2C_ADDRESS 0x24 |
| 46 | + |
| 47 | +#define MCP23008_IODIR 0x00 |
| 48 | +#define MCP23008_IPOL 0x01 |
| 49 | +#define MCP23008_GPINTEN 0x02 // INTERRUPT-ON-CHANGE CONTROL (GPINTEN) REGISTER, GPINTEN â?? enables the individual inputs |
| 50 | +#define MCP23008_DEFVAL 0x03 // DEFAULT COMPARE (DEFVAL) â?? holds the values that are compared against the associated input port values |
| 51 | +#define MCP23008_INTCON 0x04 // INTERRUPT CONTROL (INTCON) - Controls if the input values are compared against DEFVAL or the previous values on the portREGISTER: |
| 52 | +#define MCP23008_IOCON 0x05 // IOCON (ODR and INPOL) â?? configures the INT pin as push-pull, open-drain and active-level |
| 53 | +#define MCP23008_GPPU 0x06 |
| 54 | +#define MCP23008_INTF 0x07 // INTERRUPT FLAG (INTF) REGISTER: 1 = Pin caused interrupt |
| 55 | +#define MCP23008_INTCAP 0X08 |
| 56 | +#define MCP23008_GPIO 0x09 |
| 57 | +#define MCP23008_OLAT 0x0A |
| 58 | + |
| 59 | +extern void DELAY_milliseconds(uint16_t milliseconds); |
| 60 | +uint8_t MCP23008_Read(uint8_t i2c_address, uint8_t reg, uint8_t* data); |
| 61 | +uint8_t MCP23008_Write(uint8_t i2c_address, uint8_t reg, uint8_t data); |
| 62 | +void printButtonPressed(int pressedButton); |
| 63 | +void button_led_control(void); |
| 64 | + |
| 65 | +static volatile bool resetFlag = false; |
| 66 | +// All joystick pins re-mapped as they are not connected to Curiosity Nano GPIO by default |
| 67 | +static const char* buttonLabels[8] = {"J-UP", "J-LEFT", "J-DOWN", "J-RIGHT", "J-PUSH", "SW3", "SW2", "SW1"}; |
| 68 | + |
| 69 | +uint8_t MCP23008_Read(uint8_t i2c_address, uint8_t reg, uint8_t* data) |
| 70 | +{ |
| 71 | + i2c_host_error_t errorState = I2C_ERROR_NONE; |
| 72 | + size_t txLength = 1; |
| 73 | + size_t rxLength = 1; |
| 74 | + uint8_t txBuffer[1] = {0}; |
| 75 | + |
| 76 | + txBuffer[0] = reg; // I2C Client register to read from |
| 77 | + |
| 78 | + uint8_t waitCounter = 100; |
| 79 | + if(I2C_Host.WriteRead((uint16_t)(i2c_address), txBuffer, txLength, data, rxLength)) |
| 80 | + { |
| 81 | + while (I2C_Host.IsBusy() && (waitCounter > 0U)) |
| 82 | + { |
| 83 | + I2C_Host.Tasks(); |
| 84 | + waitCounter--; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + errorState = I2C_Host.ErrorGet(); |
| 89 | + return errorState; |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +uint8_t MCP23008_Write(uint8_t i2c_address, uint8_t reg, uint8_t data) |
| 94 | +{ |
| 95 | + i2c_host_error_t errorState = I2C_ERROR_NONE; |
| 96 | + size_t txLength = 2; |
| 97 | + uint8_t txBuffer[2] = {0}; |
| 98 | + |
| 99 | + txBuffer[0] = reg; // I2C Client register to write to |
| 100 | + txBuffer[1] = data; |
| 101 | + |
| 102 | + uint8_t waitCounter = 100; |
| 103 | + if(I2C_Host.Write((uint16_t)i2c_address, txBuffer, txLength)) |
| 104 | + { |
| 105 | + while (I2C_Host.IsBusy() && (waitCounter > 0U)) |
| 106 | + { |
| 107 | + I2C_Host.Tasks(); |
| 108 | + waitCounter--; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + errorState = I2C_Host.ErrorGet(); |
| 113 | + return errorState; |
| 114 | +} |
| 115 | + |
| 116 | +void printButtonPressed(int pressedButton) { |
| 117 | + if (pressedButton >= 0 && pressedButton < 8) { |
| 118 | + (int) printf("Button pressed: %s\n", buttonLabels[pressedButton]); |
| 119 | + } else { |
| 120 | + (int) printf("Invalid button pressed\n"); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void button_led_control(void) |
| 125 | +{ |
| 126 | + uint8_t ioexInputs = 0; |
| 127 | + uint8_t defaultInputs = 0; |
| 128 | + uint8_t activeLEDs = 0; |
| 129 | + |
| 130 | + (uint8_t) MCP23008_Write(MCP23008_1_I2C_ADDRESS, MCP23008_IODIR, 0x00); |
| 131 | + (uint8_t) MCP23008_Write(MCP23008_1_I2C_ADDRESS, MCP23008_GPIO, 0x00); // After this line of code, the LEDs should be on. |
| 132 | + |
| 133 | + (uint8_t) MCP23008_Write(MCP23008_2_I2C_ADDRESS, MCP23008_IODIR, 0xFF); // Set IO-expander 2 pins as inputs |
| 134 | + (uint8_t) MCP23008_Write(MCP23008_2_I2C_ADDRESS, MCP23008_GPPU, 0xFF); // Set pull-up for all pins of IO-expander 2 |
| 135 | + (uint8_t) MCP23008_Write(MCP23008_2_I2C_ADDRESS, MCP23008_IOCON, 0x20); // Set SEQOP bit to 1 for Byte mode |
| 136 | + |
| 137 | + // Read the default state of the inputs |
| 138 | + (uint8_t) MCP23008_Read(MCP23008_2_I2C_ADDRESS, MCP23008_GPIO, &defaultInputs); |
| 139 | + (int) printf("Default inputs: 0x%02X\n", defaultInputs); |
| 140 | + |
| 141 | + while ((IO_Reset_GetValue() != 0) && !resetFlag) // Run until Reset SW is pressed |
| 142 | + { |
| 143 | + (uint8_t) MCP23008_Read(MCP23008_2_I2C_ADDRESS, MCP23008_GPIO, &ioexInputs); // Read GPIO register to get the state of the inputs |
| 144 | + |
| 145 | + uint8_t changed_inputs = defaultInputs ^ ioexInputs; // XOR to find changed bits |
| 146 | + if (!(changed_inputs == 0U)) // Check if there is any change from the default state |
| 147 | + { |
| 148 | + for (uint8_t i = 0U; i < 8U; i++) // Check each bit to see if a button is pressed |
| 149 | + { |
| 150 | + if (!(changed_inputs == 0U)) // Check if there is any change from the default state |
| 151 | + { |
| 152 | + if ((ioexInputs & (1U << i)) == 0U) // Button pressed (active low) |
| 153 | + { |
| 154 | + uint8_t pressedButton = i; |
| 155 | + activeLEDs = (activeLEDs | (1U << pressedButton)); |
| 156 | + (uint8_t) MCP23008_Write(MCP23008_1_I2C_ADDRESS, MCP23008_GPIO, activeLEDs); // Set IO-expander 1 pins low |
| 157 | + printButtonPressed(pressedButton); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + DELAY_milliseconds(100); // Add a small delay to avoid bouncing issues, presses longer than 100ms will register > once |
| 163 | + } |
| 164 | + resetFlag = true; |
| 165 | +} |
| 166 | + |
| 167 | +int main(void) |
| 168 | +{ |
| 169 | + DELAY_milliseconds(200); // Prevent program running when programming |
| 170 | + SYSTEM_Initialize(); |
| 171 | + (int) printf("Example: 3. I2C IO Expander 2 - LEDs and Buttons, Implementation: Polled, Visualization: Printf \r\n"); |
| 172 | + (int) printf("MCU Device family: AVR \r\n"); |
| 173 | + (int) printf("Press Curiosity Nano Explorer touch buttons and use Joystick to turn off all LEDS. Press SW0 on Curiosity Nano to exit. \r\n"); |
| 174 | + button_led_control(); |
| 175 | + |
| 176 | + while(1) |
| 177 | + { |
| 178 | + IO_LED_SetLow(); //Turn off LED during button_led_control() |
| 179 | + button_led_control(); |
| 180 | + |
| 181 | + if (resetFlag) // Check if the reset flag is set |
| 182 | + { |
| 183 | + (int) printf("Reset flag set, restarting button_led_control() \r\n\r\n"); |
| 184 | + resetFlag = false; // Clear the reset flag |
| 185 | + } |
| 186 | + } |
| 187 | + return 0; // Shouldn't get here!! |
| 188 | +} |
0 commit comments