• Latest Posts

    Subscribe my Youtube channel for mere videos

    Friday, 24 February 2017

    Serial Port (USART) - PIC18F2420/2520/4420/4520

    Universal Synchronous Asynchronous Receiver Transmitter (USART)




    USART module is a serially data transmitting and receiving hardware or software implemented protocol. USART also known as a serial communication interface (SCI). We can communicate two devices such as computers (Please note that a driver is required to interface to RS-232 voltage levels and the PIC MCU should not be directly connected to RS-232 signals.), peripheral devices and can send/receive data thru USART module. Basically USART module can be found on all of microcontroller and microprocessor devices.


    USART send/receive protocol and hardware structure on PIC controller.

    PIC controllers provided USART can be configured as a full-duplex (Both transmission and reception can occur at the same time) asynchronous system that can communicate with peripheral devices, such as CRT terminals and personal computers. It can also be configured as a halfduplex, synchronous system that can communicate with peripheral devices, such as A/D or D/A integrated circuits, serial EEPROMs, etc.

    In the hardware circuit 2 or 3 wires would be use to communicate 2 devices each other. First is TX (Transmit) line, second is RX(Receive) and third is GND( if both devices have different supply sources when the ground should be common for both devices).

    In the data sending and receiving process, bit by bit (serial wise) data transfers or receives. Mostly microcontrollers use TTL level supply voltage, it’s means if the voltage are in range of 0 to 0.8 volt with respect to ground supply will be consider as “LOW” and voltage are in range of 2.0 to 5 volt will be consider as “HIGH”.





    PIC MCUs enhanced USART module implements additional features, including automatic baud rate detection and calibration, automatic wake-up on Sync Break reception and 12-bit Break character transmit. These make it ideally suited for use in Local Interconnect Network bus (LIN bus) systems.



    The EUSART can be configured in the following modes:

    • Asynchronous (full duplex) with:

                  -  Auto-wake-up on character reception.
                  -  Auto-baud calibration.
                  - 12-bit Break character transmission.
    • Synchronous – Master (half duplex) with selectable clock polarity
    • Synchronous – Slave (half duplex) with selectable clock polarity





    USART receive and transmit pins should be configure as input pins.


    The operation of the Enhanced USART module is controlled through these registers:
    • ·        Transmit Status and Control (TXSTA).
    • ·        Receive Status and Control (RCSTA).
    • ·        Baud Rate Control (BAUDCON)
    • ·        Transmit Register (TXREG)
    • ·        Receive Register (RCREG)
    • ·        Baud rate value (SPBRG)



    TXSTA: TRANSMIT STATUS AND CONTROL REGISTER

    Bit7   :- Clock source selection bit

    • ·        If you are using asynchronous mode, not to be use. In the synchronous mode we would configure this bit for set master or slave mode.

       TXSTAbits.CSRC = 1;           // Master mode.
       TXSTAbits.CSRC = 0;           // Slave mode.


    Bit6   :- Bits transmission mede.

                 TXSTAbits.TX9 = 1;              // 9 bit transmission.
       TXSTAbits.TX9 = 0;              // 8 bit transmission.


    Bit5   :- Transmission enable/disable bit

                 TXSTAbits.TXEN = 1; // Transmission enable.
       TXSTAbits.TXEN = 1; // Transmission disable.


    Bit4   :- Synchronous/Asynchronous mode selection

                 TXSTAbits.SYNC = 1; // Synchronous mode.
       TXSTAbits.SYNC = 0; // Asynchronous mode.


    Bit3   :- Send Break Character bit

    • ·        The EUSART module has the capability of sending the special Break character sequences that are required by the LIN bus standard. The Break character transmit consists of a Start bit, followed by twelve ‘0’ bits and a Stop bit.


               TXSTAbits.SENDB = 1;        // Send Sync Break on next transmission.
               TXSTAbits.SENDB = 0;        // Sync Break transmission completed.


    Bit2   :- High or low baud rate selection

                 TXSTAbits.BRGH = 1;          // High baud rate.
       TXSTAbits.BRGH = 0;          // Low baud rate.


    Bit1   :- Transmission status bit

                 TXSTAbits.TRMT = 1; // Transmission shift register is empty.
       TXSTAbits.TRMT = 0; // Transmission shift register is full.


    Bit0   :- 9th bit of transmit data

    • ·        If 9-bit transmission is selected, the ninth bit should be loaded in bit, TX9D.



    USART TRANSMIT BLOCK DIAGRAM



    RCSTA: RECEIVE STATUS AND CONTROL REGISTER


    Bit7   :- Serial port enable/disable bit 

                 RCSTAbits.SPEN = 1;          // Serial port enable
                 RCSTAbits.SPEN = 0;          // Serial port disable


    Bit6   :- 9-Bit Receive Enable bit

                 RCSTAbits.RX9 = 1;   // Selects 9-bit reception
                 RCSTAbits.RX9 = 8;   // Selects 8-bit reception


    Bit5   :- Not in use with asynchronous mode


    Bit4   :- Continuous Receive Enable bit

              RCSTAbits.CREN = 1;         // Enable receiving
    RCSTAbits.CREN = 0;         // Disable receiving


    Bit3   :- Address Detect Enable bit

    • ·        This bit uses when your receiving data format is 9bit, otherwise not in use.

              RCSTAbits.ADDEN = 1;       // Enable address detection
              RCSTAbits.ADDEN = 0;       // Disable address detection


    Bit2   :- Framing Error bit

    • ·        When received data format will not match with configured setting and any other unsuitable condition, this error will generate. Error can be cleared by reading RCREG register and receiving next valid byte.


    Bit1   :- Overrun Error bit

    • ·        Error can be cleared by clearing bit, CREN.



    Bit0   :- 9th Bit of Received Data

    • ·        This can be address/data bit or a parity bit.


    USART RECEIVE BLOCK DIAGRAM








    BAUDCON: BAUD RATE CONTROL REGISTER

    When you are going to use asynchronous mode serial interfacing, you would not need to configure BAUDCON register. You can leave it or set all byte as 0. If you need to know as many as, take a look on controller datasheet.


    Codes:-

    Now the time is to make a program which will send “Hello World” string when the specific character ‘A’ will received. In this example we are not using interrupt on receiving. This will just a single task program and will check RCREG (receiving register) in infinite loop.


    #include <p18f2520.h>

    void Open_USART(void);
    void USART_string( char *buffer);

    char data[] = "Hello World\r\n";
    char received_bit;

    void main()
    {
                TRISCbits.TRISC6 = 1;   // Sets transmit pin
                TRISCbits.TRISC7 = 1;   // Sets recieve pin
               
                Open_USART();

                while(1)
                {
                            received_bit = RCREG;
                            if(received_bit == 'A')
                            {
                                        USART_string(data);
                                        RCREG = 0;
                            }
                }          
    }


    void Open_USART(void)
    {
                // Settings of resisters according to PIC18F2520
                TXSTA = 0x24;                                 // 8 bit mode, Transmit enable,                                                                                            Asynchronous mode, High speed 
                                                                             mode
                RCSTA = 0x90;                                // Serial enable, Receiving enable,
                BAUDCON = 0x00;                        // 8-bit Baud Rate Generator,
                SPBRG = 129;                                   // 9600 baud rate at 20Mhz oscillator.                                                                              0.16% error.
    }

    void USART_string( char *buffer)
    {
                int a = 0;
                do
                {           // Transmit a byte
                            while(!TXSTAbits.TRMT);
                            TXREG = buffer[a];
                            a++;
                } while( buffer[a] != 0 );
    }







    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel