Important considerations for using serial ports in Keil C51

Source: Internet
Author: User

Problem: Using a computer to send data through the serial port to the SCM, not continuously sent or every time the data is not too large SCM can be normal to receive, but the continuous sending a large number of data can not work correctly, to determine the hardware is certainly no problem, the software seems to have no problem

Find the code that's causing the problem
P3 = ((P3 & 0xc3) | (newval<<2)); Code 1 >
It could be that this line of code affects that the serial port does not work at all to change to the following code, the serial port works fine
P3 &= 0xc3; Code 2 >
P3 |= (NEWVAL&LT;&LT;2);
The AT89C51 data booklet, with two points of particular importance, was consulted
1, when the P3 mouth is not used for IO, and the use of the second function, the origin will be the mouth of 1, otherwise the mouth will always remain 0 (the alternate functions can only activated if the corresponding bit latch in t He port SFR contains a 1. Otherwise the port pin is stuck at 0.)
2, when using the following instructions, read the value of the port register, not the value of the external pin (when the destination operand is a port, or a port bit, these instructions read the latch Rather than the pin):
ANL (logical and e.g., ANL p1,a)
ORL (logical OR, e.g., ORL p2,a)
XRL (logical ex-or, e.g., XRL p3,a)
JBC (jump if bit = 1 and clear bit, e.g., JBC P1.1, LABEL)
CPL (complement bit, e.g., Cpl P3.0)
Inc (increment, e.g., Inc P2)
Dec (decrement, e.g., Dec P2)
DJNZ (decrement and jump if not zero, e.g., djnz P3, LABEL)
MOV PX. Y,c (move carry bit to bit Y of Port X)
CLR PX. Y (Clear bit y of Port X)
Setb PX. Y (Set bit y of Port X)
The following is a comparison of the procedures used in this section of the code, as follows
: {Uchar newval = 0x18;
c:0x003a 756018 MOV 0x60, #0x18
49:P3 = ((P3 & 0xc3) | (newval<<2));
50:
c:0x003d E560 MOV a,0x60
c:0x003f 25E0 ADD A,ACC (0XE0)
c:0x0041 25E0 ADD A,ACC (0XE0)
c:0x0043 FF MOV R7,a
c:0x0044 e5b0 MOV a,p3 (0xb0)//read here is an external pin (PIN)
c:0x0046 54c3 ANL A, #0xC3
c:0x0048 4F ORL A,R7
c:0x0049 f5b0 MOV P3 (0xb0), A
51:newval = 0x28;
c:0x004b 756028 MOV 0x60, #0x28
52:P3 &= 0xc3;
c:0x004e 53b0c3 ANL P3 (0xb0), #0xC3//Here the registers are read and modified
53:P3 |= (NEWVAL&LT;&LT;2);
c:0x0051 E560 MOV a,0x60
c:0x0053 25E0 ADD A,ACC (0XE0)
c:0x0055 25E0 ADD A,ACC (0XE0)
c:0x0057 42b0 ORL P3 (0xb0), A
54:}
Answer:
The reason for the serial port problem is very clear, is to use the code 1>, read the external pins, and then the serial port is communicating, may receive the bit just 0, read in is also 0, and then write it to the P3, is the register written 0, some time the receiver end of the serial port is set zero, serial port input is prohibited, As a result, the data will not be received at all, and the register is modified with the code 2>, And p3.0,p3.1 is the serial port of two IO Mouth line register is 1, read is still 1, through the ANL, and ORL operation is the register, so will not overwrite the value of P3 register, example mouth serial port can still work.
Suggestions:
The use of IO port is best to only need bits through the bit operation instructions to operate, to do not need to operate the bit is to avoid operations, if you want to operate at the same time a bit more than the use of &= and |= operator operations, do not use assignment operator =.

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.