Tuesday, January 15, 2013

UART Revisited - Transmitter and Receiver

Hello Everyone,

"A Universal Asynchronous Receiver/Transmitter, abbreviated UART, is a piece of computer hardware that translates data between parallel and serial forms. UARTs are commonly used in conjunction with communication standards such as EIA, RS-232, RS-422 or RS-485. The universal designation indicates that the data format and transmission speeds are configurable. The electric signaling levels and methods (such as differential signaling etc.) are handled by a driver circuit external to the UART." - Courtesy Wikipedia
Today we are going to revisit the UART Tx. and Rx. in a different configuration and different PIC Microcontroller. Our previous post: Click Here also covers a similar post on UART. But we have used PIC16F628A Microcontroller. The protocol used in the project is RS485. The IC used for the project to follow the RS485 is MAX485

In this project we have used PIC16F688 Microcontroller and have made it very simple to understand the UART operations, transmission and reception. A feature available in PIC16F688 is the Auto-Baud Rate which we have not used in this project. The baud rate used here is 1200 bauds with Fosc = 4 MHz.

Desired Baud Rate = FOSC/ [64([SPBRGH:SPBRG] + 1)]


Using the above formula the Hex value to be loaded in the SPBRG and SPBRGH can be calculated.

The procedure for the Asynchronous Transmission Set-up:

1. Initialize the SPBRGH & SPBRG register pair, BRGH and BRG16 bits to achieve the desired baud rate
2. Enable the asynchronous serial port by clearing the SYNC bit and setting the SPEN bit.
3. If 9-bit transmission is desired, set the TX9 control bit. A set ninth data bit will indicate that the 8 Least
    Significant data bits are an address when the receiver is set for address detection.
4. Enable the transmission by setting the TXEN control bit. This will cause the TXIF interrupt bit to be set.
5. If interrupts are desired, set the TXIE interrupt enable bit. An interrupt will occur immediately provided
    that the GIE and PEIE bits of the INTCON register are also set.
6. If 9-bit transmission is selected, the ninth bit should be loaded into the TX9D data bit.
7. Load 8-bit data into the TXREG register. This will start the transmission.

The procedure for the Asynchronous Reception Set-up:
1. Initialize the SPBRGH & SPBRG register pair, BRGH and BRG16 bits to achieve the desired baud rate
2. Enable the serial port by setting the SPEN bit. The SYNC bit must be clear for asynchronous operation.
3. If interrupts are desired, set the RCIE interrupt enable bit and set the GIE and PEIE bits of the INTCON
    register.
4. If 9-bit reception is desired, set the RX9 bit.
5. Enable reception by setting the CREN bit.
6. The RCIF interrupt flag bit will be set when a character is transferred from the RSR to the receive buffer.
    An interrupt will be generated if the RCIE interrupt enable bit was also set.
7. Read the RCSTA register to get the error flags and, if 9-bit data reception is enabled, the ninth data bit.
8. Get the received 8 Least Significant data bits from the receive buffer by reading the RCREG register.
9. If an overrun occurred, clear the OERR flag by clearing the CREN receiver enable bit.


A simple diagram for the UART Demonstration is provided below:



We are also providing the sample codes for the UART Tx. and Rx.:

Asynchronous Transmission program:-

           LIST P=16F688                 ;PIC16F688  
          #include <P16f688.inc>       ;Include header file  
           ERRORLEVEL -302          ;Remove message about using proper bank  
           __CONFIG _XT_OSC & _CP_OFF & _WDT_OFF & _PWRTE_ON & _CPD_OFF & _MCLRE_ON & _BOREN_OFF  
 dataL     equ          20H  
 data1     equ          21H  
 temp1     equ          2AH  
 a1        equ          28h  
 a1_1      equ          29h       
           ORG  0x000        ; Program starts at 0x000  
   
 ; --------------------------------  
 ; SET ANALOG/DIGITAL INPUTS PORT A  
 ; --------------------------------  
 
          bcf     STATUS,RP0     ; Select BANK 0  
          movlw   7  
          movwf   CMCON0         ; CMCON0=7 set comperators off to select the Digital Lines  
 ; ----------------  
 ; INITIALIZE PORTS  
 ; ----------------  
          bsf      STATUS,RP0     ; Select BANK 1  
          movlw    b'00000000'  
          movwf    TRISA          ; Initialise PORT A as Output  
          movlw    b'11101111'    ; Initialise PORT C RC0,RC1,RC2,RC3 as Input. RC4 as TX and RC5 as RX  
          movwf    TRISC  
          movlw    b'00000000'    ; Initialise PORT C as Digital Output  
          movwf    ANSEL  
 ; ------------------------------------  
 ; SET BAUD RATE TO COMMUNICATE WITH PC  
 ; ------------------------------------  
 ; Boot Baud Rate = 1200  
          bcf      STATUS,RP0      ; Select BANK 0  
          movlw    0x33            ; (0x33 h = 51 d = 1200 Baud Rate)  
          movwf    SPBRG  
          movlw    0x00            ; (0x00 h = 00 d = 1200 Baud Rate)  
          movwf    SPBRGH   
          movlw    0x00         
          movwf    BAUDCTL  
          movlw    b'00100000'      
          movwf    TXSTA           ; Enable Async Transmission  
          movlw    b'10010000'     ; Enable Async Reception  
          movwf    RCSTA  
 ; ------------------------------------  
 ; PROVIDE A SETTLING TIME FOR START UP  
 ; ------------------------------------  
          clrf     dataL  
 settle   decfsz   dataL,F         ; Set certain delay  
          goto     settle  
          movf     RCREG,W  
          movf     RCREG,W  
          movf     RCREG,W        
 ; ---------  
 ; MAIN LOOP  
 ; ---------  
 txhere    movf     PORTC,0        ; Read the PORTC  
           movwf    temp1            
           bcf      temp1,4  
           bcf      temp1,5  
           bcf      temp1,6  
           bcf      temp1,7  
           call      send          ; Send the Byte  
           call      Delay30m  
           goto      txhere  
 ; -------------------------------------------------------------  
 ; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING  
 ; -------------------------------------------------------------  
 send       clrf      data1  
 delay1     decfsz    data1,F      ; Set certain delay  
            goto      delay1  
            bcf       STATUS,RP0   
            movf      temp1,0  
            movwf     TXREG        ; Send data in W  
            bcf       STATUS,RP0        
 WtHere     btfss     TXSTA,TRMT   ; Check if the Transmission is done  
            goto      WtHere  
            return  
 Delay30m                          ; 30 Millisecond Delay  
            movlw     0x6E  
            movwf     a1  
            movlw     0x18  
            movwf     a1_1  
 Delay30m_0  
            decfsz     a1, f  
            goto       $+2  
            decfsz     a1_1, f  
            goto       Delay30m_0  
            goto       $+1  
            nop  
            return  
            END  


