PIC microcontroller C language delay program and loop subroutine implementation method

Publisher:茶叶侠Latest update time:2020-02-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Many friends say that the delay time cannot be accurately controlled in C and it is not as intuitive as assembly.

In fact, this is not the case. A deep understanding of the delay function will help you design an ideal frame price.


Generally, we use


for(x=100;--x;){;}This sentence is equivalent to x=100;while(--x){;};


or for(x=0;x<100;x++){;}


Let's write a delay function.


It is important to note here that X=100 does not mean that the loop will exit after only 100 instructions have been executed.


You can look at the compiled assembly:


x=100;while(--x){;}


After compilation:


movlw 100


bcf 3,5


bcf 3,6


movwf_delay


l2 decfsz _delay


goto l2


return


From the code, we can see that the total number of instructions is 303, and the formula is 8+3*(X-1). Note that the number of cycles is X-1, which is 99.


The summary here is the loop body when x is of type char. When x is int, it is more affected by the value of X.


It is recommended to design a char type loop body, and then use another loop body to call it, so as to achieve accurate and long-term delay.


Here is a function that can accurately control the delay. The assembly code of this function is the most concise and can most accurately control the instruction time:


void delay(char x,char y){


char z;


do{


z=y;


do{;}while(--z);


}while(--x);


}


The instruction time is: 7+(3*(Y-1)+7)*(X-1)


If we add the call instruction for function call, page setting, and 7 instructions for passing parameters.


Then it is: 14+(3*(Y-1)+7)*(X-1).


If the delay requirement is not particularly strict, you can use this function:


void delay(){


unsigned int d=1000;


while(--d){;}


}


This function produces a delay of 10003us under a 4M crystal, which is 10MS.


If D is changed to 2000, it will be 20003us, and so on.


Some friends don't understand why we don't use while(x--) followed by decrement to control how many cycles the set X value is?


Now let's look at the assembly code that compiles it:


bcf 3,5


bcf 3,6


movlw 10


movwf _delay


l2


decf _delay


incfsz _delay,w


goto l2


return


It can be seen that there is one more instruction in the loop body, which is not concise. Therefore, it is best to use the front decrement to control the loop body in PICC.


Let’s talk about this sentence:


for(x=100;--x;){;} and for(x=0;x<100;x++){;}


The two have the same meaning literally, but you can view the code through assembly. The latter code is long, while the former compiles the concise code well.


Therefore, it is best to write the loop body in the former form in PICC. A good C compiler will automatically convert the increment loop into a decrement loop, because this is determined by the hardware characteristics of the processor.


PIC is not a very smart C compiler, so the human brain is still the first priority. Mastering some experience is helpful in writing efficient and concise code.

Reference address:PIC microcontroller C language delay program and loop subroutine implementation method

Previous article:How to implement ICSP connection circuit of PIC18F4550
Next article:Introduction to the features and advantages of PIC microcontrollers

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
Guess you like

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号