C51 Questions and Answers

Publisher:快乐的成长Latest update time:2011-02-27 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. What does #pragma asm mean?
#pragma asm
MOV P1,R7
NOP
NOP
MOV P1,#0
#pragma endasm
is actually a switch to embed assembly language in C51

2. An algorithm program for hex to bcd
HEX_BCD:CLR A
MOV 30H,A
MOV 31H,A
MOV 32H,A
MOV R2,#15
H_B0: MOV A,R1
RLC A
MOV R1,A
MOV A,R0
RLC A
MOV R0 ,A
MOV A,32H
RLC A
ACALL BCD_ADJ MOV
32H,A
MOV A,31H
RLC A
ACALL BCD_ADJ
MOV 31H,A
MOV A,30H
RLC A
ACALL BCD_ADJ
MOV 30H,A
DJNZ R2,H_B0
MOV A,R0
RLC A
MOV A ,32H
RLC A
MOV 32H,A
MOV A,31H
RLC A
MOV 31H,A
MOV A,30H
RLC A
MOV 30H,A
RET
BCD_ADJ:PUSH PSW
PUSH ACC
CJNE A,#50H,$+2
JC B1
POP ACC
ADD A,#30H
PUSH ACC
B1: ANL A,#0FH
CJNE A,#5,$+2
JC B2
POP ACC
ADD A,# 3
PUSH ACC
B2: POP ACC
POP PSW
RET

3. Questions about the ALE pin of the microcontroller
"When the microcontroller does not access the external latch, the ALE terminal has a positive pulse signal output, and this frequency is about 1/6 of the clock oscillation frequency. Whenever
the external data memory is accessed, ALE only appears once in two machine cycles, that is, one ALE pulse is lost." Is there something wrong with this sentence
? I think according to this statement, three ALE pulses should be lost. I have been unable to figure out what is going on. I hope the experts can help me.
I am grateful.
Answer:
All other instructions issue one ALE every 6 machine cycles, while the MOVX instruction takes 12 machine cycles and only issues one ALE

4. How to convert an INT data into two CHAR data?
After Keil optimization, char1=int1/256, char2=int1%256 or char1=int1>>8, char2=int1&0x00ff have the same efficiency.

5. After simulating on KEIL C51, how to generate HEX file for burning? ?
Right-click Target 1 in the project, select the second one, and select CREAT HEX in OUTPUT

6. What is the difference between typedef and #define?
What is the difference between typedef and #define >>> For example,
typedef unsigned char UCHAR;
#define unsigned char UCHAR;

typedef names a new data type, but in fact this new data type already exists, it just defines
a new name.
#define is just a label definition.
There is no difference between the examples you gave, but #define can also be used like this:
#define MAX 100
#define FUN(x) 100-(x)
#define LABEL
, etc. These cases cannot be defined with typedef

7. How to set the simulation working frequency (clock) of KELC51?
Right-click on target 1 on the left, and then enter in the xtal column

8. How to share sbit variables between different modules? Extern is not possible?
Put the SBIT definition in a separate .H file, and each module includes this .h file

IX. Do I need to define the access to Px.x in C51?
Yes.
For example, sbit P17 = 0x97; can define the access to P1.7

10. The expression in the SWITCH() statement cannot be a bit variable, right?
You can use a bit variable:
#include
#include

void main()
{
bit flag;
flag=0;
switch(flag)
{
case '0':{printf("0n");break;}
case '1':{printf("1n");break;}
default:break;
}
}

A bit variable has only two states, so an if statement is enough!!!

11. Does const constant declaration occupy memory?
const is only used to define "constants". The space occupied depends on your definition. For example,
const code cstStr[] ={"abc"};
occupies code space; while
const char data cstStr[] ={"abc"};
certainly occupies memory space.
In addition, the definition of #define does not seem to occupy space.

12. How to use the extended RAM of Philips' single-chip microcomputer P89C51RD+ in C51?
Try to clear auxr.1 to 0, and then directly declare a variable of type xdata in C language

13. BUG of Keil C51
In the program, the following statement is used:
const unsigned char strArr[] ={"数学"};
The result shows that the content of strArr[] is {0xCA,0xD1,0xA7}. It is really strange!

If there is 0xfd, it will disappear. So we can only enter the internal code manually, for example, uchar strArr[] =
{0xCA,0xfd,0xd1,0xa7} (Ultraedit is very convenient).

14. How to achieve code optimization in Keil C51?
Under the Project menu, Option for target "Simulator" C51.
Do you see Code optimization?

15. What is the difference between the ! and ~ symbols in c?
! is logical inversion, ~ is bitwise inversion.

16. When programming C51 and reading a port, do you need to output 1 first?
I see that some do, but some don't. Can an expert tell me what's going on? Thanks.
It should output 1, unless you can guarantee that it was already 1 before and no other value has been output in the middle.

17. When timer 1 (T1) is used to generate baud rate, can P3^5 still be used as a normal I/O port?
P3.5 can be used as a normal IO port.

