01控制一个LED 灯的亮和灭(Control an LED light and out)
01控制一个LED 灯的亮和灭(Control an LED light and out)
The first lesson, to understand the microcontroller and microcontroller control principle and the use of DX516, to control the brightness of a LED light and put out
Contents of this chapter:
The basic principle of SCM, how to use the DX516 simulator, and how to program a LED lamp lit out, how to enter the KEILC51uV
Debugging environment, how to use single step, breakpoint, full speed, stop debugging methods
Nie Xiaomeng June 2006
SCM is now becoming more and more popular, learning the upsurge of single-chip also came, many people because of work needs or personal interests need
To learn microcontroller. It can be said that master SCM development, there is more a rice bowl.
51 SCM has more than 30 years of history, in China, the University of SCM courses are mostly 51, and 51 after so many years
Development has also grown in many ways, with many improvements in functionality and many branches. And domestic Bookstore SCM rack,
Most of them are in the 51 series. Predictably, the 51
microcontroller will only be more and more in the market, the function will only become more and more rich, predictable
It will not be possible for decades to disappear.
As a beginner, how to enter the microcontroller? What knowledge and equipment do you need? Knowledge, in fact, does not require much, will be simple
C language, I know the basic structure of 51 SCM on it. Regular college graduates are able to get started on these 2 courses independently
High school students are qualified.
Even if you have not learned SCM courses, only mastered the C language fur, through this series of tutorials, you will gradually enter the microcontroller
Gate. Of course, in the process of learning, you still have to study more books SCM, to understand their basic structure and working style.
Next to 51 as an example to understand what the microcontroller is, what is the principle of control?
In digital circuits, there are only two cases of voltage signals. High and low levels are recorded digitally, and they are 1 and 0. SCM internal
The CPU, registers, buses, and so forth are operated by 1 and
0 two signals, and the data is saved by 1 or 0. Singlechip
The input and output pins, which are the IO ports, also only output or recognize 1 and 0 two signals, that is, high and low level. When single chip microcomputer transport
After one or a set of level signals to the IO port, the external device can read these signals and perform corresponding operations
Department control. When an external or a set of level signals are sent to the IO port of the microcontroller, the microcontroller can also read the signals and analyze them
Yes, this is the microcontroller to read the external signal. Of course, in actual operations, these signals can be very complex and must be strictly followed
Specified time sequence (sequential) input and output. Each device also has its own timing, so it can be controlled as long as it is strictly observed
Equipment, do whatever you can imagine.
You might ask again, how can I get the microcontroller to control and analyze the external equipment? The answer is the program, you can write the relevant procedures, and
And burn them to write the program space inside the monolithic integrated circuit, when the single chip microcomputer is on the electricity, will step by step according to the procedure
that you write to carry out the instruction, do
What do you want to do?.
In the 51 standard chip, there are 32 input and output IO, divided into 4 groups, each group of 8, respectively, P0 mouth, P1 mouth, P2 mouth, P3 mouth.
The 8 feet of the P1 mouth are represented by P1.0 to P1.7, and the rest are similar. 51 is to use these 32 mouths to complete all external operations. For 51
The internal structure, if you know it, is the best; if you don't understand, you can put it down first, after completing a few chapters of the tutorial,
You'll be interested in reading your own materials. Of course,
If you want to be a good programmer for MCU development, you still have to
Must be familiar with the internal structure of the microcontroller and working principle, must not be lazy!
In this chapter, you will use the program to control the brightness and extinction of an LED led. You should know that the LED light tube is passing through a certain current
Bright, dead electricity. In order not to burn the LED through too much current, we also have to play an upper limit current resistor. 51 of IO is weakly pullup
Mode, in the output of high power, usually only output dozens of a small amount of current to the ground, and in the output of low power, VCC power input can be tens of Ma
Current to IO. General LED requires 10 mA or so current lighting, we will LED connected to the power VCC and IO port, the middle string
On the resistance, when the IO output is low, the light will be on, and conversely, the lights will go out. What we want to control in this program is P1.0. Please refer to
The circuit diagram of the test board we are going to use is that the breadboard is presented at the purchase of the dx516 emulator.
Figure 1, test circuit diagram
Figure 2: test board appearance
The following describes the emulator and simulation environment.
In the actual microcontroller learning and development, you can use the simulator to simulate a CPU chip, so that it works in accordance with the procedures you wrote, and
And debugging, step by step to exclude the program bug, so that the program works properly. After the program is working, you can write the program that you write with the writer
Burn the chips into the microcontroller and let it run itself.
To use an emulator, you have to have a compiler debugging environment that runs on a computer and we write it on a computer
The debugger and the computer are connected to the emulator, and all the data and programs in the emulator can be observed and viewed from the computer
Observe variables, write values of variables, step into the debugger, set breakpoints, debug, run at full speed, stop programs, and so on.
We use the world's most advanced keilC51 compiler debugging environment, the simulator using (shrimp electronic network)
Designed DX516 Professional Edition simulator, the simulator is fully functional, cost-effective, is a good tool for learning and development!
You can download the relevant Chinese language information about keilc51 in the data download section of this page
This information details how to use C51 programming and how to debug using the Keil uV2 environment. Please test, complete, or experiment in this chapter
In the process, if you do not understand the place, you must
take time to read!
You should also find the way to download the cracked version of keilc51 in the pages above. Chinese law stipulates that in the study and research work
It's OK to use copyrighted software, but if you're developing a product, you'd better buy a genuine software.
Here is an introduction to the use of the DX516 emulator:
1. install
Assemble the emulator and the test board as shown in Figure 3. The serial line is inserted into the emulator in the correct direction, and the other end is connected to the serial port of the computer. Please try
Hard serial ports using computers.
The jumper on the left of the emulator base is placed on the side of EMB to enter the simulation state. If placed on the RUN side, it will be shipped offline
Row status.
Select the crystal jumper, please put it on the side of IN to use the internal crystal oscillator of the simulator, and the internal crystal oscillator is more reliable. If it's on OUT's side, then
External user board oscillator will be used.
Fig. 3 the emulator is inserted on the test board
2. Power Supply
Because the user board uses little current, you can use the USB to power the USB. The maximum current of the USB can be supplied by 500mA, and the electronic board is inserted into the computer
In the mouth of usb. (in practice),
If the user board uses current over 100mA, we recommend the use of external power supplies
3. start-up
When the emulator is powered, or the button on the emulator is pressed, the emulator will issue "beep", indicating that the emulator will start normally. meanwhile
The light above the emulator flashes once, indicating normal emulation.
4. Simulation settings
First setting:
C51 users, please add a sentence in front of the main () function of your code:
Char, code, dx516[3], _at_, 0x003b;
If the above settings you have not done, during the loading process, the simulator will send "beep beep" three short sound alarm, then the simulation results will be
May not be correct.
In our routine, this sentence has been added. This sentence will not affect the work of the program, you can always retain.
Second settings:
In the hardware emulation settings option, select serial interrupt and tick in the front.
If the above settings you have not done, during the loading process, the simulator will issue "beep" a long sound alarm, then the simulation results will be available
Can it be incorrect?.
Rest settings:
Select use keil Monitor-51 Driver so that hardware emulation is used
Select loadApplication at start to load the program directly at startup
Please select Go till main and run directly to the main function
after loading
In the hardware emulation settings option, select the 115200bps baud rate, and all cache may not be selected, or only cache code. with
Please select the correct serial number.
Figure 4 Simulation Settings
OK, now we can start the experiment. We open the established project and write the program test. By the way, I'll also learn about program tuning
Try skills. As to how to build a new project, please refer to C51's help file, or try it yourself, WWW.DAXIA.COM's DX516
There are also steps in the column to teach you how to do it for the first time..." The article can learn.
Double click the lesson1.uv2 in the lessoncode01 directory and open the following interface as follows:
Figure 6: program interface
This interface is uV3, and it's the same as uV2.
Click on the third or third buttons in the figure above. (your compiler button position is not necessarily at that location, and look for it yourself), you can see the second
To compile the results. The above shows 0errrs, 0warnings, which is the best compile result. If you have error, you can't do the next simulation, if
There are warning, we must try to eliminate, really can not be eliminated, but also to confirm that it will not affect the program before proceeding to the next simulation.
In the compilation result, we can also see how many bytes have been reported by data, XDATA, code and so on. We should pay attention to whether there is one in your SCM
So many resources, if not enough, will cause problems in the future. For example, AT89C51's program space is 4K, XDATA, if not outside
Expansion is 0, and data is the 128. Beyond these limits, the program cannot run in AT89c51. Different chips have different capacities, such as
SST89E516RD has the 64K program, the internal 768 byte XDATA, and the 256 byte data. We must have considered these in our routines
Yes, it certainly won't go beyond, because the DX516 emulator has the same capacity as SST89E516RD, so it will be noticed in the future.
Here we deliberately write the P10 of the ninth line as P11 and compile the point, because there is no predefined P11, so we report the error, as follows
Figure:
Double click on the line in the error report, and the window will jump to this row,
It is convenient for you to make changes. Well, now, please correct the mistake and make up again
Once the report is correct, the following simulation begins.
Click on the first two lines, fifth in a magnifying glass, inside a d letter button, you can enter the simulation, the simulator should be connected in advance yo. Get into
After simulation, you need to click the button to exit the simulation environment. Note that if the program is running at full speed, the simulation environment cannot be pulled out directly,
You must quit at first and then click the simulation button before you can quit.
Point to the simulation button, the program starts loading, the PC automatically runs to main (), stops and points to the first line of the main () function.
Here is a screenshot of the simulation environment:
By the way, introduce the button on the debug interface:
Figure 5: button description
After entering the simulation window, if the window does not appear in the source code window, but the window with the disassembly code, the window will be turned off directly
Revert to the code window. The next entry will also go directly to the source window.
Now experiment with a single step, a single step (two single steps, a single step at a general point). You can see the light on. The PC pointer also points to the next one
Program line.
Picture, light on
Click again, and PC goes the next step. The light goes out.
Once again, PC goes to the pending program and continues. The point is still in this line. This instruction is actually to keep the program jumping to itself
OK, nothing else. Commonly referred to as program pending.
In general, programs in real applications don't hang, and generally do a big loop in the main function, as follows:
Void main (void) / main program
{
While (1)
{
P11=0; / / Lighting
P10=1; / / off
}
}
Please change the main function program to the above code, and we will test the breakpoint operation next.
Compiled results are as follows:
Figure:
After entering simulation
Figure:
You can see the following prompt: "connected to Monitor-51 V.DX", followed by "V.DX", is already connected to the prawns
,
Emulator hints. V.DX is a unique signature of a shrimp simulator.
In the fifteenth row double click on, you can see the program for the left there is a red box, which is to set a breakpoint, and then double-click a breakpoint.
It's canceled. If the program encounters a breakpoint at full speed, it automatically stops and analyzes. Note that after entering the simulation, and
When the program is stopped, you can set or cancel the breakpoint.
Graph: set breakpoint
Now that you're running at full speed, you can see that the program has stopped at the breakpoint, and that the light is just because the previous command has just executed the lights
Lighted.
Now set the breakpoint in the fourteenth row and cancel the last breakpoint.
Figure: setting another breakpoint
Now running at full speed, can see the program stops at the breakpoint, and because just executed the lights, the lights were out.
OK, now test full speed running and stop.
Cancel the breakpoint, and then run at full speed, you can see the light is on, but not very bright, because the program is cyclic, bright, alternating into
OK, the bright time is not all the time.
Now that the point is stopped, you can see that the program is stopped and repeated several times at full speed and stop, and you can see that each stop is not necessarily the same
The.
The lesson was over, and we learned how to light up and some basic compilation and debugging operations,
The next lesson will learn how to make LED blink,
And more debugging methods, and how to view the running status and set the value of the internal register.
Homework:
Switch to second LED lights (P1.1) and finish the test in this chapter.