4375 views|1 replies

1379

Posts

0

Resources
The OP
 

Flashing Lights [Copy link]

Here is a flashing light experiment procedure for everyone

As shown in the figure below: Connect a light-emitting diode L1 to the P1.0 port , so that L1 is constantly on and off.

2. Circuit diagram 

Figure 4.1.1

3. Hardware connection on the system board 

Eight LEDs have been connected to our "Programming Experiment Integrated Board" and you can do this experiment without any changes.

4. Programming content 

How can we make the light flash continuously? Actually, we need the light to be on for a period of time and then off for a period of time, that is, P1.0 needs to continuously output high and low levels. How can we achieve this requirement? Please consider whether the following instructions are feasible:

SETB P10

CLR P10

...

This is not feasible. There are two problems. First, the computer executes instructions very quickly. After executing SETB P10, the light is off, but after a very short time (microseconds), the computer executes the CLR P10 instruction again, and the light is on again, so it is impossible to tell that the light has been off. Second, after executing CLR P10, the SETB P10 instruction will not be executed again, so there will be no chance to turn off the light in the future.

  In order to solve these two problems, we can make the following assumptions. First, after executing SETB P10, delay for a period of time (a few seconds or a few tenths of a second) and then execute the second instruction, so that we can tell that the light has been off. Second, after executing the second instruction, let the computer execute the first instruction again, and keep going around in circles. We call it a "loop", and the task can be completed.

The following is a program (the numbers in brackets are for the sake of explanation and do not need to be entered):

; Main program:

LOOP: SETB P10 ; (1)

    LCALL DELAY ; (2)

    CLR P10 ; (3)

    LCALL DELAY ; (4)

    LJMP LOOP ; (5)

; Subprogram

DELAY: MOV R7, #250 ; (6)

D1: MOV R6, #250 ; (7)

D2: DJNZ R6, D2 ; (8)

  DJNZ R7, D1 ; (9)

  RET ; (10)

  END ; (11)

Analyze the first five instructions according to the above assumptions.

  The first one turns off the light, the second one should be a delay, the third one turns on the light, the fourth one is exactly the same as the second one, also a delay, and the fifth one should be to execute the first instruction. The implementation principle of the second and fourth instructions will be discussed later. Let's look at the fifth instruction first. LJMP is an instruction, which means transfer. Where does it transfer? It is followed by LOOP. Let's see where there is another LOOP. Yes, there is a LOOP before the first instruction, so we can intuitively know that it will transfer to the first instruction. The LOOP before the first instruction is called a label. Its purpose is to give this line a name for easy use. Does it have to be called LOOP? Of course not. What name to give is entirely up to the programmer. It can be called A, X, etc. Of course, at this time, the name after the fifth instruction LJMP must also be changed.

  The purpose of the second and fourth instructions is delay. How is it implemented? The form of the instruction is LCALL. This instruction is called a call subroutine instruction. Let's see what follows the instruction, DELAY. Find DELAY before the sixth instruction. Obviously, this is also a label. The function of this instruction is as follows: when the LCALL instruction is executed, the program will go to the program marked by the label after LCALL for execution. If the RET instruction is encountered during the execution of the instruction, the program will return to the instruction below the LCALL instruction to continue execution. From the instructions starting from the sixth line, we can see that there is indeed a RET instruction. After executing the second instruction, it will go to execute the sixth instruction, and after executing the 6th, 7th, 8th, and 9th instructions, it will encounter the 10th instruction: RET. After executing this instruction, the program will come back to execute the third instruction, that is, clear P10 to make the light on, and then the fourth instruction. Executing the fourth instruction means going to execute the 6th, 7th, 8th, 9th, and 10th instructions, and then coming back to execute the 5th instruction. The 5th instruction means that the program will go back to the 1st instruction to start execution. This cycle repeats, and the light keeps on and off.

  All the programs from the line marked with the DELAY mark to the line marked with RET are a delay program, which is about a few tenths of a second. As for the specific time, we will learn how to calculate it later. The last line of the program is END, which is not an instruction. It just tells us that the program ends here. It is called a "pseudo-instruction".

 

Design Method of Delay Program

Next, let's look at the meaning of other symbols.

DELAY: MOV R7, #250; (6)
D1: MOV R6, #250; (7)

D2: DJNZ R6, D2; (8)

DJNZ R7, D1; (9)    

RET; (10)


MOV: This is an instruction, which means to transfer data. Speaking of transfer, we all know that to transfer something, we need to transfer it from one person's hand to another person's hand, which means there must be a receiver, a transmitter and something. From the instruction MOV R7, #250, R7 is a receiver, 250 is the number to be transferred, and the transmitter is omitted in this instruction (note: not every transfer instruction will omit it, in fact, most data transfer instructions will have a transmitter). Its meaning is also very clear: send the data 250 to R7, so after executing this instruction, the value in the R7 unit should be 250. There is a # sign in front of 250, what does it mean? This # is used to indicate that 250 is the object being passed, not the transmitter. So what does MOV R6, #250 mean? There is no need to analyze it.