18. How to convert INT to 2 CHAR in C51?
Experts:
How to convert INT to CHAR in C51? For example:
X = LOW (Z);
Y = HIGH (Z);
Answer:
x = (char) z;
y = (char) (z>>8);

19. If I want to set the 7th bit of 2EH to 1, can I use bit operation?
I don't quite understand the bit operation instructions. Please give me some advice: For
example, SETB 07H means 20H.7 is set to 1, right? (I read this in a book)
So if I want to set the 7th bit of 2EH to 1, how can I express it like the example I gave? Thank you!
SETB 77H
setb (2eh-20h)*8+7
20h-2fh has 8 bits that can be operated (00h-7fh) per byte, and other RAM cannot be directly operated by bits

20. What is the difference between char *addr=0xc000 and char xdata *addr=0xc000?
char *addr=0xc000;
char xdata *addr=0xc000;
Are there any other differences except the different bytes occupied in the memory?

char *addr=0xc000; is a general definition, the pointer variable addr can point to the value of any memory space;
char xdata *addr=0xc000; specifies that the pointer variable can only point to the value in xdata;
in the latter definition, the pointer variable (addr) will occupy one less storage byte.


uchar xdata *addr=0xc000; pointer points to external ram;
if: data uchar xdata *addr=0xc000; pointer points to external ram but the pointer itself exists in internal ram (data)
and
so on idata uchar xdata *addr=0xc000; pdata uchar xdata *addr=0xc000;
data uchar idata *addr=0xa0;.....

....

21. How long does it take to execute while(p1_0)?
Assuming that P1_0 is the first pin of the microcontroller's P1 port, how long does it take to execute the above code in KEIL C:
while (P1_0)
{ P1_0=
0;
}
while(!P1_0)
{
P1_0=1;
}
? Can you explain the execution time of while(P1_0) in detail?

You can tell by running the simulation.
I tried it and it took about 14 cycles.

22. How to write a watchdog program for C51?
Dear experts, I used KEIL C51 to write a program with an external watchdog, but the program cannot run. After searching
, I found that after the program was compiled by C51, an initialization program was added to the front of the MAIN() function. When entering
the main program to set the watchdog, the watchdog time has expired and my program is reset. How can I modify this
initialization program so that the watchdog is set as soon as it runs?

You can add a watchdog refresh instruction to startup.a51, of course, using assembly, and then recompile startup.a51
and connect it to your program. The new startup.a51 will automatically replace the system default startup module.

23. Keil C51 How to add the modified startup.a51 to the project file
Just add it directly Be
careful not to change the symbols such as ?STACK, ?C_START, ?C_STARTUP. Startup.a51 can be added to the project directly without modification. You can modify some assembly restrictions or stack pointers in it.

24. About baud rate setting
I found a problem when setting the serial port baud rate: when the crystal oscillator is 11.0592MHz, if 9600BPS is set,
TH1=0XFD, TL1=0XFD, and if 19200BPS is set, will TH1 and TL1 change? If not, why? If changed, why
? (Because I read that the two are the same in the book), I hope you can give me some advice.
Answer:
When BIT7 (SMOD) of the power control register (PCON) is 1, the baud rate doubles.
The values ​​of TH1 and TL1 remain unchanged.

25. How to declare in C to reserve this part of RAM area and not be used by C?
I don't know how to control this in the C source program, but just add the following paragraph in the assembly program:
DSEG AT 20H
AA: DS 10
In this way, C51 will not occupy 20H--29H.
Or define it like this in C51:

uchar data asm_buff[10] _at_ 0x20;

26. Questions about floating-point operations
When I was using C51, I found that it has a limit on the number of floating-point parameters passed. May I ask:
1) The parameters are passed in the form of global variables. Are there also limits on the parameters passed in the form of global variables?
2) How many limits are there on this kind of floating-point parameter passing?
3) Is the result of float*float a float type or a double type? Can it be directly assigned to a float type variable?
Answer:
Since the parameters of KEIL C51 are passed through R0-R7, there will be restrictions.
However, KEIL provides a compilation parameter that can support the passing of more parameters. For
details, see the PDF document of KEIL.
I suggest that you define multiple parameters to be passed in pointers or structures, and pass parameters
through pointers or structures. This is better.

The answer to the third question is YES. You will know if you try it yourself.

27. How to define RAM at a certain address
using the _at_ command, so that you can locate a more flexible address
uchar xdata dis_buff[16] _at_ 0x6020 ; //Locate RAM
and locate dis_buff[16] at the 16 bytes starting at 0x6020

28. In Keil C, what function can be used to get the parity bit?
For example, for 32-bit data, XOR the four bytes with each other and check P. If you are worried that P may be changed, you can use inline assembly.
#include
unsigned char parity(unsigned char x){
x^=x;
if(P)return(1);
else return(0);
}

unsigned char parity2(unsigned int x){
#pragma asm
mov a,r7
xrl ar6,a
#pragma endasm
if(P)return(1);
else return(0);
}
Reference address:C51 Questions and Answers

Previous article:Talk about C51 programming standards
Next article:Simulate switch light

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号