Brief analysis of common problems in MCU Keil

Publisher:MeshulunLatest update time:2016-10-21 Source: ofweekKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. Hybrid Programming

  1. Interface within the module:

  Use the following identifiers:

  #pragma asm

  Assembly Statements

  #pragma endasm

  Note: If you use assembly language in your C51 program, be aware that you need to activate the "Generate Assembler SRC File" and "Assembler SRC File" options in the Properties of the Keil compiler.

  Let's take an example:

  #i nclude

  void main(void)

  {

  P2=1;

  #pragma asm

  MOV R7,#10

  DEL:MOV R6,#20

  DJNZ R6,$

  DJNZ R7,DEL

  #pragma endasm

  P2=0;

  }

  Other:

  1. Add "xx.c" to the project, right-click "xx.c" and select "options for file"xx.c" Select "Generate Assembler SRC File" and "Assemble SRC File" and check the black box to make it effective;

  2. According to the selected compilation mode, add the corresponding library file to the project like adding "xx.c" and place it under "xx.c". For example, in smail mode, select "keilc51libc51s.lib" to add it to the project. If floating-point operations are to be performed, also add "keilc51libc51fpl.lib" to the project.

  The LIB files in the C51LIB directory under the Keil installation directory are as follows:

  C51S.LIB - Small model without floating point operations

  C51C.LIB - Compact model without floating point operations

  C51L.LIB - Large model without floating point operations

  C51FPS.LIB - Small model with floating point operations

  C51FPC.LIB - Compact model with floating point operations

  C51FPL.LIB - Large model with floating point operations

  3. Add optimization to the "xx.c" header file: for example, #pragma OT(4,speed)

  4. Add assembly code to "xx.c"

  #pragma ASM

  ;Assembler Code Here

  #pragma ENDASM

  5. Compile and generate xx.hex

  Notice:

  If you do not do the first step, you will get the following warning: 'asm/endasm' requires src-control to be active

  If you do not do the second step, the following warning will appear: UNRESOLVED EXTERNAL SYMBOL;

  REFERENCE MADE TO UNRESOLVED EXTERNAL等

  If you do not do step 3, the following warning will appear: UNDEFINED SYMBOL (PASS-2)
 

  2. Interrupt Usage

  interrupt xx using y

  The xx value after interrupt is the interrupt number, which means the interrupt port number to which this function corresponds. It is usually in 51.

  0 External interrupt 0

  1 Timer 0

  2 External interrupt 1

  3 Timer 1

  4 Serial interrupt

  The others have their own meanings according to the corresponding microcontroller. In fact, when C is compiled, the entry address of your function is placed in the jump address of the corresponding interrupt. Using y means that the register group used by this interrupt function is generally 4 registers r0-r7 in 51. If your terminal function and other programs do not use the same register group, the register group will not be pushed into the stack when entering the interrupt, and will not be popped out when returning, saving code and time.

  3. How to use reentrant

  The following warning appeared in the program:

  *** WARNING L15: MULTIPLE CALL TO SEGMENT

  SEGMENT: ?PR?_CRCDATA?PANEL_DISP

  CALLER1: ?C_C51STARTUP

  CALLER2: ?PR?UART_RECV?PANEL_DISP

  *** WARNING L15: MULTIPLE CALL TO SEGMENT

  SEGMENT: ?PR?ANALOGALLBECKON?PANEL_DISP

  CALLER1: ?C_C51STARTUP

  CALLER2: ?PR?UART_RECV?PANEL_DISP

  *** WARNING L15: MULTIPLE CALL TO SEGMENT

  SEGMENT: ?PR?SWITCHALLBECKON?PANEL_DISP

  CALLER1: ?C_C51STARTUP

  CALLER2: ?PR?UART_RECV?PANEL_DISP

  My program compiled with these 3 warnings, but the program can be downloaded and run normally. But I think these warnings will cause bugs in the program. Literally, it means that the receiving function UART_RECV() in my program calls analogAllBeckon() and switchAllBeckon() multiple times.

  Because the ordinary functions of 51 are not reentrant, the variables are placed at fixed addresses. When two functions are run at the same time, the same variable will be modified, resulting in wrong results. So I added void analogAllBeckon()reentrant{//All Analog data beckon after analogAllBeckon() and switchAllBeckon() functions to eliminate the warning in the program. This method shows that the function can be called by multiple tasks without modifying the variable value in the function, so as to achieve the reentrant nature of the function.

  There is a detailed discussion on the use of reentrant on the Keil official forum.

  Andy Neil (official engineer) suggested

  "Are you sure that you really need to make everything reentrant?...A reading of the Keil app notes & knowledgebase articles on this subject showed that it was not necessary. "

  Since each time a function declared by reentrant is called, the function parameters and internal variables must be pushed onto the stack, it is easy to cause the stack to overflow. S52 only has a 256-byte data segment. If a simple function has one parameter and three internal variables, more than 4 bytes need to be pushed onto the stack, not including the function call stack. Reentrant is not actually suitable for low-end microcontrollers. Someone on the Keil forum said that reentrant is only suitable for microcontrollers with more than KB of RAM.

  4. Variable declaration

  The difference between data, idata, xdata, and pdata in the 51 series data: fixedly refers to the first 128 RAMs from 0x00 to 0x7f, which can be directly read and written with acc, with the fastest speed and the smallest generated code. idata: fixedly refers to the first 256 RAMs from 0x00 to 0xff, of which the first 128 are exactly the same as the 128 of data, but the access method is different. idata is accessed in a pointer-like manner similar to that in C. The statement in the assembly is: mox ACC, @Rx. (Unimportant supplement: pointer-style access of idata in C works very well) xdata: external extended RAM, generally refers to the external 0x0000-0xffff space, accessed with DPTR. pdata: the lower 256 bytes of external extended RAM, read and written when the address appears on A0-A7, read and write with movx ACC, @Rx. This is quite special, and C51 seems to have a bug for this, so it is recommended to use it less. But it also has its advantages, and the specific usage belongs to the intermediate problem, so it is not mentioned here.

  The role of startup.a51 is the same as that of assembly. The variables and arrays defined in C are initialized in startup.a51. If you define a global variable with a value, such as unsigned char data xxx="100";, then startup.a51 will have a related assignment. If there is no =100, startup.a51 will clear it to 0. (startup.a51==variable initialization). After these initializations are completed, the SP pointer will also be set. There will be no assignment or clearing action for non-variable areas, such as the stack area. Some people like to modify startup.a51 to satisfy their own hobbies. This is unnecessary and may be wrong. For example, when power-off protection is applied, you want to save some variables, but modifying startup.a51 to achieve this is a stupid method. In fact, you can just use the characteristics of the non-variable area and define a pointer variable to point to the lower part of the stack: 0xff. Why do you still need to modify it? It can be said that you don't need to modify startup.a51 at any time if you understand its characteristics.

  5. Type-related

  A variable can be defined using bit, but not using sbit. sbit can define a port.

Keywords:MCU Reference address:Brief analysis of common problems in MCU Keil

Previous article:Choosing the Right 8-bit MCU Communication Interface for IoT Applications
Next article:Design of Intelligent Level Converter Based on Single Chip Microcomputer

Recommended ReadingLatest update time:2024-11-16 07:49

Design of remote control of household appliances by telephone based on AT89C51 microcontroller and DTMF communication
introduction With the development of computer technology and telecommunications, remote communication through telephone lines has become more and more common. People usually use MODEM for communication, but in applications where the amount of communication data is not large and the communication rate is not high, we c
[Microcontroller]
Design of remote control of household appliances by telephone based on AT89C51 microcontroller and DTMF communication
An article analyzing 51 microcontroller PWM dual servo control
  Introduction to PWM   Pulse width modulation is a very effective technology that uses the digital output of a microprocessor to control analog circuits. It is widely used in many fields from measurement and communication to power control and conversion.   Pulse width modulation is an analog control method that mod
[Microcontroller]
An article analyzing 51 microcontroller PWM dual servo control
Design of I2C bus interface circuit based on MCS-51 microcontroller
The I2C bus is composed of the serial data line SDA and the serial clock line SCL, which can send and receive data. It allows several compatible devices to share the bus. All devices and interface circuits connected to the I2C bus should have an I2C bus interface, and all SDA/SCL should be connected to the same end. A
[Microcontroller]
Design of I2C bus interface circuit based on MCS-51 microcontroller
The difference between 51 microcontroller's on-chip RAM and off-chip RAM
When introducing the microcontroller resources earlier, we mentioned that the STC89C52 has a total of 512 bytes of RAM, which is used to save data. For example, the variables we define are stored directly in the RAM. However, the 512-byte RAM of the microcontroller is not all equal in status, but is divided into block
[Microcontroller]
Design of intelligent traffic lights based on real-time operating system RTX51 and AT89C52 microcontroller
This paper introduces a design method of intelligent traffic lights that dynamically adjusts time based on changes in traffic flow; while conducting traffic statistics, it monitors violations; allocates the green light time of each lane according to a fuzzy algorithm to achieve dynamic adjustment of traffic flow. Anal
[Microcontroller]
Design of intelligent traffic lights based on real-time operating system RTX51 and AT89C52 microcontroller
How to control LED lights using touch sensor and 8051 microcontroller
In this project we are interfacing touch sensor with 8051 microcontroller AT89S52. If you are new to 8051 microcontrollers then you can start the 8051 by blinking an LED. What is a capacitive touch sensor? Capacitive touch works on the electrostatic charges available on our bodies. The screen is already filled with
[Microcontroller]
How to control LED lights using touch sensor and 8051 microcontroller
Teach you step by step how to learn microcontroller, and you will be able to write and develop your own programs immediately after learning.
To learn microcontroller, we have to divide it into several steps: The first step: buy a learning development board, which will probably cost more than 100 oceans. You should buy a development board that is suitable for beginners, as long as it has basic functions. It does not seem to be very rich in resources, but th
[Microcontroller]
A brief discussion on several self-refresh methods of MCU Boot
The main function of the car software Boot program is to refresh the App program. In a specific customer project, Boot is also part of the customer's needs, and there is also a software development plan following the project (some call the Boot on the project CB, Customer Boot, in order to distinguish it from other Bo
[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号