8051 serial




















For using serial port we simply need to configure the serial port:. Each of these register is of 1 byte size. SM0: Serial port mode Bit 0 of serial port mode.

SM1: Serial port mode Bit 1 of serial port mode. TB8: Transmit bit 8 Set or clear by software. The 9 bits will be transmitted in mode 2 and 3. RB8: Receive bit 8 Set or clear by software. The 9 bits will be received in mode 2 and 3. First eight bits are the data received and 9 th bit received will be placed in RB8. TI: Transmit Interrupt flag Set by hardware when a byte is transmitted completely. Now the port is free and ready to send the next byte.

This bit must be cleared by software. RI: Receive Interrupt flag Set by hardware when a byte has been completely received. This lets the program to know that it needs to read the value quickly before another byte is read. The Upper four bits are configuration bits. SM0 and SM1 set the serial mode between 0 and 3. The four modes along with the baud rates are in the table given below. In modes 0 and 2 the baud rate is fixed which is based on the oscillator frequency. In modes 1 and 3 the baud rate is variable which depends on how often Timer 1 overflows.

SM2 is a flag bit for Multiprocessor communication. By this way, program knows that a byte has been received and it needs to be processed.

Mode 3. Mode 1 8-bit UART. This is asynchronous communication, so no clock signal is sent with the data. Instead, a single start bit always logic 0 and a single stop bit always logic 1 encapsulate the data 8 bits.

Therefore, in total, ten bits are sent per byte of data. Baud rate. If the designer of an system is required to transmit data from the serial port to a device e. By default, the Baud rate is equal to the timer 1 overflow frequency divided by How to control the Baud rate? To configure the port to a specific Baud rate we therefore need to configure timer 1 to give us an overflow at an appropriate interval.

This can be achieved by setting up timer 1 as an 8-bit auto-reload interval timer. Then, if we put the correct value into TH1, the overflow signal will have the appropriate frequency to give us the Baud rate we desire.

The formula that determines the relationship between TH1 and Baud rate:. Programming the Baud rate. The code below shows how to configure both timer 1 and the serial port to transmit data at Baud. CLR SM0 ;. Once the timer is started, the program does not need to do anything else with it.

The serial port itself monitors through hardware the overflow signal and at every 32 nd overflow 16 th if SMOD is set a bit is transmitted. Various standard Baud rates. The table shows some standard Baud rates together with the required TH1 value to achieve this Baud rate with the serial port in mode 1.

Calculation of system clock rate for desired Baud rate. Why In Example 9, why do we need to choose To transmit data along the serial line you simply write to the serial buffer and. Baud rate refers to the number of bits transmitted per second. It is very important to decide the number of bits that are transferred per second for proper communication between two devices.

Both devices should support a particular baud rate for communication to occur. The can generate a number of baud rates using timer one in mode 2 8-bit auto-reload to generate baud rates. As mentioned in our article on timers and counter the master clock of the microcontroller is divided by 12 to give the machine cycle frequency In the case of serial communication, this machine cycle frequency is divide by 32 and fed to timer 1.

So timer 1 gets a frequency of 28, Hz. What this means is that the timer gets 28, pulses in a second. Now there are a number of baud rates that can be generated by using timer 1 and this input frequency. The table for the same is given below. In this case timer 1 is set to mode 2 auto-reload in which every time the timer overflows the value in TH1 is reloaded into TL1, and the timer counts up from the value fed into it.

In the case of generating Mhz baud rate TH1 is fed a value of FDH and as it is an 8 bit up counter, it counts up from to and then overflows. This frequency of overflow is used to generate the baud rate. Bit 7 of the PCON register can be used to double the baud rate. When the SMOD bit is set to 0 the value of the dividing factor changes from 32 to 16 due to which the baud rate doubles.

Parity bits are used in mode 2 and 3. The main reason for using parity bits is to ensure correct data transmission. In some cases, due to noise in the channel data gets corrupted. To send a parity bit the particular bit must be put in TB8 register which transmits the data with the 8-bit data in mode 2 or 3. This bit is received and stored in the RB8 register during the reception. Now that we have an idea of all the components required for serial communication, let us look at a few programs to understand it better.

In this method, we continuously monitor flags RI and TI. RB8: It is used in modes 2 and 3, it is the 9th bit received by the microcontroller.

TI: It is known as Transmit Interrupt flag which is set by hardware to indicate the end of a transmission. It has to be cleared by the software. RI: It is known as Receive Interrupt flag which is set by hardware to indicate the end of a reception.

It is defined as number of bits transmitted or received per second and usually expressed in Bits per second bps. Learn how to configure Timers in It is a 8 bit register that holds the data needed to be transmitted or the data that is received recently. The serial port of is full duplex so the microcontroller can transmit and receive data using the register simultaneously. Initialize — Initialize timer for baud rate and set mode for serial transmission.

This code have three parts initialize ,receive and transmit to perform the process of serial communication.



0コメント

  • 1000 / 1000