Skip to content

Commit 8898ea4

Browse files
committed
MPAE-19438 created and tested mplabx project, working as expected
1 parent e1ea737 commit 8898ea4

36 files changed

+4519
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Please do not change this logo with link -->
1+
<!-- MPAE-19438 Please do not change this logo with link -->
22

33
<a target="_blank" href="https://www.microchip.com/" id="top-of-page">
44
<picture>
@@ -8,7 +8,15 @@
88
</picture>
99
</a>
1010

11-
# Update the title for pic18f57q43-i2c-io-expander2-leds-and-buttons-polled-printf here
11+
# PIC18F57Q43 I2C_Host Example Component for CNano Explorer: I/O Expander 2 LEDs and BUTTONs (Polled, Printf)
12+
13+
When running this example using the MPLAB Data Visualizer, the following is what the output will look like.
14+
15+
![alt text](images/pic18f57q43-i2c-io-expander2-leds-and-buttons-polled-printf.png)
16+
17+
This is the required MCC Melody Configuration to achieve this result.
18+
19+
![alt text](images/pic18f57q43-i2c-io-expander2-leds-and-buttons-polled-printf_configuration.png)
1220

1321
<!-- This is where the introduction to the example goes, including mentioning the peripherals used -->
1422

42.4 KB
Loading
190 KB
Loading
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#
2+
# There exist several targets which are by default empty and which can be
3+
# used for execution of your targets. These targets are usually executed
4+
# before and after some main targets. They are:
5+
#
6+
# .build-pre: called before 'build' target
7+
# .build-post: called after 'build' target
8+
# .clean-pre: called before 'clean' target
9+
# .clean-post: called after 'clean' target
10+
# .clobber-pre: called before 'clobber' target
11+
# .clobber-post: called after 'clobber' target
12+
# .all-pre: called before 'all' target
13+
# .all-post: called after 'all' target
14+
# .help-pre: called before 'help' target
15+
# .help-post: called after 'help' target
16+
#
17+
# Targets beginning with '.' are not intended to be called on their own.
18+
#
19+
# Main targets can be executed directly, and they are:
20+
#
21+
# build build a specific configuration
22+
# clean remove built files from a configuration
23+
# clobber remove all built files
24+
# all build all configurations
25+
# help print help mesage
26+
#
27+
# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and
28+
# .help-impl are implemented in nbproject/makefile-impl.mk.
29+
#
30+
# Available make variables:
31+
#
32+
# CND_BASEDIR base directory for relative paths
33+
# CND_DISTDIR default top distribution directory (build artifacts)
34+
# CND_BUILDDIR default top build directory (object files, ...)
35+
# CONF name of current configuration
36+
# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration)
37+
# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration)
38+
# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration)
39+
# CND_PACKAGE_DIR_${CONF} directory of package (current configuration)
40+
# CND_PACKAGE_NAME_${CONF} name of package (current configuration)
41+
# CND_PACKAGE_PATH_${CONF} path to package (current configuration)
42+
#
43+
# NOCDDL
44+
45+
46+
# Environment
47+
MKDIR=mkdir
48+
CP=cp
49+
CCADMIN=CCadmin
50+
RANLIB=ranlib
51+
52+
53+
# build
54+
build: .build-post
55+
56+
.build-pre:
57+
# Add your pre 'build' code here...
58+
59+
.build-post: .build-impl
60+
# Add your post 'build' code here...
61+
62+
63+
# clean
64+
clean: .clean-post
65+
66+
.clean-pre:
67+
# Add your pre 'clean' code here...
68+
# WARNING: the IDE does not call this target since it takes a long time to
69+
# simply run make. Instead, the IDE removes the configuration directories
70+
# under build and dist directly without calling make.
71+
# This target is left here so people can do a clean when running a clean
72+
# outside the IDE.
73+
74+
.clean-post: .clean-impl
75+
# Add your post 'clean' code here...
76+
77+
78+
# clobber
79+
clobber: .clobber-post
80+
81+
.clobber-pre:
82+
# Add your pre 'clobber' code here...
83+
84+
.clobber-post: .clobber-impl
85+
# Add your post 'clobber' code here...
86+
87+
88+
# all
89+
all: .all-post
90+
91+
.all-pre:
92+
# Add your pre 'all' code here...
93+
94+
.all-post: .all-impl
95+
# Add your post 'all' code here...
96+
97+
98+
# help
99+
help: .help-post
100+
101+
.help-pre:
102+
# Add your pre 'help' code here...
103+
104+
.help-post: .help-impl
105+
# Add your post 'help' code here...
106+
107+
108+
109+
# include project implementation makefile
110+
include nbproject/Makefile-impl.mk
111+
112+
# include project make variables
113+
include nbproject/Makefile-variables.mk
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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: PIC18F/18F
35+
*/
36+
37+
#include "mcc_generated_files/system/system.h"
38+
#include <stdbool.h>
39+
40+
// Note: MCP23008 - 8-Bit I2C I/O Expander with I2C (& SPI) Serial Interface(s)
41+
// Reference to the MCP23008 data sheet: https://www.microchip.com/DS20001919
42+
// MCP23008 register addresses are in Table 1-2 (page 5) of the data sheet
43+
// The Curiosity Nano Explorer (EV58G97A) has 2 x MCP23008 IO Expanders
44+
45+
#define MCP23008_1_I2C_ADDRESS 0x25
46+
#define MCP23008_2_I2C_ADDRESS 0x24
47+
48+
#define MCP23008_IODIR 0x00
49+
#define MCP23008_IPOL 0x01
50+
#define MCP23008_GPINTEN 0x02 // INTERRUPT-ON-CHANGE CONTROL (GPINTEN) REGISTER, GPINTEN â?? enables the individual inputs
51+
#define MCP23008_DEFVAL 0x03 // DEFAULT COMPARE (DEFVAL) â?? holds the values that are compared against the associated input port values
52+
#define MCP23008_INTCON 0x04 // INTERRUPT CONTROL (INTCON) - Controls if the input values are compared against DEFVAL or the previous values on the portREGISTER:
53+
#define MCP23008_IOCON 0x05 // IOCON (ODR and INPOL) â?? configures the INT pin as push-pull, open-drain and active-level
54+
#define MCP23008_GPPU 0x06
55+
#define MCP23008_INTF 0x07 // INTERRUPT FLAG (INTF) REGISTER: 1 = Pin caused interrupt
56+
#define MCP23008_INTCAP 0X08
57+
#define MCP23008_GPIO 0x09
58+
#define MCP23008_OLAT 0x0A
59+
60+
extern void DELAY_milliseconds(uint16_t milliseconds);
61+
uint8_t MCP23008_Read(uint8_t i2c_address, uint8_t reg, uint8_t* data);
62+
uint8_t MCP23008_Write(uint8_t i2c_address, uint8_t reg, uint8_t data);
63+
void printButtonPressed(int pressedButton);
64+
void button_led_control(void);
65+
66+
static volatile bool resetFlag = false;
67+
// All joystick pins re-mapped as they are not connected to Curiosity Nano GPIO by default
68+
static const char* buttonLabels[8] = {"J-UP", "J-LEFT", "J-DOWN", "J-RIGHT", "J-PUSH", "SW3", "SW2", "SW1"};
69+
70+
uint8_t MCP23008_Read(uint8_t i2c_address, uint8_t reg, uint8_t* data)
71+
{
72+
i2c_host_error_t errorState = I2C_ERROR_NONE;
73+
size_t txLength = 1;
74+
size_t rxLength = 1;
75+
uint8_t txBuffer[1] = {0};
76+
77+
txBuffer[0] = reg; // I2C Client register to read from
78+
79+
uint8_t waitCounter = 100;
80+
if(I2C_Host.WriteRead((uint16_t)(i2c_address), txBuffer, txLength, data, rxLength))
81+
{
82+
while (I2C_Host.IsBusy() && (waitCounter > 0U))
83+
{
84+
I2C_Host.Tasks();
85+
waitCounter--;
86+
}
87+
}
88+
89+
errorState = I2C_Host.ErrorGet();
90+
return errorState;
91+
}
92+
93+
94+
uint8_t MCP23008_Write(uint8_t i2c_address, uint8_t reg, uint8_t data)
95+
{
96+
i2c_host_error_t errorState = I2C_ERROR_NONE;
97+
size_t txLength = 2;
98+
uint8_t txBuffer[2] = {0};
99+
100+
txBuffer[0] = reg; // I2C Client register to write to
101+
txBuffer[1] = data;
102+
103+
uint8_t waitCounter = 100;
104+
if(I2C_Host.Write((uint16_t)i2c_address, txBuffer, txLength))
105+
{
106+
while (I2C_Host.IsBusy() && (waitCounter > 0U))
107+
{
108+
I2C_Host.Tasks();
109+
waitCounter--;
110+
}
111+
}
112+
113+
errorState = I2C_Host.ErrorGet();
114+
return errorState;
115+
}
116+
117+
void printButtonPressed(int pressedButton) {
118+
if (pressedButton >= 0 && pressedButton < 8) {
119+
(int) printf("Button pressed: %s\n", buttonLabels[pressedButton]);
120+
} else {
121+
(int) printf("Invalid button pressed\n");
122+
}
123+
}
124+
125+
void button_led_control(void)
126+
{
127+
uint8_t ioexInputs = 0;
128+
uint8_t defaultInputs = 0;
129+
uint8_t activeLEDs = 0;
130+
131+
(uint8_t) MCP23008_Write(MCP23008_1_I2C_ADDRESS, MCP23008_IODIR, 0x00);
132+
(uint8_t) MCP23008_Write(MCP23008_1_I2C_ADDRESS, MCP23008_GPIO, 0x00); // After this line of code, the LEDs should be on.
133+
134+
(uint8_t) MCP23008_Write(MCP23008_2_I2C_ADDRESS, MCP23008_IODIR, 0xFF); // Set IO-expander 2 pins as inputs
135+
(uint8_t) MCP23008_Write(MCP23008_2_I2C_ADDRESS, MCP23008_GPPU, 0xFF); // Set pull-up for all pins of IO-expander 2
136+
(uint8_t) MCP23008_Write(MCP23008_2_I2C_ADDRESS, MCP23008_IOCON, 0x20); // Set SEQOP bit to 1 for Byte mode
137+
138+
// Read the default state of the inputs
139+
(uint8_t) MCP23008_Read(MCP23008_2_I2C_ADDRESS, MCP23008_GPIO, &defaultInputs);
140+
(int) printf("Default inputs: 0x%02X\n", defaultInputs);
141+
142+
while (IO_Reset_GetValue() && !resetFlag) // Run until Reset SW is pressed
143+
{
144+
(uint8_t) MCP23008_Read(MCP23008_2_I2C_ADDRESS, MCP23008_GPIO, &ioexInputs); // Read GPIO register to get the state of the inputs
145+
146+
uint8_t changed_inputs = defaultInputs ^ ioexInputs; // XOR to find changed bits
147+
if (!(changed_inputs == 0U)) // Check if there is any change from the default state
148+
{
149+
for (uint8_t i = 0U; i < 8U; i++) // Check each bit to see if a button is pressed
150+
{
151+
if (!(changed_inputs == 0U)) // Check if there is any change from the default state
152+
{
153+
if ((ioexInputs & (1U << i)) == 0U) // Button pressed (active low)
154+
{
155+
uint8_t pressedButton = i;
156+
activeLEDs = (activeLEDs | (1U << pressedButton));
157+
(uint8_t) MCP23008_Write(MCP23008_1_I2C_ADDRESS, MCP23008_GPIO, activeLEDs); // Set IO-expander 1 pins low
158+
printButtonPressed(pressedButton);
159+
}
160+
}
161+
}
162+
}
163+
DELAY_milliseconds(100); // Add a small delay to avoid bouncing issues, presses longer than 100ms will register > once
164+
}
165+
resetFlag = true;
166+
}
167+
168+
int main(void)
169+
{
170+
DELAY_milliseconds(200); // Prevent program running when programming
171+
SYSTEM_Initialize();
172+
(int) printf("Example: 3. I2C IO Expander 2 - LEDs and Buttons, Implementation: Polled, Visualization: Printf \r\n");
173+
(int) printf("MCU Device family: PIC18F/18F \r\n");
174+
(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");
175+
button_led_control();
176+
177+
while(1)
178+
{
179+
IO_LED_SetLow(); //Turn off LED during button_led_control()
180+
button_led_control();
181+
182+
if (resetFlag) // Check if the reset flag is set
183+
{
184+
(int) printf("Reset flag set, restarting button_led_control() \r\n\r\n");
185+
resetFlag = false; // Clear the reset flag
186+
}
187+
}
188+
return 0; // Shouldn't get here!!
189+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This file has been autogenerated by MPLAB Code Configurator. Please do not edit this file.
2+
3+
manifest_file_version: 1.0.0
4+
project: pic18f57q43-i2c-io-expander2-leds-and-buttons-polled-printf
5+
creation_date: 2025-04-11T13:57:12.268+02:00[Europe/Berlin]
6+
operating_system: Windows 11
7+
mcc_mode: IDE
8+
mcc_mode_version: v6.25
9+
device_name: PIC18F57Q43
10+
compiler: XC8 3.00
11+
mcc_version: 5.5.2
12+
mcc_core_version: 5.7.1
13+
content_manager_version: 5.0.1
14+
is_mcc_offline: false
15+
is_using_prerelease_versions: true
16+
mcc_content_registries: https://registry.npmjs.org/,https://artifacts.microchip.com/artifactory/api/npm/npm/
17+
device_library: {library_class: com.microchip.mcc.melody.Library, name: Melody, version: 2.8.1}
18+
packs: {name: PIC18F-Q_DFP, version: 1.28.451}
19+
modules:
20+
- {name: '@mchp-mcc/delay-blocking-driver', type: MELODY, version: 3.2.0}
21+
- {name: '@mchp-mcc/example-i2c-host', type: MELODY, version: 1.0.0}
22+
- {name: '@mchp-mcc/i2c-host-driver', type: MELODY, version: 1.1.0-dev.1}
23+
- {name: '@mchp-mcc/main-manager', type: MELODY, version: 3.1.2}
24+
- {name: '@mchp-mcc/pic-8bit', type: MELODY, version: 5.29.2-dev.1}
25+
- {name: '@mchp-mcc/pic18-configuration-bits-v1', type: MELODY, version: 5.3.5}
26+
- {name: '@mchp-mcc/pic18-pin-manager', type: MELODY, version: 3.7.1}
27+
- {name: '@mchp-mcc/pin-content-processor', type: MELODY, version: 3.9.1-feat.6}
28+
- {name: '@mchp-mcc/scf-pic8-i2c-v1', type: MELODY, version: 6.1.4}
29+
- {name: '@mchp-mcc/scf-pic8-interrupt-v1', type: MELODY, version: 5.2.11}
30+
- {name: '@mchp-mcc/scf-pic8-osc-v1', type: MELODY, version: 4.4.0}
31+
- {name: '@mchp-mcc/scf-pic8-uart-v1', type: MELODY, version: 7.2.0}
32+
- {name: '@mchp-mcc/uart-driver', type: MELODY, version: 1.10.2}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This file has been autogenerated by MPLAB Code Configurator. Please do not edit this file.
2+
3+
manifest_file_version: 1.0.0
4+
project: pic18f57q43-i2c-io-expander2-leds-and-buttons-polled-printf
5+
creation_date: 2025-04-11T13:57:12.228+02:00[Europe/Berlin]
6+
operating_system: Windows 11
7+
mcc_mode: IDE
8+
mcc_mode_version: v6.25
9+
device_name: PIC18F57Q43
10+
compiler: XC8 3.00
11+
mcc_version: 5.5.2
12+
mcc_core_version: 5.7.1
13+
content_manager_version: 5.0.1
14+
is_mcc_offline: false
15+
is_using_prerelease_versions: true
16+
mcc_content_registries: https://registry.npmjs.org/,https://artifacts.microchip.com/artifactory/api/npm/npm/
17+
device_library: {library_class: com.microchip.mcc.melody.Library, name: Melody, version: 2.8.1}
18+
packs: {name: PIC18F-Q_DFP, version: 1.28.451}
19+
modules:
20+
- {name: '@mchp-mcc/delay-blocking-driver', type: MELODY, version: 3.2.0}
21+
- {name: '@mchp-mcc/example-i2c-host', type: MELODY, version: 1.0.0}
22+
- {name: '@mchp-mcc/i2c-host-driver', type: MELODY, version: 1.1.0-dev.1}
23+
- {name: '@mchp-mcc/main-manager', type: MELODY, version: 3.1.2}
24+
- {name: '@mchp-mcc/pic-8bit', type: MELODY, version: 5.29.2-dev.1}
25+
- {name: '@mchp-mcc/pic18-configuration-bits-v1', type: MELODY, version: 5.3.5}
26+
- {name: '@mchp-mcc/pic18-pin-manager', type: MELODY, version: 3.7.1}
27+
- {name: '@mchp-mcc/pin-content-processor', type: MELODY, version: 3.9.1-feat.6}
28+
- {name: '@mchp-mcc/scf-pic8-i2c-v1', type: MELODY, version: 6.1.4}
29+
- {name: '@mchp-mcc/scf-pic8-interrupt-v1', type: MELODY, version: 5.2.11}
30+
- {name: '@mchp-mcc/scf-pic8-osc-v1', type: MELODY, version: 4.4.0}
31+
- {name: '@mchp-mcc/scf-pic8-uart-v1', type: MELODY, version: 7.2.0}
32+
- {name: '@mchp-mcc/uart-driver', type: MELODY, version: 1.10.2}

0 commit comments

Comments
 (0)