Reading and Writing data to USART in MPLAB harmony

How to read and write data to USART in MPLAB Harmony

Reading data and Writing data to USART is one of the basic task you want to do when you work with microcontroller. Most electronics or electrical engineering colleges doesn’t teach these sorts of things off-course. If you were to apply for electronics and computer engineering technology jobs you should know how to handle USART of a microcontroller. So considering PIC32 microcontroller and that you are using MPLAB Harmony, here are some tips with codes to read data and write data to PIC32 USART.

The MPLAB Harmony USART driver supports 3 kinds of data transfer models- Byte transfer, File I/O or block byte transfer and Buffer transfer. Each of these have different client API or functions for data transfer, that is, to read and write. Each type of data transfer have merits and demerits and they should be used according to application requirement. For example a simple USART read and write where small number of bytes have to transmitted you can use the Byte Transfer model. But if you need transfer huge data then Buffer Transfer model is more suited.

Reading and Writing data to USART in MPLAB harmony using Byte-by-Byte Transfer method

For example in byte transfer model, the DRV_USART_ByteWrite() and DRV_USART_ByteRead() functions are used for write and read respectively.

Writing to USART

Suppose you want to transfer data such as “Electronics Engineering Stuffs”. Following is a simple example of writing data to the USART.

Here, you have put the data that you want to transmit in char pointer buffer. Then using for loop you will transmit the data via USART using DRV_USART_ByteWrite() function. Before transmission of the data, first can utilize the DRV_USART_TransmitBufferIsFull() function to check whether the transmit buffer is full or not. If it is full it will return true and you don’t want to send data at this point. So using if statement you break out of this writeUSART() function. Otherwise using else statement you can transmit the data.

Now you can use this function inside one suitable state of your MPLAB Harmony application state machine where you want to send the data. For example if you have a state called USARTTransmit then you could use the above writeUSART() function in the following way.

Remember that this is just an example.

Reading from USART

Consider that you want to read the incoming data in USART. The following example code illustrates how you can read data from USART using the Byte transfer model.

This function readUSART() uses the DRV_USART_ByteRead() function to read 1 byte at a time, 10 bytes of data using the for loop. The data gets stored in the mybuffer array. The URV_USART_ReceiveBufferIsEmpty() is used to check whether the receive buffer is empty or not. If it is empty then it should read the USART receive buffer using the DRV_USART_ByteRead() function. Otherwise it should exit the readUSART() function.

So in this way you can read and write data to USART using MPLAB harmony.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

arrow -> in C programming language

How to make microcontroller pin low or high properly?

How to make microcontroller pin low or high properly?