Writing a Program for 8051 to Continuously Send and Receive Data

30 Apr 2023 Balmiki Mandal 0 Embedded C

Program for the 8051 to Transfer Data Serially and Parallel

This program is written to transfer letter “A” continuously as well as parallel, and to receive bytes of data serially at 9600 baud rate, using an 8051 microcontroller. The frequency of the controller is 11.0592 MHz.

The program begins by setting P1 as an output port. This is done using the instruction MOV P1, #0FFH, which moves the value 0FFH into P1.

Next, we configure the serial port. This is done using the instructions MOV SCON, #50H, which sets the serial port to mode 1, and MOV TMOD, #20H, which sets the serial port to auto-receive mode.

Next, we set the timer interrupt to generate an interrupt every 1 ms. This is done using the instructions MOV TL0, #0CBH and MOV TH0, #0B2H. This will generate an interrupt every 1 ms.

Now we can start the loop for the main program. This loop will continuously transmit the letter “A” via the serial port, as well as parallel. Every time the timer interrupt occurs, it will send the letter A in parallel by writing it to P1. At the same time, it will also receive any incoming bytes from the serial port and save them in a buffer.

The program will then wait until all the data has been received, before sending it out on P1. Finally, it will loop back to the beginning of the program.

This program should be able to transfer data successfully at 9600 baud rate. It also ensures that all data received is accurate, as it is being constantly monitored.

 

Here's an example program for the 8051 microcontroller to continuously transfer the letter "A" in parallel while also receiving bytes of data serially at 9600 baud and transferring them to P1. This program assumes that an external UART 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) {
        // Transfer the letter "A" in parallel
        P1 = 0x41;  // ASCII code for "A"

        // Check if a byte has been received
        if (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 continuously transfers the letter "A" in parallel by setting the value of P1 to the ASCII code for "A". It also checks if a byte has been received by checking the receive interrupt flag (RI) in the SCON register. If a byte has been received, it is read from the UART receive buffer (SBUF) and placed in the P1 register.

Note that this program only receives one byte of data at a time. To receive multiple bytes of data, you can modify the program to use a buffer to store the received data and process it as needed.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.