{"id":752,"date":"2017-05-25T14:00:43","date_gmt":"2017-05-25T14:00:43","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=752"},"modified":"2024-07-18T16:29:09","modified_gmt":"2024-07-18T15:29:09","slug":"i2c-4","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic18\/xc8-compiler\/i2c-xc8-compiler\/i2c-4","title":{"rendered":"I2C Master"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\t\/\/-----------------------------------------------\n\t\/\/----- SETUP SYNCHRONOUS SERIAL PORT (SSP) -----\n\t\/\/-----------------------------------------------\n\t\/\/----- SETUP SSP AS I2C -----\n\t\/\/Used for: LED Driver\n\t\/\/I2C requirements:- 100kHz clock high time 4.0uS, clock low time 4.7uS, 400kHz clock high time 0.6uS, clock low time 1.3uS\n\t\/\/The PIC18 does not fully conform to the 400kHz I2C spec (which applies to all rates >100kHz) but may be used with care\n\t\/\/where higher rates are required.\n\t\/\/Lower 7 bits of SSPADD are the baud rate.  ((Tcycle \/ 2) * SSPADD * 2) = time per bit.  1 \/ time per bit = frequency.\n\t\/\/400kHz @ 40MHz OSC = SSPADD 25\n\t\/\/400kHz @ 16MHz OSC = SSPADD 9\n\t\/\/100kHz @ 40MHz OSC = SSPADD 100\n\t\/\/100kHz @ 32MHz OSC = SSPADD 80\n\t\/\/100kHz @ 16MHz OSC = SSPADD 39\n\t\/\/100kHz @ 4MHz OSC = SSPADD 10\n\tSSPADD = 39;\t\t\t\t\t\/\/setup i2c clk speed\n\tOpenI2C(MASTER, SLEW_OFF);\t\t\/\/Slew rate control should be off for 100KHz, on for 400KHz<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#define\tLED_DRIVER_I2C_START_IN_PROGRESS_BIT\t\tSSP1CON2bits.SEN\n#define\tLED_DRIVER_I2C_RESTART_IN_PROGRESS_BIT\tSSP1CON2bits.RSEN\n#define\tLED_DRIVER_I2C_WRITE_BYTE(a)\t\t\t\tWriteI2C1(a)\n#define\tLED_DRIVER_I2C_TX_IN_PROGRESS_BIT\t\t\tSSP1STATbits.R_W\n#define\tLED_DRIVER_I2C_ACK_NOT_RECEIVED_BIT\t\tSSP1CON2bits.ACKSTAT\n#define\tLED_DRIVER_I2C_READ_BYTE(a)\t\t\t\ta=ReadI2C1()\t\t\t\/\/&lt;&lt;&lt;&lt; UPDATE THIS FUNCTION IF USING DIFFERENT I2C PORT!!\n#define\tLED_DRIVER_I2C_ACK_IN_PROGRESS_BIT\t\tSSP1CON2bits.ACKEN\n#define\tLED_DRIVER_I2C_STOP_IN_PROGRESS_BIT\t\tSSP1CON2bits.PEN\n\n\/\/StopI2C1 - Generate bus stop condition\n#define LED_DRIVER_I2C_STOP_I2C StopI2C1()\n\n\/\/StartI2C1 - Generate bus start condition\n#define LED_DRIVER_I2C_START_I2C StartI2C1()\n\n\/\/RestartI2C1 - Generate bus restart condition\n#define LED_DRIVER_I2C_RESTART_I2C RestartI2C1()\n\n\/\/NotAckI2C1 - Generate bus Not ACK condition\n#define LED_DRIVER_I2C_NOT_ACK NotAckI2C1()\n\n\/\/AckI2C1 - Generate bus ACK condition\n#define LED_DRIVER_I2C_ACK AckI2C1()\n\n\/\/Idle I2C1 -  Test if I2C1 module is idle\n#define LED_DRIVER_I2C_IDLE_I2C IdleI2C1()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-752","post","type-post","status-publish","format-standard","hentry","category-i2c-xc8-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/752","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=752"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":1564,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/752\/revisions\/1564"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}