E4:- ? Input buffer overrun error detection; and

E4:- Embedded Systems RS232 CommunicationLIT Page 1 of 11 Paul MorrowSerial Ports in the PIC MicrocontrollerSerial data communication are useful because only one or two signal wires are needed,compared with at least eight data lines for a parallel bus plus control signals. The 8-bit PIC microcontroller mid-range and high-end (PIC18) range offers a choice of serial interfaces (RS232; SPI; I2C; USB ; Ethernet; Canbus) as shown below in Table 1.1.The best serial communication method for any given communication application depends on the distance between nodes, the speed, and the number of hardware connections required.1. RS232:- USARTThe universal synchronous/asynchronous receive transmit (USART) module implemented inside a PIC is used to implement RS232 communications. Is is typically used in asynchronous mode to implement off-chip, one-to-one connections. The termasynchronous means no separate clock signal is needed to time the data reception, soonly a data send, data receive, and ground wires are needed. It is quick and simple toimplement if a limited data bandwidth (i.e. slow datarate) is acceptable.A common application is connecting the PIC chip to a host PC for uploading dataacquired by the MCU subsystem as shown below in Fig. 1.1. The USART link can send data up to 100 meters by converting the signal to higher-voltage levels (typically +/- 12 V) using an RS232 driver chip (MAX232). The digital signal is inverted and shifted to become bipolar (symmetrical about 0 V, line negative when inactive) for transmission.Figure 1.1 USART OperationThe PIC USART module contains all all clock generators, shift registers and data buffers necessary to perform an input or output serial data transfer independently of the device program execution. It also inserts the necessary Start and Stop bits used as part of the RS232 protocol.E4:- Embedded Systems RS232 CommunicationLIT Page 2 of 11 Paul MorrowThe USART system integrated into the PICmicrocontroller has the following features: ? Full-duplex asynchronous transmit and receive; ? Programmable 8- or 9-bit character length; ? Address detection in 9-bit mode; ? Input buffer overrun error detection; and ? Half-duplex communication in synchronous mode (master or slave). The USART transmits and receives data using standard non-return-to-zero (NRZ) format. As seen in Fig. 1.2 below, this mode does not use a clock signal, while the data format being transferred is very simple.Figure 1.2 USART Asynchronous OperationBriefly, each data is transferred in the following way: ? In the idle state, the data line has high logic level (1); ? Each data transmission starts with START bit which is always a zero (0); ? Each data is 8- or 9-bit wide (LSB bit is first transferred); and ? Each data transmission ends with STOP bit which always has logic level which is always a one (1).E4:- Embedded Systems RS232 CommunicationLIT Page 3 of 11 Paul Morrow1.1 USART Asynchronous TransmitterIn order to enable data transmission via the USART module, it is necessary to configure it to operate as a transmitter as shown below in Fig. 1.3, by the following control bits:-TXEN = 1 – USART transmitter is enabled by setting this bit of the TXSTA register; SYNC = 0 – USART is configured to operate in asynchronous mode by clearing this bit of the TXSTA register; and SPEN = 1 – By setting this bit, Serial Port Enable, of the RCSTA register, the USART is enabled and the TX/CK pin is automatically configured as output. If this pin is simultaneously used for some analog function, it must be disabled by clearing the corresponding bit of the ANSEL register.Figure 1.3 USART Asynchronous Transmitter The central part of the USART transmitter shift register (TSR) which is not directly accessible by the user. In order to start transmission, the module must be enabled by setting the TXEN bit of the TXSTA register. Data to be sent should be written to the TXREG register, which will cause the following sequence of events: ? Byte will be immediately transferred to the shift register TSR; ? TXREG register remains empty, which is indicated by setting flag bit TXIF of the PIR1 register. ? If the TXIE bit of the PIE1 register is set, an interrupt will be generated. Besides, the flag is set regardless of whether an interrupt is enabled or not. Also, it cannot be cleared by software, but by writing new data to the TXREG register; ? Control electronics “pushes” data from the TSR register towards the TX pin in rhythm with internal clock: START bit (0) … data … STOP bit (1); ? When the last bit leaves the TSR register, the TRMT bit of the TXSTA register is automatically set; andE4:- Embedded Systems RS232 CommunicationLIT Page 4 of 11 Paul Morrow? If the TXREG register has received a new character data in the meantime, the whole procedure is repeated immediately after the STOP bit of the previous character has been transmitted. Fig. 1.4 below shows the relevant waveforms inside the USART when a single serial transmission is sent from the PIC. The TRMT bit returns high straight away when the stop bit has been transmitted which indicates that the TSR is empty.Figure 1.4 USART Asynchronous Master Transmission Figure 1.5 below shows the relevant waveforms inside the USART when a back-to-back serial transmission is sent from the PIC. Note that the TXIF flag does not return high until the contents of the TXREG are transferred to the TSR register. Figure 1.5 USART Asynchronous Master Transmission Back-to-BackWhen setting up an asynchronous transmission, follow these steps:1. Initialize the SPBRG register for the appropriate baud rate. If a high-speed baud rate is desired, set bit BRGH. (see section on Baud Rate Generation)2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.3. If interrupts are desired, then set enable bit TXIE.4. Enable the transmission by setting bit TXEN which will also set bit TXIF.5. Load data to the TXREG register (starts transmission).6. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are set.E4:- Embedded Systems RS232 CommunicationLIT Page 5 of 11 Paul Morrow1.2 USART Asynchronous ReceiverSimilar to the activation of USART transmitter, in order to enable the receiver it is necessary to define the following bits, as shown in Fig. 1.6:CREN = 1 – USART (continuous ) receiver is enabled by setting this bit of the RCSTA register; SYNC = 0 – USART is configured to operate in asynchronous mode by clearing this bit stored in the TXSTA register; and SPEN = 1 – By setting this bit of the RCSTA register, USART is enabled and the RX/DT pin is automatically configured as input. If this bit is simultaneously used for some analog function, it must be disabled by clearing the corresponding bit of the ANSEL register.Figure 1.6 USART Asynchronous ReceiverOn receiving an ASCII character from an external device, firstly the START bit is detected, data is then transferred to the shift register RSR through the RX pin. When the STOP bit has been received, the following occurs: ? Data is automatically transferred to the RCREG register (if empty); ? The flag bit RCIF is set and an interrupt, if enabled by the RCIE bit of the PIE1 register, occurs. Similar to transmitter, the RCIF flag bit is cleared by software only, i.e. by reading the RCREG register. The RCREG is a double buffered FIFO , or two deep character FIFO memory. (first-in, first-out) which allows reception of two characters simultaneously and a third byte to begin shifting to the FSR register; ? If the RCREG register is full (contains two bytes) and the shift register RSR detects a new STOP bit, the overflow bit OERR (overrun error bit) will be set. In this case, data in the RSR data is lost, and the OEER bit must be cleared in software. It is done by clearing and resetting the CREN bit. Note: it is not possible to receive new data if the OERR bit is set;E4:- Embedded Systems RS232 CommunicationLIT Page 6 of 11 Paul MorrowFig. 1.7 below shows the relevant waveforms inside the UART when three serial bytes are received by the PIC. Flag bit RCIF is a read-only bit, which is cleared when the RCREG register has been read and is empty.Figure 1.7 USART Asynchronous ReceptionWhen setting up an asynchronous reception, follow these steps:1. Initialize the SPBRG register for the appropriate baud rate. If a high-speed baud rate is desired, set bit BRGH. (see section on Baud Rate Generation)2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.3. If interrupts are desired, then set enable bit RCIE.4. Enable reception by setting bit CREN.5. Flag bit RCIF will be set when reception is complete and an interrupt will be generated if enable bit RCIE is set.6. Read the 8-bit received data by reading the RCREG register.7. If any error occurred, clear the error by clearing enable bit CREN.8. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are set.E4:- Embedded Systems RS232 CommunicationLIT Page 7 of 11 Paul Morrow1.3 Baud Rate GeneratorAs shown in Fig 1.3 and Fig. 1.6, the PIC has a programmable 8-bit Baud Rate Generator which is used to set the clock rate for Transmitting and Receiving data at a certain baud rate. The SPBRG register controls the period of a free running 8-bit timer as shown in Fig. 1.8 below.Figure 1.8 USART SPRG registerBesides, the BRGH bit of the TXSTA register alos controls the Baud Rate. In synchronous mode, the BRGH bit is ignored. Given the desired Baud Rate and Fosc, the nearestinteger value for the SPBRG register (i.e. X) can be calculated using the formula in Table 1.1.The formulas used to determine the Baud Rate are shown in Table 1.1:Table 1.1 USART Baud Rate Generator (BRG)The desired above formula can be re-worked (when SYNC = 0 and BRGH = 0) to work out the correct value in the SPBRG register to calculate a desired Baud Rate:-An example of calculated Baud Rates are shown below for asynchonrous mode (BRGH=0) are shown in Table 1.2:-Table 1.2 Baud Rates when FOSC=20MHz in Aysnchronous modeE4:- Embedded Systems RS232 CommunicationLIT Page 8 of 11 Paul MorrowExample Program with Serial Communication:-/******************************************************************************* ** File: LAB_1_RS2332.c ** Project: LAB_1_RS2332 ** Description: This program uses the RS232 (EB015-00) Board connected to ** PORTB to communicate with the Hyperterminal. The LED Board is ** connected to PORTA where a value entered by the user 0-9 is ** used to turn on the associated LEDS on PORTA as a BCD ** representation. ** ********************************************************************************* ** Created By: Paul Morrow 01/09/15 ** ** Versions: 1.0 ** *******************************************************************************/#include // Include HiTech C header file for PIC#include // used for printf statement#include // used for atoi function#include “usart.h” // used for usart functions and setup/* High Speed Crystal Oscillator, Watchdog Timer off, Power-Up Timer Off, MCLRoff, Brown-Out Reset Off, Low Voltage Programming Off, Code Protection Off,Write Protection Off, In-circuit Debugger Disabled, CCP1 Function on RB0,Code Protection Off*/__CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & LVP_OFF& CPD_OFF & WRT_OFF & DEBUG_OFF & CCPMX_RB0 & CP_OFF);#define _XTAL_FREQ 19660800main(){unsigned char input, count, i, result;ANSEL = 0; // Turn off all Analog I/OTRISA = 0x00; // Setup PORTA as outputPORTA = 0x00; // Clear PORTAinit_comms(); // Setup RS232 comms//int atoi( const char * s );char num10;char text1 = {72,69,76,76,79,’’};char text2 = {‘H’,’E’,’L’,’L’,’0′,’’};char text3 = {‘0′,’2′,’3′,’4′,’5′,’’};while(1) // Loop forever{for( i = 0 ; i <= 3 ; i++){printf("Type a number from 0-9 to turn on LEDs ");input = getch(); // Get character from RCREGPORTA = input - 0x30; // Convert ASCII to Numberprintf ("I detected %c ", input);}E4:- Embedded Systems RS232 CommunicationLIT Page 9 of 11 Paul Morrowfor( i = 0 ; i <= 3 ; i++){/* Pin RA5 on the PIC16F88, can only be configured as an*//* input. Also, pin RA5 for U18 pin chips are not wired *//* to PORTA on the EB-006. Try holding the Reset button *//* while enter values from 0-255 to see the effect. *//* Also, RA6 and RA7 are used for the oscillator pins */printf("Type a number from 0-255 to turn on LEDs ");getstr (num, 3);printf("num0 = %c ", num0);printf("num1 = %c ", num1);printf("num2 = %c ", num2);result = atoi(num);printf("Result = %d ", result);PORTA = result;printf("PORTA = %d ", PORTA);}putstr(text1); // print a string, or array of charsprintf(" ");putstr(text2);printf(" ");putstr(text3);printf(" ");for (count = 0x31; count <=0x34; count++){putch('#');putch(count); // print numbers 1 - 4putch(0x0A); // 0x0A equals New Lineputch(0x0D); // 0x0D equals Carriage Returninput = getch();putch(input);putch(0x0A);putch(0x0D);}for( i = 0 ; i <= 3 ; i++){printf("Press a key and I will echo it back ");input = getch();printf("I detected %c ", input);printf("ASCII Value = %d ", input);}__delay_ms(100);}}E4:- Embedded Systems RS232 CommunicationLIT Page 10 of 11 Paul Morrow***********************************usart.h**************************/* USART Settings Header *//* Desired Baud Rate = 9600 */#define BAUD 9600/* Oscillator Frequency = 19.6608MHz (L = Long Integer) */#define FOSC 19660800L/* (SPRG Register) <= baudsetting = {19660800/(64*9600)} - 1 = 31 */#define baudsetting ((int)(FOSC/(64UL * BAUD) - 1))#define RX_PIN TRISB2#define TX_PIN TRISB5void init_comms (void);void putch (unsigned char);void putstr (unsigned char *text);unsigned char getch (void);void getstr (char *output, unsigned char length);***********************************usart.c**************************#include // Include HiTech C header file for PIC#include // Include stdio library header#include “usart.h” // Include usart settings header/* Function to Initialise to setup to USART */void init_comms(void){RX_PIN = 1; // Set RX pin as an input pinTX_PIN = 0; // Set TX pin as an output pinSPBRG = baudsetting; // Set Baud Rate as calculated in usart.hRCSTA = 0x90; // Serial Port Enabled ; Continuous Receive modeTXSTA = 0x20; // TXEN Enabled ; Asynchronouse mode}E4:- Embedded Systems RS232 CommunicationLIT Page 11 of 11 Paul Morrow/* Function to send one Character to the output pin TX */void putch (unsigned char byte){/* Output One byte */while (!TXIF); // Set when register is empty// (i.e. wait until it is set)TXREG = byte; // tranfer byte TXREG register}/* Function to send a string of characters to the output pin TX */void putstr (unsigned char *text){/* Output a String */int i;for ( i = 0 ; texti != ‘’; i++){putch( texti);}}/* Function to get a character from the input pin RX */unsigned char getch (void){/* Retreive One Character */while (!RCIF); // Set when register contains data// (i.e. ait until it is set)return RCREG; // transfer byte from RCREG Register}/* Function to get a string from the input pin RX */void getstr (char *output, unsigned char length){/* Retreive One String */int i;for( i = 0 ; i < length ; i++){outputi = getch();}}

x

Hi!
I'm James!

Would you like to get a custom essay? How about receiving a customized one?

Check it out