{"id":1446,"date":"2023-09-15T15:25:42","date_gmt":"2023-09-15T14:25:42","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=1446"},"modified":"2023-09-21T10:43:08","modified_gmt":"2023-09-21T09:43:08","slug":"i2c-master-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic32\/xc32\/i2c-xc32\/i2c-master-2","title":{"rendered":"I2C Master"},"content":{"rendered":"\n<p>Configure I2C in MCC<\/p>\n\n\n\n<p>Once you Generate the MCC code it will have dealt with initialising the I2C port ready for you to use it.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">In your code<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;definitions.h>\t\t\t\/\/Include all the MCC generated files so their fucntions can be called\n\n\n\n\n\/\/IN YOUR INITIALSE CODE\n\t\/\/----- SETUP I2C 4 -----\n\t\/\/Used for: \n\t\/\/Setup I2C using MCC:\n\t\/\/\tSetup I2C:\n\t\/\/\t\tOperating Mode: Master\n\t\/\/\t\tDisable Slew Rate Control: No (disable for Standard Speed mode (100 kHz) and 1 MHz mode, enable for High Speed mode (400 kHz))\n\t\/\/\t\tSMBus Input Levels: No\n\t\/\/\t\tStop in Idle Mode bit: Yes (Yes=Discontinue module operation when the device enters Idle mode)\n\t\/\/\t\tI2C Band Rate (hz): 400000\n\t\/\/\t\tSDA Hold Time: Min 100nS (Delay shouldn't normally be needed)\n\t\/\/\t\tInclude Force Write I2C Function: No\n\t\/\/\tSetup interrupts:\n\t\/\/\t\tEnable I2C4 Bus Collision Event Interrupt = Will already be set to Yes\n\t\/\/\t\tEnable I2C4 SlaveEvent Interrupt = Will already be set to Yes\n\t\/\/\t\tEnable I2C4 Master Event Interrupt: Will already be set to Yes\n\t\/\/\t\tPriority (set for all): 3\t\/\/&lt;&lt;&lt;Set as required for your application\n\t\/\/\t\tSubpriority (set for all): 0\t\/\/&lt;&lt;&lt;Set as required for your application\n\t\n\t\/\/I2C4_CallbackRegister(I2C4_MyEventHandler,(uintptr_t)NULL);\t\t\/\/Register my callback function to be called when the I2C operation completes (optional)\n\n\n\/\/HOW TO PERFORM A READ OPERATION\n\/*\n#define\tMY_I2C_ADDRESS\t\t0xa0\n\tuint8_t RxData&#91;1];\n\tif (I2C4_Read((MY_I2C_ADDRESS >> 1), &amp;RxData&#91;0], sizeof(RxData)))\t\t\/\/Address needs passing in bits6:0\n\t{\n\t\t\/\/READ STARTED\t\t\n\t\twhile (I2C4_IsBusy())\n\t\t\t;\n\t\t\n\t\t\/\/READ COMPLETED\n\t\tif (I2C4_ErrorGet() == I2C_ERROR_NONE)\n\t\t{\n\t\t\t\n\t\t}\n\t}\n*\/\n\n\n\/\/HOW TO PERFORM A WRITE OPERATION\n\/*\n#define\tMY_I2C_ADDRESS\t\t0xa0\n\tuint8_t TxData&#91;1] = {0x00};\n\tif (I2C4_Write((MY_I2C_ADDRESS >> 1), &amp;TxData&#91;0], sizeof(TxData)))\t\t\/\/Address needs passing in bits6:0\n\t{\n\t\t\/\/WRITE STARTED\n\t\twhile (I2C4_IsBusy())\n\t\t\t;\n\t\t\n\t\t\/\/WRITE COMPLETED\n\t\tif (I2C4_ErrorGet() == I2C_ERROR_NONE)\n\t\t{\n\t\t\t\n\t\t}\n\t}\n*\/\n\n\n\/\/HOW TO PERFORM A WRITE READ OPERATION\n\/*\n#define\tMY_I2C_ADDRESS\t\t0xa0\n\tuint8_t RxData&#91;1];\n\tuint8_t TxData&#91;1] = {0x00};\n\tif (I2C4_WriteRead((MY_I2C_ADDRESS >> 1), &amp;TxData&#91;0], sizeof(TxData), &amp;RxData&#91;0], sizeof(RxData)))\t\t\/\/Address needs passing in bits6:0\n\t{\n\t\t\/\/WRITE READ STARTED\n\t\twhile (I2C4_IsBusy())\n\t\t\t;\n\t\t\n\t\t\/\/WRITE READ COMPLETED\n\t\tif (I2C4_ErrorGet() == I2C_ERROR_NONE)\n\t\t{\n\t\t\t\n\t\t}\n\t}\n*\/\n\n\n\/\/*************************************************\n\/\/*************************************************\n\/\/********** I2C4 OPERATION COMPLETE IRQ **********\n\/\/*************************************************\n\/\/*************************************************\n\/*\nvoid I2C4_MyEventHandler (uintptr_t context)\n{\n\tif (I2C4_ErrorGet() != I2C_ERROR_NONE)\n\t{\n\t\t\/\/AN ERROR OCCURED\n\t}\n\telse\n\t{\n\t\t\/\/LAST OPERATION COMPLETED (Stop bit)\n\t\t\n\t}\n}\n*\/\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Other UART functions<\/h5>\n\n\n\n<p>MCC will have created a few useful functions you can call &#8211; see the file it generated in: \/config\/default\/peripheral\/i2c\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Configure I2C in MCC Once you Generate the MCC code it will have dealt with initialising the I2C port ready for you to use it. In your code Other UART functions MCC will have created a few useful functions you can call &#8211; see the file it generated in: \/config\/default\/peripheral\/i2c\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115],"tags":[],"class_list":["post-1446","post","type-post","status-publish","format-standard","hentry","category-i2c-xc32"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/comments?post=1446"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1446\/revisions"}],"predecessor-version":[{"id":1473,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1446\/revisions\/1473"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=1446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=1446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=1446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}