I2C Bus Using a PIC24F1024GB610 and Explorer 16/32 Development Kit
by Navy_Guy in Circuits > Microcontrollers
2334 Views, 3 Favorites, 0 Comments
I2C Bus Using a PIC24F1024GB610 and Explorer 16/32 Development Kit
There are so many people online having trouble with the I2C bus on the PIC24FJ1024GB610 Microcontroller. After a few days of reading the Microchip forums and coming up empty, I put my brain to work and figured out the issue. I wanted to share the solution with everyone that is having the same issue.
Supplies
Development Hardware:
- Explorer 16/32 Development Kit from Microchip. Part # DM240001-3
- PIC24FJ1024GB610 Microcontroller from Microchip.
- 24LC256 I2C Serial EEPROM from Microchip.
- TC74A5-3.3VCT I2C Serial Digital Thermal Sensor from Microchip.
- (2 ea.) 4.7 kOhm resistors
- Various hookup wires
- Breadboard
Software:
- MPLAB X IDE Version 6.00
- MCC - MPLAB Code Configurator Version 5.1.9 with Core Version 5.4.4
- Foundation Services Version 0.2.3
- PIC24/dsPIC33/PIC32MM MCUs Version 1.171.1
- Packs: PIC24F-GA-GB-DFP Version 1.7.214
- XC16 Version 2.00 Compiler Tool chain
Other Software:
- PuTTY or Tera Term Serial Terminal for UART Communications.
- AutoDesk EAGLE Version 9.6.2 was used for the Hookup Schematic in this Project.
Lets Hook Up the Hardware
- Follow the schematic above to hook up the TC74A5-3.3VAT Temperature sensor and the 24LC256 EEPROM.
- Hook up the SCL and SDA lines of the two chips to Header J48, Pins P58_SCL2 and P59_SDA2 respectively.
- Make sure and disconnect Jumper JP2 on the Explorer 16/32 Dev. Kit, as this may affect the I2C Communications.
Note: I found out that I2C1, using Pins P66_SCL1 and P67_SDA1 was not working properly. Not sure exactly why I2C1 is not working, but when I tried I2C2, everything worked fine. So I believe that a lot of people were trying to use I2C1.
Setting Up the Project in MPLAB X 6.00
- Open up MPLAB X Version 6.00.
- Select File->New Project and pick Standalone Project, then click Next. (See image)
- Select Device (See image for settings), then click Next.
- Select Compiler (See image), then click Next.
- Finally, Select a name for your project and the folder for which to store it. (NO Image)
NOTE: For Tutorials on MPLAB X and MPLAB Code Configurator (MCC) go to Microchip University and sign up for an account.
Start MCC
- Click on the blue MCC Icon in the Tool Bar to open MPLAB Code Configurator.
- This will open up the "MCC Content Manager Wizard. Click on "Select MCC Classic"
- When the next screen Pops up, Select Finish.
System Module Set Up
- Set the Clock to "Primary Oscillator" at 8 MHz.
- Set the ICD (In Circuit Debugger) to PGEC2 and PGED2. This will be useful to Debug your project.
Set the MCC Core Version
- Check that the MPLAB Code Configurator plugin is Version 5.1.9
- Check that the Core Version is Version 5.4.4
Next, Let's Set Up the UART
- In the "Device Resources" window, click the arrow to expand the "Peripherals" for the PIC24.
- Scroll down the the UART arrow and expand it.
- Pick UART1 [PIC24 / dsPIC33 / PIC32MM MCUs by Microchip Technology, Inc.] by clicking on the Green + sign. (see Image)
- Next you will see UART1 show up in the "Project Resources" window under "Peripherals". Click on the UART1 Tab at the top of the Main window to select the setting for the UART.
- Check the box to "Redirect Printf to UART". This will allow us to use the printf statement to output data to the Terminal.
Set Up Pins for UART
- In the "Pin Manager: Grid View", select P49 and P50 for the UART RXB and TXB respectively. Refer to the Explorer 16/32 Development Board Schematic for this.
- The USB will output the data on the USB Micro connection J40 on the Development Board.
Last Step for UART
- In the bottom window, select the "Notifications [MCC]" tab and note the message about the UTX pin.
- In the upper window, select the "Pin Module" tab and check the "Start High" checkbox for the U1TX pin.
Next, Let's Setup the I2C Bus
- In the "Device Resources" window pick Libraries->Foundation Services.
- Click the Green + signs for I2C1Simple and DELAY. This will add these libraries to our project.
Go to I2CSIMPLE Tab on Main Window
- In the Main window Select the I2CSIMPLE Tab.
- Select "Communication with EEPROM 24LC256" from the drop down box.
Go to 16 Bit I2CMASTER Tab on Main Window
- In the "Select I2C" drop down box, pick I2C2.
Go to I2C2 Tab on Main Window
- In the "Mode Selection" drop down box, select "Master" and keep the other settings as they are.
- There is nothing to do for the "DELAY" tab.
Setup the Pins for I2C2
- If you look at the "Pin Module" and the "Pin Manager: Grid View" they should look like the image.
Generate the MCC Code
- In the upper left "Project Resources" window, click the button labeled "Generate". This will generate the UART, I2C2 and Delay code for the project.
- If you get the message "Generation Complete" with no errors we are ready to move onto the next step.
- On the Tool Bar on the top, Click the Blue "MCC" button to close MPLAB Code Configurator.
Build the Project to Check for Errors
- In the tool bar click on "Build Project" and look for the message, "Build Successful" in the Output Window.
Let's Add the Code in Main.c
/** Section: Included Files */ #include "mcc_generated_files/system.h" #include "mcc_generated_files/mcc.h" #include "mcc_generated_files/examples/i2c_simple_example.h" #define TC74_ADDRESS 0x4D uint8_t dataReady, tempC; float tempF; /* Main application */ int main(void) { // initialize the device SYSTEM_Initialize(); I2CSIMPLE_example(); while (1) { // Add your application code // Read Data Ready in Register 0x01 dataReady = i2c_read1ByteRegister( TC74_ADDRESS, 0x01 ); // Read CONFIG Register bit 6 position ( 1 = ready, 0 = not ready ) dataReady = dataReady & 0x40; if( dataReady ) { tempC = i2c_read1ByteRegister( TC74_ADDRESS, 0x00 ); // Read TEMP register tempF = ((float)tempC * 1.8 ) +32; printf("tempC = %d tempF = %4.1f \r\n", tempC, (double)tempF ); } DELAY_milliseconds(1000); } return 1; } /** End of File */
Downloads
Changes to I2C_simple_example.c File
- Make sure and comment out the "while(1)" loop at the end of the I2CSIMPLE_example() Function or the Code to display the temperature will not be reached.
- That is it for the coding. Most of it was taken care of my MPLAB X and MCC for us.
Downloads
Serial Output in PuTTY Window
- Well, that is it for this project. Hope this helps a lot of people that were having issues with i2c on the PIC24FJ1024GB610 Microcontroller.