{"id":764,"date":"2017-06-28T10:05:39","date_gmt":"2017-06-28T10:05:39","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=764"},"modified":"2024-06-24T11:50:21","modified_gmt":"2024-06-24T10:50:21","slug":"spi","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic18\/xc8-compiler\/spi-xc8-compiler\/spi","title":{"rendered":"SPI"},"content":{"rendered":"<h4>\nMaster SPI Port<br \/>\n<\/h4>\n<h5>\nInitialise<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n#define INTERBOARD_COMMS_SS_PIN\t\t\t\tLATDbits.LATD3\r\n#define INTERBOARD_COMMS_PACKET_LENGTH\t\t10\r\n\r\n\t\/\/----- SETUP SSP2 AS SPI BUS -----\r\n\t\/\/Used for: \r\n\t\/\/CLK idle in low state\r\n\t\/\/Devices clock in data on the rising edge, output data on the falling edge\r\n\tSSP2STAT = 0b11000000;\r\n\tSSP2CON1 = 0b00100010;\t\t\/\/16MHz Fosc \/ 64 = 250000kHz\r\n\r\n\t\/\/----- INIT SPI -----\r\n\t\/\/Send a dummy byte to trigger the BF bit\r\n\tSSP2BUF = 0;\r\n\tINTERBOARD_COMMS_SS_PIN = 1;\r\n<\/code><\/pre>\n<h5>\nTX and Receive<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\t\/\/Exit if TX in progress\r\n\t\tif (!SSP2STATbits.BF)\r\n\t\t\treturn;\r\n\r\n\t\t\/\/Exit if pausing\r\n\t\tif (interboard_comms_10ms_timer &gt; 0)\r\n\t\t\treturn;\r\n\r\n\t\tif (spi_comms_byte_index == 0xff)\r\n\t\t{\r\n\t\t\t\/\/DONE COMPLETE PACKET\r\n\t\t\tNop();\t\t\/\/Ensure a delay after clock of last byte\r\n\t\t\tNop();\r\n\t\t\tNop();\r\n\t\t\tNop();\r\n\t\t\tNop();\r\n\t\t\tINTERBOARD_COMMS_SS_PIN = 1;\r\n\r\n\t\t\tspi_comms_byte_index = 0;\r\n\t\t\tinterboard_comms_10ms_timer = 2;\t\t\/\/Wait 10-20ms before next comms\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\r\n\t\tif (spi_comms_byte_index == 0)\r\n\t\t{\r\n\t\t\t\/\/----- BYTE 0 -----\r\n\t\t\tINTERBOARD_COMMS_SS_PIN = 0;\r\n\r\n\t\t\tspi_tx_rx_data[0] = 0x10;\r\n\t\t\tspi_tx_rx_data[1] = 0x11;\r\n\t\t\tspi_tx_rx_data[2] = 0x12;\r\n\t\t\tspi_tx_rx_data[3] = 0x13;\r\n\t\t\tspi_tx_rx_data[4] = 0x14;\r\n\t\t\tspi_tx_rx_data[5] = 0x15;\r\n\t\t\tspi_tx_rx_data[6] = 0x16;\r\n\t\t\tspi_tx_rx_data[7] = 0x17;\r\n\t\t\tspi_tx_rx_data[8] = 0x18;\r\n\t\t\tspi_tx_rx_data[9] = 0x19;\r\n\t\t}\r\n\r\n\t\tdata = SSP2BUF;\r\n\t\tSSP2BUF = spi_tx_rx_data[spi_comms_byte_index];\r\n\t\tspi_tx_rx_data[spi_comms_byte_index] = data;\r\n\r\n\r\n\t\tspi_comms_byte_index++;\r\n\t\tif (spi_comms_byte_index &gt;= INTERBOARD_COMMS_PACKET_LENGTH)\r\n\t\t\tspi_comms_byte_index = 0xff;\t\t\/\/Flag packet all done\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h4>\nSlave SPI Port<br \/>\n<\/h4>\n<h5>\nInitialise<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP SSP2 AS SPI BUS -----\r\n\t\/\/SLAVE MODE\r\n\tTRISDbits.TRISD0 = 1;\r\n\tTRISDbits.TRISD3 = 1;\r\n\tSSP2STAT = 0b01000000;\r\n\tSSP2CON1 = 0b00100100;\t\t\/\/SPI slave mode, SS is used\r\n\r\n\tPIE3bits.SSP2IE = 1;\t\t\/\/Enable interrupt\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master SPI Port Initialise #define INTERBOARD_COMMS_SS_PIN LATDbits.LATD3 #define INTERBOARD_COMMS_PACKET_LENGTH 10 \/\/&#8212;&#8211; SETUP SSP2 AS SPI BUS &#8212;&#8211; \/\/Used for: \/\/CLK idle in low state \/\/Devices clock in data on the rising edge, output data on the falling edge SSP2STAT = 0b11000000; SSP2CON1 = 0b00100010; \/\/16MHz Fosc \/ 64 = 250000kHz \/\/&#8212;&#8211; INIT SPI &#8212;&#8211; \/\/Send [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-spi-xc8-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/764","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=764"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/764\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}