Writing a Program for 8051 to Receive Bytes of Data Serially

30 Apr 2023 Balmiki Mandal 0 Embedded C

The 8051 microcontroller can be used to receive bytes of data serially and store them in memory. To achieve this, you will need to write a program for the 8051 to set the desired baud rate at 9600, 8-bit data, and 1 stop bit.

Programming Steps

  1. Set the communication mode – SCON register to Serial Mode 1. This defines that the microcontroller will communicate with 8 data bits, 1 stop bit, and no parity.
  2. Set the baud rate – TMOD register can be used to define the baud rate.
  3. Enable the serial port – Set the bit SM0 of SCON register to 1.
  4. Set the receiving flag – Set the bit RI of SCON register to 0.
  5. Load the address of the storage location in P1 into the Data Pointer– DPTR register.
  6. Receive a byte of data – SBUF register can be used to receive the byte of data.
  7. Wait until the reception is complete – Wait until the RI bit of SCON register is set to 1.
  8. Store the received data in the address in P1– Move the contents of SBUF to the address pointed by DPTR.
  9. Repeat steps 5 to 8 until all the data has been received.

The above steps can be used to write a program for the 8051 to receive bytes of data serially and put them in P1, set the baud rate at 9600, 8-bit data, and 1 stop bit.

 

Example  program for the 8051 microcontroller to receive bytes of data serially and put them in P1. This program assumes that an external UART (universal asynchronous receiver-transmitter) module is connected to the microcontroller and is configured to communicate at 9600 baud, 8-bit data, and 1 stop bit.

#include <8051.h>

void serial_init() {
    // Set the UART baud rate to 9600
    TMOD |= 0x20;  // Timer 1 in mode 2
    TH1 = 0xFD;    // Set reload value for 9600 baud at 11.0592 MHz crystal
    SCON = 0x50;   // 8-bit data, 1 stop bit, enable receiver
    TR1 = 1;       // Start Timer 1
}

void main() {
    unsigned char data_byte;

    serial_init();

    while (1) {
        // Wait for a byte to be received
        while (!RI);

        // Read the received byte from the UART
        data_byte = SBUF;

        // Put the received byte in P1
        P1 = data_byte;

        // Clear the receive interrupt flag
        RI = 0;
    }
}

In this program, the serial_init() function sets up the UART to communicate at the desired baud rate and data format. The main() function then enters an infinite loop where it waits for a byte to be received by checking the receive interrupt flag (RI) in the SCON register. Once a byte is received, it is read from the UART receive buffer (SBUF) and placed in the P1 register. Finally, the receive interrupt flag is cleared to indicate that the byte has been read.

Note that the exact configuration and register settings for the UART may vary depending on the specific hardware and microcontroller being used.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.