{"id":800,"date":"2018-02-09T16:19:12","date_gmt":"2018-02-09T16:19:12","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=800"},"modified":"2022-02-18T15:37:48","modified_gmt":"2022-02-18T15:37:48","slug":"i2c-without-using-xc32-library","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic32\/xc32-v2-old\/i2c\/i2c-without-using-xc32-library","title":{"rendered":"I2C without using XC32 library"},"content":{"rendered":"<h4>\nMaster Fast Read (no write of regsiter address first)<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#define\tMEMS_SENSOR_I2C_MASTER_IRQ_FLAG\t\t\t\t0x0080\r\n\r\n\ti2c_comms_failed = 0;\r\n\r\n\t\/\/----- SEND THE START CONDITION -----\r\n\tI2C4CON |= 0x0001;\t\t\t\/\/Set SEN\r\n\twhile (I2C4CON &amp; 0x0001)\t\/\/Wait for SEN to clear\r\n\t\t;\r\n\r\n\r\n\t\/\/----- SEND THE I2C DEVICE ADDRESS WITH THE READ BIT SET -----\r\n\tIFS1 &amp;= ~MEMS_SENSOR_I2C_MASTER_IRQ_FLAG;\r\n\tI2C4TRN = (MEMS_SENSOR_B_I2C_ADDRESS | 0x01);\t\t\t\/\/Bit 1 high for read\r\n\twhile (!(IFS1 &amp; MEMS_SENSOR_I2C_MASTER_IRQ_FLAG))\r\n\t\t;\r\n\t\/\/Check ACK bit\r\n\tif (I2C4STAT &amp; 80000)\r\n\t\ti2c_comms_failed = 1;\t\t\t\/\/Should be low for ACK\r\n\r\n\t\/\/----- READ THE DATA BYTES -----\r\n\tfor (count = 0; count &lt; 6; count++)\r\n\t{\r\n\t\t\/\/Read byte\r\n\t\tIFS1 &amp;= ~MEMS_SENSOR_I2C_MASTER_IRQ_FLAG;\r\n\t\tI2C4CON |= 0x0008;\t\t\t\/\/Set RCEN\r\n\t\twhile (!(IFS1 &amp; MEMS_SENSOR_I2C_MASTER_IRQ_FLAG))\r\n\t\t\t;\r\n\t\tdata[count] = I2C4RCV;\r\n\r\n\t\t\/\/Acknowledge\r\n\t\twhile (I2C4CON &amp; 0x001f)\t\/\/The five Least Significant bits of I2CxCON must be &lsquo;0&rsquo; (master logic inactive) before attempting to set the ACKEN bit\r\n\t\t\t;\r\n\t\tif (count &lt; (6-1))\r\n\t\t\tI2C4CON &amp;= ~0x0020;\t\t\/\/Set ACKDT (0=ACK, 1=NAK)\r\n\t\telse\r\n\t\t\tI2C4CON |= 0x0020;\t\t\/\/Set ACKDT (0=ACK, 1=NAK)\r\n\t\tI2C4CON |= 0x0010;\t\t\t\/\/Set ACKEN\r\n\t\twhile (I2C4CON &amp; 0x0010)\r\n\t\t\t;\r\n\r\n\t\tif (i2c_comms_failed)\r\n\t\t\tdata[count] = 0;\r\n\t}\r\n\r\n\t\/\/----- SEND STOP -----\r\n\tI2C4CON |= 0x0004;\t\t\t\/\/Set PEN\r\n\twhile (I2C4CON &amp; 0x0004)\t\/\/Wait for PEN to clear\r\n\t\t;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Fast Read (no write of regsiter address first) #define MEMS_SENSOR_I2C_MASTER_IRQ_FLAG 0x0080 i2c_comms_failed = 0; \/\/&#8212;&#8211; SEND THE START CONDITION &#8212;&#8211; I2C4CON |= 0x0001; \/\/Set SEN while (I2C4CON &amp; 0x0001) \/\/Wait for SEN to clear ; \/\/&#8212;&#8211; SEND THE I2C DEVICE ADDRESS WITH THE READ BIT SET &#8212;&#8211; IFS1 &amp;= ~MEMS_SENSOR_I2C_MASTER_IRQ_FLAG; I2C4TRN = (MEMS_SENSOR_B_I2C_ADDRESS | [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-800","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/800","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=800"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/800\/revisions"}],"predecessor-version":[{"id":802,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/800\/revisions\/802"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}