Tuesday, 3 December 2013

PIC16F628A UART code + Proteus simulation


This post answers the question, "How to use interrupt based UART in PIC16F628A" ?

Also, using PIC16 simulator (Proteus) you can verify this UART code and change it according to your needs. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the 'Downloadssection at the bottom of this page.


This PIC16F628A microcontroller tutorial provides the interrupt based UART functions which you can easily understand and use. After reading this page you can easily use PIC16F628A serial UART interface.



It is assumed that you know how to blink an LED with PIC16F628A microcontroller. If you don't then please read this page first, before proceeding with this article.


The following diagram (made in Proteus) shows the PIC microcontroller circuit diagram.



Figure 1.   PIC16F628A UART circuit

In the above figure, UART baud rate is currently set to 9600 bps, but you can change it to your desired value. RB2 pin is the TX pin and RB1 pin is the RX pin the UART. Whenever a new character is received on the UART, then PIC16F628A goes into interrupt service routine and received character is echoed back. 



When code starts to execute then PIC16F628A sends 'Hello World' to virtual terminal. After that PIC16F628A echos back whatever character is sent to it. 'Hi' was typed in the virtual terminal after the start up of the simulation in Proteus and PIC16F628A echoed it back.

Code

The code used to set different properties of UART is shown below. (From UART.h file)


Figure 2.   UART settings in the code


In the above figure, _XTAL_FREQ is defined to be 20000000, which is the external crystal frequency used with this PIC16F628A. If you change the crystal (e-g by attaching an external crystal of different frequency) then you will need to change _XTAL_FREQ value as well. For example, if 12 MHz external crystal is used, then _XTAL_FREQ should be defined as 12000000 in the above code.


Similarly, you can define Baudrate for the UART, in the above figure it is defined to be 9600 bps. You can change this value if you need. All other settings are at default, which means this UART will use one starting bit, 8 data bits and then one stop bit. There is no parity bit and no flow control mechanism.

The code for main function is shown below.




Figure 3.   Main code for UART of PIC16F628A 

In the main function, firstly UART is initialized using InitUART() function. After that, using SendStringSerially() function 'Hello World!' is sent on UART. After that, global and peripheral interrupts are enabled, so that when a character is received on UART then an interrupt should be generated. In the end, in the while(1) loop, code executes NULL statements.


The code for InitUART() function is shown below.



Figure 4.   InitUART function code



From the above figure, it is clear that InitUART() function initializes UART TX and RX pin direction first. Also, baud rate is set in SPBRG register according to the value of external crystal and BAUDRATE. After that, various other register settings of UART are being programmed with correct values to start UART module.


The code for the UART byte transmit and receive functions is shown below.





                                         Figure 5.   UART byte transfer and receive function code
By using SendByteSerially() function, you can send a single byte on UART. For example, SendByteSerially('H'); statement will send character 'H' on UART. Similarly, ReceiveByteSerially() function will receive a byte from UART and return its value. You can call these function in your main to transmit or receive bytes on UART.



Interrupt service routine function for UART reception is shown below.



Figure 6.   ISR code for UART reception


In the ISR code shown above, the character received from UART RX pin is stored in the RCREG register and this character is echoed back using SendByteSerially() function. Whenever a character is sent to PIC16F628A, then this interrupt ISR() function is executed automatically and in this function received character is echoed back.

You can leave your comments and suggestions in the comment section below.

Further Reading Suggestions

No comments:

Post a Comment