Keil C51 continuous reading method of the same port

Publisher:painterLatest update time:2016-12-14 Source: eefocusKeywords:Keil Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    C language is one of the most efficient, concise and hardware-friendly programming languages ​​that is universally recognized. The transplantation of C language to the single-chip microcomputer MCS-51 began in the mid-to-late 1980s. After nearly 10 years of development, C language has overcome the shortcomings of long code generation and slow running speed. In addition, C language has unparalleled advantages over assembly language in terms of development speed, software quality, structuring, maintainability, etc., and has been increasingly widely used. Keil C51 is a single-chip microcomputer C language compilation system developed by Keil, Germany. The software is fully functional and is one of the most widely used languages ​​by domestic technical developers.
    In actual work, it is found that programs written in C language that continuously read the same port often fail to execute after being compiled by Keil C51. Take the 8051 single-chip microcomputer reading 12-bit A/D MAX197 as an example, as shown in Figure 1.
 
    In Figure 1, P1.1 port is used to read the interrupt signal sent by A/D when the conversion is completed, and P1.0 selects to read the high 4 bits or low 8 bits. Now assume that the address of A/D is 8000H, and the starting working word of CH0 port is 40H. To obtain the corresponding high and low conversion data, program in C language as follows.
#include
unsigned char xdata MAX197 _at_ 0x8000;
sbit MAXINT= P1^1;
sbit MAXHBEN= P1^0;
……
void main()
{unsigned char up4, down8; //Set up two variables to receive data
……
MAX197= 0X40; //Start A/D CH0 port for conversion
while(MAXINT) //Wait for conversion to complete
{};
P1.0=0; //Read the lower 8 bits
down8=MAX197;
P1.0=1; //Read the upper 4 bits
up4=MAX197;
}
    The above program does not get the high and low bit data as expected. In fact, the data obtained in down8 and up4 are the lower 8 bits. The following is part of the C language code after compilation.
  41: //Get the lower 8 bits
  42: MAXHBEN=0;
C:0x000C C290 CLR MAXHBEN(0x90.0)
  43: down8=MAX197;
C:0x000E 908000 MOV DPTR,#MAX197(0x8000)
C:0x0011 E0 MOVX A,@DPTR
C:0x0012 F509 MOV 0x09,A
  44: //Get the upper 4 bits
  45: MAXHBEN=1
C:0x0014 D290 SETB MAXHBEN(0x90.0)
  46: up4=MAX197;
  47:
  48: 
C:0x0016 F5O8 MOV 0x08,A //0x08 is up4
  49: }
    By analyzing the above program, we can find that the C compiled program does not read the port again after P1.0 is set to high potential, but directly sends the result of the last read to the high 4-bit variable. If the high bit is read first and the low bit is read later, two high 4-bit data will be obtained. To confirm this, put 4 C language statements that read an external port repeatedly together. After compiling, it is found that only the first statement is compiled and executed. In other words, Keil C51 performs "special" processing when compiling for repeated reading of the same port address, which is very noteworthy. So how to deal with the situation where the same port really needs to be read continuously? The following two methods are introduced for reference.

The first method: add delay.
    The delay should not be too long, especially when the conversion speed is required to be high. First, write a delay function:
void delay()
{unsigned char i;
for (i=0, i<=1;i++);
}
Then put the delay program in the middle of the above two reads.
P1.0=0; //Read the lower 8 bits
down8=MAX197:
delay();
P1.0=1; //Read the upper 4 bits
up4=MAX197;
The compiled results are as follows:
 49: //Get the lower 8 bits
 50: MAXHBEN=0:
C:0x000C C29O CLR MAXHBEN(0x90.0)
 51: down8=MAX197;
C:0x000E 908000 MOV DPTR,#MAX197(0x8000)
C:0x0011 E0 MOVX A,@DPTR
C:0x0012 F509 MOV 0x09,A
 52: delay();
 53: //Get the upper 4 bits
C:0x0014 120029 LCALL delay(C:0029)
 54:MAXHBEN = 1;
C:0x0017 D290 SETB MAXHBEN(0x90.0)
 55:up4=MAX197;
 56:
 57:
C:0x0019 E0 MOVX A,@DPTR
C:0x001A F508 MOV 0x08,A
 58: }
    It can be seen that after P1.0 is set high, the port is read and written again, the program is normal and the upper 4 bits are obtained.

