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 MCC logo icon in the MPLAB X IDE toolbar.
Information

Note: MCC may take a while to load the first time it is launched.

Select MCC Classic from MCC Content Manager Wizard.

Select MCC Classic

Click Finish since no additional components are needed.

Click Finish

Configure System Module

Now you can start configuring the settings for the PIC18F47Q10.

Click System Module in the upper left window.

Select System Module

For System Module, we will use the HFINTOSC Oscillator for the clock source. Verify the System Module configuration window matches the accompanying image:

Select HFINTOSC

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.

Add I2CSIMPLE

Select the I2CSIMPLE Foundation Service in the Project Resources window to configure its settings:

Select I2CSIMPLE

Information

Note: The MSSP1 [Foundation Services Library] will be automatically added to the Peripherals Folder since the I2C function is supported by the MSSP CIP.

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.

Open I2CSIMPLE

Verify settings match the accompanying image:

MCC I2C Configuration

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.

Add EUSART2

Select EUSART2 in the Project Resources window.

Open EUSART2

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 EUSART2

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:

Configure GPIO

Generate MCC Code
Press the Generate Button Generate MCC Code button.
Verify the MCC code generation is successful.

MCC Code Generation Successful

Update main.c
Click on the Projects tab, open the Source Files folder and double-click on main.c to open it.

Open main.c

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
Information

Note: The MCP9600 datasheet can be used to find the target register addresses.

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:

Add variables to main.c

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.

Foundation Services I2C Read 2 Bytes Function

Information

Note: This function will be used to read two registers of the MCP9600 over the I2C bus to retrieve temperature data.

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));
Information

Note: The value read is shifted to convert value to Celsius.

Verify your code additions:

Add code to main.c