DJNZ: This is another instruction. Let's take a look at the two things following this instruction. One is R6 and the other is D2. We already know what R6 is. Let's check what D2 is. D2 is in front of this line. We have learned that this is called a label. What is the purpose of a label? It is to give this line a name. The execution process of the DJNZ instruction is as follows. It subtracts 1 from the value of the first parameter behind it, and then checks whether this value is equal to 0. If it is equal to 0, it will execute further. If it is not equal to 0, it will transfer to where? You may have guessed that it transfers to the place specified by the second parameter (please explain how this statement is executed in your own words). The final execution result of this instruction is to circle around 250 times.
 

 

After executing DJNZ R6, D2 (that is, after the value of R6 is equal to 0), the next line will be executed, that is, DJNZ R7, D1. Please analyze the result of this sentence by yourself. (Turn to execute MOV R6, #250, and the value in R7 is reduced by 1). Finally, the sentence DJNZ R6, D2 will be executed 250*250=62500 times. Why do we execute the same instruction so many times? It is for delay.
 


Timing analysis:

We introduced the delay program before, but it is not perfect, because we only know that the sentence DJNZ R6, D2 will be executed 62500 times, but how long will it take to execute so many times? Does it meet our requirements? We don’t know yet, so we will solve this problem below.

First, let’s raise a question: What is the most important thing in our school. (Bell) The principal can go on a business trip and the teacher can rest, but the school will be in chaos if there is no bell for a day. The whole school works in a unified and coordinated manner under the unified command of the bell. This bell rings according to a certain schedule, which we can call "sequence of timing". A unit composed of people must have a certain timing, and computers must have strict timing. In fact, a computer is more like a big clock. There are strict rules for when the minute hand moves, when the second hand moves, and when the hour hand moves. There can be no chaos. The things that computers have to accomplish are more complicated, so its timing is also more complicated.

We know that when a computer works, it takes instructions from the ROM one by one, and then executes them step by step. We stipulate that the time it takes for a computer to access the memory is called a machine cycle. This is a time base, just like we use "seconds" as our time base. Why not just use "seconds"? How good, very familiar. If we continue to learn, we will know that using "seconds" is not accustomed to it. A

machine cycle includes 12 clock cycles. Let's calculate how long a machine cycle is. Suppose a single-chip microcomputer works on a 12M crystal oscillator, and its clock cycle is 1/12 (microseconds). Its one machine cycle is 12*(1/12), which is 1 microsecond. (Please calculate the machine cycle of a microcontroller working on a 6M crystal oscillator).

 

As shown in the figure above , there are three quartz crystals on our test board. Through the selection switch, you can choose quartz crystals of different frequencies, and there is also a reserved external crystal oscillator socket. It is convenient for expansion. Here, we choose a 12MHz crystal oscillator. Therefore, 1 machine cycle is 1 microsecond



Among all the instructions of the MCS-51 microcontroller, some are completed relatively quickly, requiring only one machine cycle, while some are completed relatively slowly, requiring two machine cycles, and two instructions require four machine cycles. This is not difficult to solve, is it? The execution time of the instruction I asked you to sweep the floor is always longer than the instruction of wiping the blackboard. In order to keep the length of the instruction execution time constant, a new concept is introduced: instruction cycle. The so-called instruction cycle refers to the time to execute an instruction. INTEL gives its instruction cycle number for each instruction. Most of this data does not need to be remembered, but some instructions need to be remembered, such as the DJNZ instruction, which is a two-cycle instruction.

Let's calculate the delay just now. First, we must know the frequency of the crystal oscillator. Let's assume that the crystal oscillator used is 12M, then one machine cycle is 1 microsecond. The DJNZ instruction is a two-cycle instruction, so it takes 2 microseconds to execute once. A total of 62,500 executions, exactly 125,000 microseconds, or 125 milliseconds.

This post is from MCU

Latest reply

//p1 8 light flashing program#include //sbit L1=P1^0; void delay02s(void) //Delay 0.2 seconds subroutine { unsigned char i,j,k; for(i=20;i>0;i--) for(j=20;j>0;j--) for(k=248;k>0;k--); } void main(void) { while(1) { P1=0; delay02s(); P1=0xff; delay02s(); } }  Details Published on 2009-3-24 14:42
 

154

Posts

0

Resources
2
 
//p1 8 light flashing program#include
//sbit L1=P1^0; void delay02s(void) //Delay 0.2 seconds subroutine { unsigned char i,j,k; for(i=20;i>0;i--) for(j=20;j>0;j--) for(k=248;k>0;k--); } void main(void) { while(1) { P1=0; delay02s(); P1=0xff; delay02s(); } }
This post is from MCU

赞赏

1

查看全部赞赏

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list