The microcontroller USART peripheral requires setting up so that data can be transmit and received. The USART needs to be activated so that the associated pins are used for communications (and not as general purpose Input/Output). The data format and speed (baudrate) also need configuring to be the same as the PC. All the setup information can be found in the PIC datasheet. UART Registers The PIC 16F627 has 3 registers that require setting up for communications
In our example we are going to transmit and receive at 9600 baud. Our data will have 8 data bits and each byte will have a start and one stop bit. Baudrate (SPBRG) The baudrate is dependant on the microcontroller clock speed, as we are using the internal oscillator in our example this will be 4MHz. There are two formulas for calculating the baudrate depending on whether the High Baud Rate bit is selected (BRGH).
As the baudrate needs to be within 5% (max), then high speed (BRGH = 1) must be used. Transmit Register (TXSTA) The Transmit Register requires setting to 00100100 (binary) which is 0x24 (hex)
Receiving Register (RCSTA) The Recieve Register requires setting to 10010000 (binary) which is 0x90 (hex)
Initialisation Code As well as the UART registers there is one other set up that is required. The PORT Pins must be set up as inputs for the pins to be associated with the UART peripheral. The pins associated with the UART are on PORT B, RB1 (RX) and RB2 (TX). The following code extract configures the UART.
|