The second method: set another pointer.
void main()
{unsigned char up4,down8; //Set 2 variables for receiving data
unsigned char xdata *pt1;
pt1=0x8000;
……
MAX197=0X40; //Start A/D CH0 port for conversion
while(MAXINT) //Wait for conversion to complete
{};
P1.0=0; //Read the lower 8 bits
down8= MAX197:
P1.0=1; //Read the upper 4 bits
up4=*pt1:
……
The compiled result is as follows:
 42: //Get the lower 8 bits
 43: MAXHBEN=0;
C:0x0010 C290 CLR MAXHBEN(0x90.0)
 44: down8=MAX197;
C:0x0012 908000 MOV DPTR,#MAX197(0x8000)
C:0x0015 E0 M0VX A,@DPTR
C:0x0016 F509 MOV 0x09,A
 45: MAXHBEN=1:
 46: //Take the upper 4 bits
 47:
C:0x0018 D290 SETB MAXHBEN(0x90.0)
48: up4=*pt1:
49:
50:
C:0x001A 8F82 MOV DPL(0x82),R7
C:0x001C 8E83 MOV DPH (0x83),R6
C:0x001E E0 MOVX A,@DPTR
C:0x001F F508 MOV 0x08,A
The above two methods both solve the problem that Keil C51 cannot handle continuous reading and writing of a port, but if the conversion speed requirement is particularly high, it is recommended to use the second method.

Keywords:Keil Reference address:Keil C51 continuous reading method of the same port

Previous article:C51-keil compilation common errors and warnings processing 53
Next article:51 high-precision division program and its use

Recommended ReadingLatest update time:2024-11-16 12:44

51 MCU Tutorial Lecture 5_C51 Program
The C51 language program is composed of a main function and several functions. The main function is the starting point of the program. When the microcontroller runs the program, it will first execute the first statement in the main function until all the statements in the main function are executed. The program of the
[Microcontroller]
C51 MCU timer/counter control word setting
Before using the timer/counter, you need to set its working mode through the timer/counter control word. There are two special function registers related to timing/counting in the microcontroller, which are TMOD and TCON. By the way, TMOD and TCON are names. We can directly use these names to specify them when writing
[Microcontroller]
C51 MCU timer/counter control word setting
How to use Keil 5 to write 51 MCU project
At present, we usually use keil4 to write 51 programs, and many programs for STM32 and other microcontrollers use keil5. So how to make 51 and STM32 programs compatible in keil5, eliminating the tediousness of switching versions?  It's very simple and can be done in just two steps.  The following method is for a s
[Microcontroller]
How to use Keil 5 to write 51 MCU project
PID Control C51 Program (4)
typedef struct PID {      double     SetPoint;         // Desired Value      double     Proportion;         // Proportional Const      double     Integral;         // Integral Const      double     Derivative;         // Derivative Const      double     LastError;         // Error      double     PrevError;         //
[Microcontroller]
Notes on using printf in KEIL C51
Key points of using printf() function in keil   When searching for information online, I found an article introducing the use of printf() function in keil. I copied it here as a memo. In Keil, printf sends data to the serial port by default. Therefore, if you use this function, you must initialize the serial port f
[Microcontroller]
Keil uses fromelf to generate bin files
1. The syntax format is as follows: fromelf input_file The options are as follows and can be used in combination, separated by spaces: 2. Usage process in Keil 1. Configure Output and generate executable files aa is the executable file name, and the suffix is ​​axf. That is, after compilation, a file
[Microcontroller]
Keil uses fromelf to generate bin files
"Beginner's C51 Self-study Notes" ADC0804
    #include reg52.h #define uchar unsigned char  #define uint unsigned int    sbit rs=P2^4; sbit rw=P2^5; sbit e=P2^6; sbit adcs=P3^5; sbit adrd=P3^0; sbit adwr=P3^3; sbit P23=P2^3;   float table1 =" www.baidu.com "; uchar table2 ="LCD1602 test ok!"; fly table3 ="0123456789";    //Delay function, at 12MHz crystal
[Microcontroller]
How to set ROM using keil c51 to generate hex
I use at89s52 (256RAM, 8K ROM), and after compiling the program, it shows program size: data=56.0 xdata=0 code=3529. There is no external ROM in my circuit. How can I set it so that the generated hex code is only stored in the at89s52? It is definitely not okay to choose small, because my code is larger than 2K; if I c
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号