Use the serial port to control the light-off

Source: Internet
Author: User

This note contains two aspects:

1. Only control the LED light off and no value is returned.

2. Control the LED light off and return a value.

After reading a few videos, we are talking about the serial port method 1. Other functions are not available yet. Here we only use serial port method 1 to implement these two functions. In the serial port, the initial value of the timer is calculated based on the baud rate to be used. The timer uses method 2 to automatically install the initial value to avoid errors when the value assignment statement installs the initial value.

For example, 9800bps, Timer usage 2, serial port usage 1, crystal oscillator baud rate is 11.0592 MHz, and the values of Th1 and TL1 are obtained.

The baud rate of method 1 = (2 ^ smod/32) xt1 overflow rate. After the single-chip microcomputer is reset, all the power management registers pcon are cleared, and the smod is naturally cleared as one of them.

The baud rate is known. This leaves the T1 overflow rate.

Assuming that the initial value is X, the number of 255-x timers overflows once each time (the timer is 8 bits, and the maximum value is. 256 overflow ). The time of each count is a machine cycle, with the machine cycle = T clock cycle x 12. So the overflow time is = Number X each time = (256-x) * 12/fosc. Then the base rate is the reciprocal of the overflow time.

Therefore, combined with the formula "the baud rate of method 1 = (2 ^ smod/32) xt1 overflow rate", the formula can be summarized:

9600 = 2 ^ 0/32 * fosc/(256-x) * 12 bring all known data to get 9600 = 2 ^ 0/32*11059200/(256-x) * 12 = "" ", returns x 253.

On this basis, if smod is set to 1, the baud rate is:

Baud Rate = (2 ^ 1/32) * 11059200/(256-253) = 2 * [1/32*11059200/(256-253)] = 2*9600 = 19200. That is, it is twice the original.

If you change the crystal oscillator to 12 MHz and then calculate the initial value, the obtained X is: 252.744792 ...... Infinite decimals. This will produce errors. I used to feel that the integer crystal oscillator is quite good. Now I know why the 11.0592mhz crystal oscillator exists.

In this way, the initial value is obtained and the following code is pasted.

It only controls the light-off of the LED and does not return a value.

This can be implemented in two ways: Query and interrupt.

A. First use query. It is better to judge, because it is implemented by using if judgment.

# Include <reg52.h> void main () {// set the parameter tmod = 0x20; // set the method of timer 1 to 2th1 = 0xfd; TL1 = 0xfd; // load Th1 and tl1tr1 = 1; // start the timer 1ren = 1; // allow serial receiving bit sm0 = 0; SM1 = 1; // set the serial port mode to 1/** Ea = 1; // The number of elasticsearch instances allowed for global interruption * es = 1; // serial interrupt bits * the query method is used to determine the receipt interrupt bits. Therefore, even if the interrupt bits are not enabled, You Can **/while (1) {// riif (Ri = 1) detected by the query method // Ri indicates the receiving interrupt flag. Hardware is set to 1, software must be cleared 0 {p1 = sbuf; rI = 0 ;}}}

B interrupt method

# Include <reg52.h> void main () {// set the parameter tmod = 0x20; // set the method of timer 1 to 2th1 = 0xfd; TL1 = 0xfd; // load Th1 and tl1tr1 = 1; // start the timer 1ren = 1; // allow serial receiving bit sm0 = 0; SM1 = 1; // set the serial port mode to 1 Ea = 1; // The number of elasticsearch instances with global interruptions is 1; // The number of while (1) ports with serial ports with interruptions is 1 ); // waiting for interruption} // interrupt detection rivoid Ser () interrupt 4 {p1 = sbuf; rI = 0 ;}

In addition to the Code, these two operators feel that they are allowed to enable the interrupt. Because Ri is set to 1, it is automatically executed by hardware. You can use if to determine whether to enable the interrupt bits.

The above two are single-direction, and then two-way.

/** Send data to the lower computer through the serial port and display it on the fl lamp at the P1 port. * At the same time, the microcontroller returns the received data, which is displayed on the serial port assistant */# include <reg52.h> unsigned char flag; void main () {// set the parameter tmod = 0x20; // set the timer 1 to work in the following way: 2th1 = 0xfd; TL1 = 0xfd; // load Th1, tl1tr1 = 1; // start the timer 1sm0 = 0; SM1 = 1; // set the serial port mode to 1ren = 1; // enable serial receiving bit Ea = 1; // enable global interruption bit es = 1; // Serial Port Interrupt allowed bit while (1) {/* the MCU buffer register is empty at the beginning, and no data can be displayed * receive data from the serial port first, then return the data * receives the data during the interruption, and sets the flag position to 1. indicates that the received data * If the received data (flag = 1) is received, the received data is received; otherwise, the received data is not received and not displayed. Continue to judge the flag value */If (flag = 1) {// send data es = 0; // disable serial port interruption and send data sbuf = p1; // write data to the sbuf register while (! Ti); // wait Ti = 0; es = 1; flag = 0 ;}} void Ser () interrupt 4 {// receive data p1 = sbuf; flag = 1; ri = 0 ;}

The flag in the main function is 0. It must not be missing. Otherwise, the serial assistant will be stuck for a short time ....

In this example, there are two more important statements:

P1 = sbuf; // assign the value in the sbuf register to p1

Sbuf = p1; // write the value of P1 to sbuf

Sbuf is written in this way: sbuf serial data buffer register, a sending buffer register, and a receiving buffer register. The two public addresses are 99 h, but physically they are two independent registers. How can we distinguish between sending and receiving? It is distinguished by statements.

If you want to control the flow lamp, you need to send the hexadecimal format.

For example, send FB (1111,1011 ). On my Development Board, L2 lights are on. It is not easy to control the sending characters. If the "FB" is sent in the form of characters using the routine in 2, the microcontroller returns the serial assistant and displays it as "62" in hexadecimal notation.

There are no memorable images, or several fl lights. But at this time, the water lamp is not the water lamp at that time. The current fl lamp, but I can control the development board from the computer: d

I just don't know when I will write my host computer next time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.