Demonstrating 8-bit I2C Controller Read, Step 3: Configure PIC18F47Q10 Resources with MCC
Last modified by Microchip on 2023/11/09 08:54
Launch MPLAB® Code Configurator (MCC)
Open MCC by clicking the MCC logo in the MPLAB X IDE toolbar.
Select MCC Classic from MCC Content Manager Wizard.
Click Finish since no additional components are needed.
Configure System Module
Now you can start configuring the settings for the PIC18F47Q10.
Click System Module in the upper left window.
For System Module, we will use the HFINTOSC Oscillator for the clock source. Verify the System Module configuration window matches the accompanying image:
Configure I2C
Now we will add the Core Independent Peripherals (CIPs) needed for the project and configure them. Inter-Integrated Circuit (I2C) will be configured to read the temperature from the MCP9600 EMF to Thermal Converter on the Thermo J Click board™.
Add I2C by opening the Foundations Services Driver folder in Device Resources window and clicking the + icon for I2CSIMPLE.
Select the I2CSIMPLE Foundation Service in the Project Resources window to configure its settings:
Open the I2CSIMPLE Help Navigator by clicking the blue question mark by I2CSIMPLE. It will be referenced later when an I2C Foundation Service function is added to the main.c file.
Verify settings match the accompanying image:
Configure EUSART
The EUSART is used to transmit temperature data to the Curiosity Nano virtual serial port.
Add EUSART2 by opening the EUART folder in the Device Resources window and clicking the + icon for EUART2.
Select EUSART2 in the Project Resources window.
Update EUSART2 configuration window for the following settings:
Check Enable Transmit
- Redirect STDIO to USART
- 9600 baud rate
Verify settings match the accompanying image:
Configure GPIO
General Purpose Input/Outputs (GPIO) are configured EUSART and I2C input/output.
Select Pin Manager: Grid View in the bottom right window. Click each of the following pins to create a green lock as either an input or output:
MSSP1: SCL1 to RB1
- MSSP1: SDA1 to RB2
- EUSART2: RX2 to RD1
- EUSART2: TX2 to RD0
Verify settings are as shown in the accompanying image:
Generate MCC Code
Press the Generate Button .
Verify the MCC code generation is successful.
Update main.c
Click on the Projects tab, open the Source Files folder and double-click on main.c to open it.
Add the following global constants in main.c to address the temperature registers of the MCP9600:
#define MCP9600_I2C_TARGET_ADDR 0b1100000 //Target device address
#define MCP9600_REGISTER_Th_ADDR 0b00000000 //Th register memory pointer
#define MCP9600_REGISTER_Th_ADDR 0b00000000 //Th register memory pointer
Declare a variable in the main to store last read value and initialize it to 0 in main.c:
uint16_t sensor_val = 0; //Variable to hold sensor value
Verify your code changes look like the following:
Open the I2CSIMPLE Help Navigator in your browser: Navigate to the I2C Drivers -> I2C Simple Driver Source Code Reference -> Functions folder. Find the i2c_read2ByteRegister function description.
Add the following 2 lines of code in the while(1) loop in main.c:
sensor_val = i2c_read2ByteRegister(MCP9600_I2C_TARGET_ADDR, MCP9600_REGISTER_Th_ADDR);
printf("\nSensor temp reading (Celsius): %u ", (sensor_val >> 4));
printf("\nSensor temp reading (Celsius): %u ", (sensor_val >> 4));