{"id":1566,"date":"2024-07-19T11:36:15","date_gmt":"2024-07-19T10:36:15","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=1566"},"modified":"2024-07-24T16:58:12","modified_gmt":"2024-07-24T15:58:12","slug":"i2c-using-mcc-3","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic18\/xc8-compiler\/i2c-xc8-compiler\/i2c-using-mcc-3","title":{"rendered":"I2C (using MCC)"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">I2C Address<\/h4>\n\n\n\n<p>Set as right shifted byte address (excluding RW at bit 0).<br>So 0b01101000 is set as (0x68 >> 1), not 0x68.<\/p>\n\n\n\n<p>We always specify I2C addresses as the normal byte address, so we would use (0x68 >> 1) when setting the address to be used.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example usage (without interrupts)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mcc_generated_files\/i2c_host\/mssp1.h\"\n\n\/\/WRITE BYTES\n\tuint8_t TxData&#91;2] = {1,2}\n\tI2C1_Write(MCP3425_I2C_DEVICE_ADDRESS, &amp;TxData&#91;0], 2);\n\twhile (I2C1_IsBusy())\n\t\tI2C1_Tasks();\n\tResult = I2C1_ErrorGet();\t\t\/\/0= no error (success), &gt;0 = error\n\n\/\/READ BYTES\n\tuint8_t RxData&#91;3];\n\tI2C1_Read(MCP3425_I2C_DEVICE_ADDRESS, &amp;RxData&#91;0], 3);\n\twhile (I2C1_IsBusy())\n\t\tI2C1_Tasks();\n\tResult = I2C1_ErrorGet();\t\t\/\/0= no error (success), &gt;0 = error\n\n\/\/WRITE READ BYTES\n\tuint8_t TxData&#91;2] = {1,2}\n\tuint8_t RxData&#91;3];\n\tI2C1_WriteRead(MCP3425_I2C_DEVICE_ADDRESS, &amp;TxData&#91;0], 2, &amp;RxData&#91;0], 3);\n\twhile (I2C1_IsBusy())\n\t\tI2C1_Tasks();\n\tResult = I2C1_ErrorGet();\t\t\/\/0= no error (success), &gt;0 = error<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">How we use in our libraries<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">In our .h file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>bool I2C1_Write_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength);\nbool I2C1_Read_Blocking (uint16_t Address, uint8_t *RxData, size_t RxLength);\nbool I2C1_WriteRead_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength, uint8_t *RxData, size_t RxLength);\n\n\/\/WRITE BYTES\n#define IO_EXPANDER_I2C_WRITE(Address, TxData, TxLength)\t\t\t\t\t\t\tI2C1_Write_Blocking((Address >> 1), TxData, TxLength)\n\n\/\/READ BYTES\n#define IO_EXPANDER_I2C_READ(Address, RxData, RxLength)\t\t\t\t\t\t\t\tI2C1_Read_Blocking((Address >> 1), RxData, RxLength)\n\n\/\/WRITE READ BYTES\n#define IO_EXPANDER_I2C_WRITE_READ(Address, TxData, TxLength, RxData, RxLength)\t\tI2C1_WriteRead_Blocking((Address >> 1), TxData, TxLength, RxData, RxLength)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">In our .c file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mcc_generated_files\/i2c_host\/mssp1.h\"\n\n\/\/We have to create functions for access because the MCC I2C code is non blocking whereas we want it blocking until complete\n\n\/\/WRITE BYTES\nbool I2C1_Write_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength)\n{\n\tNop();\n\tNop();\n\tNop();\n\tif (I2C1_Write(Address, TxData, TxLength) == 0)\n\t\treturn(0);\n\twhile (I2C1_IsBusy())\n\t\tI2C1_Tasks();\n\tif (I2C1_ErrorGet() == 0)\t\t\/\/0= no error (success), >0 = error\n\t\treturn(1);\n\telse\n\t\treturn(0);\n}\n\n\/\/READ BYTES\nbool I2C1_Read_Blocking (uint16_t Address, uint8_t *RxData, size_t RxLength)\n{\n\tif (I2C1_Read(Address, RxData, RxLength) == 0)\n\t\treturn(0);\n\twhile (I2C1_IsBusy())\n\t\tI2C1_Tasks();\n\tif (I2C1_ErrorGet() == 0)\t\t\/\/0= no error (success), >0 = error\n\t\treturn(1);\n\telse\n\t\treturn(0);\n}\n\n\/\/WRITE READ BYTES\nbool I2C1_WriteRead_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength, uint8_t *RxData, size_t RxLength)\n{\n\tif (I2C1_WriteRead(Address, TxData, TxLength, RxData, RxLength) == 0)\n\t\treturn(0);\n\twhile (I2C1_IsBusy())\n\t\tI2C1_Tasks();\n\tif (I2C1_ErrorGet() == 0)\t\t\/\/0= no error (success), >0 = error\n\t\treturn(1);\n\telse\n\t\treturn(0);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I2C Address Set as right shifted byte address (excluding RW at bit 0).So 0b01101000 is set as (0x68 >> 1), not 0x68. We always specify I2C addresses as the normal byte address, so we would use (0x68 >> 1) when setting the address to be used. Example usage (without interrupts) How we use in our [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81,131],"tags":[],"class_list":["post-1566","post","type-post","status-publish","format-standard","hentry","category-i2c-xc8-compiler","category-pic18-peripherals"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1566","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=1566"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1566\/revisions"}],"predecessor-version":[{"id":1588,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1566\/revisions\/1588"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=1566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=1566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}