{"id":1430,"date":"2023-09-15T13:25:18","date_gmt":"2023-09-15T12:25:18","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=1430"},"modified":"2023-09-21T11:04:07","modified_gmt":"2023-09-21T10:04:07","slug":"uart-interrupt-driven","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic32\/xc32\/uart-xc32-pic32\/uart-interrupt-driven","title":{"rendered":"UART (interrupt driven)"},"content":{"rendered":"\n<p>Configure the UART in MCC<\/p>\n\n\n\n<p>Once you Geneate the MCC code it will have dealt with initialising the UART 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\nvolatile uint8_t Uart3RxBuffer&#91;1];\n\n\n\/\/IN YOUR INITIALSE CODE\n\t\/\/----- SETUP USART 3 -----\n\t\/\/Used for: \n\t\/\/Config done in MCC:\n\t\/\/\tSetup IO pin assignments:\n\t\/\/\t\tPPS Input Pin Configuration > Use PPS Input# > U3RX, > RPF0\n\t\/\/\t\tPPS Output Pin Configuration > Use PPS Output# > U3TX > RPF1\n\t\/\/\tSetup UART:\n\t\/\/\t\tOperating mode: None blocking mode\n\t\/\/\t\t115200-8-N-1\n\t\/\/\tSetup interrupts:\n\t\/\/\t\tEnable UART1 Fault Interrupt = Will already be set to Yes\n\t\/\/\t\tEnable UART1 Receive Done Interrupt = Will already be set to Yes\n\t\/\/\t\tEnable UART1 Transmit Done 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\n\t\/\/UART3_WriteCallbackRegister(UART3_MyTxEventHandler, (uintptr_t)NULL);\t\t\/\/Register our callback function to be called when TX is complete (optional)\n\tUART3_ReadCallbackRegister(UART3_MyRxEventHandler, (uintptr_t)NULL);\t\t\/\/Register our callback function to be called when data is received or a RX error occurs\n\n\t\/\/Start receiving\n\tUART3_Read(&amp;Uart3RxBuffer&#91;0], 1);\t\t\t\/\/You can setup to receive multiple bytes, but we want an IRQ callback to us for every byte received\n\n\t\/\/TX some bytes\n\tuint8_t TxBuffer&#91;] = {0x30, 0x39};\n\tUART3_Write(&amp;TxBuffer&#91;0], sizeof(TxBuffer));\n\n\/\/YOUR FUNCTION TO BE CALLED WHEN TX IS COMPLETE (if wanted)\n\/\/*******************************************\n\/\/*******************************************\n\/\/********** UART3 TX COMPLETE IRQ **********\n\/\/*******************************************\n\/\/*******************************************\n\/*\nvoid UART3_MyTxEventHandler(uintptr_t context)\n{\n\t\/\/TX HAS COMPLETED\n\t\n}\n*\/\n\n\/\/YOUR FUNCTION TO BE CALLED WHEN RX IS COMPLETE\n\/\/**********************************\n\/\/**********************************\n\/\/********** UART3 RX IRQ **********\n\/\/**********************************\n\/\/**********************************\nvoid UART3_MyRxEventHandler(uintptr_t context)\n{\n\tuint8_t Data;\n\t\n\tData = Uart3RxBuffer&#91;0];\n\t\n\tif (UART3_ErrorGet() != UART_ERROR_NONE)\n\t{\n\t\t\/\/A RX ERROR HAS OCCURED\n\t\t\/\/(It has been cleared already)\n\t\t\n\t\t\/\/UART_ERROR Values: UART_ERROR_NONE, UART_ERROR_OVERRUN, UART_ERROR_FRAMING, UART_ERROR_PARITY\n\t\t\n\t\t\/\/Start receiving again ready for next byte\n\t\tUART3_Read(&amp;Uart3RxBuffer&#91;0], 1);\n\t}\n\telse\n\t{\n\t\t\/\/WE HAVE A VALID RX \n\t\t\n\t\t\/\/Start receiving again ready for next byte\n\t\tUART3_Read(&amp;Uart3RxBuffer&#91;0], 1);\n\t\t\n\t\t\t\t\n\t\t\/\/>>>>> Test code - return the byte + 1\n\t\t\/\/U3TXREG = Data + 1;\n\t\t\/\/&lt;&lt;&lt;&lt;&lt;\n\t\t\n\t}\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\/uart\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Configure the UART in MCC Once you Geneate the MCC code it will have dealt with initialising the UART 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\/uart\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-1430","post","type-post","status-publish","format-standard","hentry","category-uart-xc32-pic32"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1430","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=1430"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1430\/revisions"}],"predecessor-version":[{"id":1474,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1430\/revisions\/1474"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=1430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=1430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=1430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}