Asynchronous Reception Program:-

           LIST P=16F688                 ;PIC16F688  
          #include <P16f688.inc>       ;Include header file  
           ERRORLEVEL -302                ;Remove message about using proper bank  
           __CONFIG _XT_OSC & _CP_OFF & _WDT_OFF & _PWRTE_ON & _CPD_OFF & _MCLRE_ON & _BOREN_OFF   
 dataL     equ          20H  
 data1     equ          21H  
 data2     equ          22H  
 temp1     equ          2AH  
 a1        equ          28h  
 a1_1      equ          29h       
          ORG  0x000      ; Program starts at 0x000  
 
 ; --------------------------------  
 ; SET ANALOG/DIGITAL INPUTS PORT A  
 ; --------------------------------  

                bcf      STATUS,RP0     ; Select BANK 0  
                movlw    7  
                movwf    CMCON0         ; CMCON0=7 set comperators off to select the Digital Lines  
 ; ----------------  
 ; INITIALIZE PORTS  
 ; ----------------  
               bsf       STATUS,RP0     ; Select BANK 1  
               movlw     b'00000000'  
               movwf     TRISA          ; Initialise PORT A as Output  
               movlw     b'11100000'    ; Initialise PORT C RC0,RC1,RC2,RC3 as Output. RC4 as TX and RC5 as RX  
               movwf     TRISC  
               movlw     b'00000000'    ; Initialise PORT C as Digital Output  
               movwf     ANSEL  
 ; ------------------------------------  
 ; SET BAUD RATE TO COMMUNICATE WITH PC  
 ; ------------------------------------  
 ; Boot Baud Rate = 1200  
               bcf      STATUS,RP0     ; Select BANK 0  
               movlw    0x33           ; (0x33 h = 51 d = 1200 Baud Rate)  
               movwf    SPBRG  
               movlw    0x00           ; (0x00 h = 00 d = 1200 Baud Rate)  
               movwf    SPBRGH  
               movlw    0x00          
               movwf    BAUDCTL  
               movlw    b'00100000'      
               movwf    TXSTA          ; Enable Async Transmission  
               movlw    b'10010000'    ; Enable Async Reception  
               movwf    RCSTA  
 ; ------------------------------------  
 ; PROVIDE A SETTLING TIME FOR START UP  
 ; ------------------------------------  
              clrf     dataL  
 settle       decfsz   dataL,F         ; Set certain delay  
              goto     settle  
              movf     RCREG,W  
              movf     RCREG,W  
              movf     RCREG,W        
 ; ---------  
 ; MAIN LOOP  
 ; ---------  
 loop         call     receive           ; Wait for Reception of Byte  
              movwf    data2  
              bcf      STATUS,RP0  
              movwf    PORTC             ; Output the received byte on PORT C  
              goto     loop  

 ; -------------------------------------------  
 ; RECEIVE CHARACTER FROM RS232 AND STORE IN W  
 ; -------------------------------------------  
 ; This routine does not return until a character is received.  

 receive      btfss     PIR1,RCIF       ; (5) check for received data  
              goto      receive  
              movf      RCREG,W         ; Save received data in W  
              return  
              END  

 UART Rx. ASM File: Click Here (ASM)
UART Rx. HEX File: Click Here (HEX)
UART Tx. ASM File: Click Here (ASM)
UART Tx. HEX File: Click Here (HEX)

Well we actually did not do this mini project for the sake of RnD as it was just a revisit. Some students from Jyothi Nivas College wanted to learn and do this project for a learning curve. Here are some of the pictures of the student who made the receiver and the transmitter.

Simple UART Transmitter

Simple UART Receiver

The UART Rx. and Tx. in simple wire connection using RS485 Protocol

Students who made the UART Receiver

Students who made the UART Transmitter






   

No comments:

Post a Comment