Single-chip microcomputer tutorial 4. C Language Foundation and realization of running lights

Source: Internet
Author: User
Tags bitwise

Single-chip microcomputer tutorial 4. C Language Foundation and realization of running lightsC language, people who have not touched the computer programming language will see it very mysterious, it feels very difficult, and in my opinion, C language logic and operations, is the primary level, so we do not fear it, I as far as possible from the primary mathematical logic way with everyone to learn C language. 1.1 binary, decimal, and hexadecimal

Seemingly very simple things, but from the students to learn the situation of the video, many students can not completely understand. Here is a brief introduction to some considerations, and then the experiment will be more profound explanation.

1, the decimal does not say more, every ten, a bit has 10 values: 0~9, our life everywhere is its figure. Binary is the binary, it is a bit only two values: 0 and 1, but it is the most basic theoretical basis for the realization of computer systems, computers (including single-chip microcomputer) chip is based on the millions on the hundreds of millions of switch-tube combination, each of them can only have open and close two states, It's hard to figure out the third State (don't justify half-open half-state, it's unstable, it's strongly avoided), so they can only correspond to binary 1 and 2 values, not 2, 3, 4 ... understanding the binary is helpful in understanding the nature of the computer. The binary data should be written with a prefix of 0b, and the value of each bit can only be 0 or 1. Hexadecimal is the combination of 4 bits to represent, so it each has 0b0000~0b1111 a total of 16 values, with 0~9 plus a~f (or a~f), then it is naturally 16 carry, it is essentially the same as the binary, is a binary abbreviation form, is also a common form of our program writing. The hexadecimal data is written with a prefix of 0x, the following table is the corresponding relationship between three kinds of binary.

Decimal

Binary

Hexadecimal

0

0b0

0x00

1

0b1

0x01

2

0b10

0x02

3

0b11

0x03

4

0b100

0x04

5

0b101

0x05

6

0b110

0x06

7

0b111

0x07

8

0b1000

0x08

9

0b1001

0x09

10

0b1010

0x0A

11

0b1011

0x0B

12

0b1100

0x0C

13

0b1101

0x0D

14

0b1110

0x0E

15

0b1111

0x0F

16

0b10000

0x10

17

0b10001

0x11

18

0b10010

0x12

......

......

......

Table 4-1 Conversion in binary

2, for the binary, the 8-bit binary we call a byte, the binary expression range value is from 0b00000000~0b11111111, and our program in hexadecimal notation is from 0x00 to 0xFF, here teaches you a binary conversion decimal and hexadecimal method , binary 4-bit a group, follow the law of 8,4,2,1 such as 1010, then from the highest bit, the number size is 8*1+4*0+2*1+1*0 = 10, then the decimal is 10, hexadecimal is 0xA. In particular, when binary to hexadecimal, a hexadecimal one is exactly the same as the binary 4-bit correspondence, these people do not need to force memory, with a few times on the proficiency.

3, for the system, is only the manifestation of the data, and the size of the data will not be different because of the form of the binary representation, such as binary 0b1, decimal 1, hexadecimal 0x01, they are essentially the same data size equal. We are in the C language programming, we only write decimal and hexadecimal, then no 0x is the decimal, with the 0x symbol is hexadecimal.

1.2 C language variable types and ranges

What is a variable? The variables are natural and constant relative. Constants such as 1, 2, 3 ... such as fixed numbers, and variables, and our primary school of X is a concept we can make it is 1, also can let it is 2, we want it to be a few of our program to decide.

So in our elementary school math, there are a few classes, positive, negative, integer, and Decimal. In the C language, the names are different from what we learned in math, and we limit the size of the data. This place is a little bit more complicated, the data range in C51 is not exactly the same as other programming environments, so the graph below is just about C51, and the other programming environment may be different, and you know it's okay.

The basic types of data in C can be divided into integer, character and floating-point type, 4-1

Figure 4-1 C language data type

In Figure 4-1, there are three basic types, each of which contains two types. The characters and integers, in addition to a certain range of data size, can only express an integer. and unsigned type can only express positive, to express negative numbers must be used signed type, express decimals, must use floating-point type.

For example, the last lesson of the flashing light of the program, we use the unsigned int i = 0, the scope of I is 0~65535, the wording of our for statement, if the 30000 changed to 70000, for (i=0;i<70000;i++); People will find that the lantern is always on, not flashing, and that understanding of the problem, of course, we want to understand the use of the For statement.

Here is a programming purpose, is to use small not big. That is, the definition can be used 1 words, it is not defined as an int, on the one hand to save RAM space can be used for other variables or intermediate operation process, on the other hand, the small space program operation speed is also faster.

1.3 For Loop statement

The For statement is a commonly used statement of our future programming, which must be learned to use, not only to delay, but also to perform some cyclic operations. The general form of the for statement is as follows:

for (expression 1; expression 2; expression 3)

(statements that need to be executed);

Its execution is: expression 1 executes first and executes only once, then executes expression 2, which is usually an expression used to determine the condition, if the expression 2 condition is true, executes (the statement that needs to be executed), then executes the expression 3, then the expression 2, and then the expression 3 ... Until expression 2 is not true, jump out of the loop and go down. As an example:

for (i = 0; i<2; i++)

j + +;

Here is a symbol + +, which indicates the meaning of add 1. If the initial value of J is 0, first execute the expression 1 of the i=0, and then judge I less than 2 this condition is set up, the execution of a j++,j value is 1, and then after the expression 3, I is the value of 1, and then determine the condition 2, or conform, J Plus once, J becomes 2, the expression 3 I also become 2 , and then judge the condition 2, found 2<2 This condition is not established, so will not execute J + + this statement. So after execution, the value of J is 2.

For the For statement in addition to this standard usage, there are several special usages of our last lesson flashing light for the use of the For statement (i=0; i<30000; i++); We do not add (need to execute the statement), without adding, is nothing to operate. But nothing to operate, we this for the statement loop judgment 30,000 times, program execution will use the time, so it played the role of delay. For example, we change the 30000 to 20000, we will find that the flashing speed of the lights, because we delay time is short, of course, we should be 40000 will find that flashing slow. But a little special attention, C language delay time is not through the program to see, and will not be proportional, such as if we this for loop inside the expression 2 use 30000 time delay 3 seconds, then the time delay 40000, may not be 4 seconds, then how to see the actual delay time it? I'll teach you later.

There is another way to write for (;;), so that after this is written, the For loop becomes a dead loop, and the execution (the statement that needs to be executed) is the same as the one we talked about while (1). And how is this syntax used?

1.4 While Loop statement

In our Microcontroller C language programming, each program we will be fixed to add a while (1), this statement can play the role of the Dead loop. For the while statement, his general form is:

while (expression)

{

loop body statement;

}

In C language, usually the expression conforms to the condition, we call the true, does not meet the condition, is called false. For example, the front i<30000, when I equals 0, then this condition is true, if I is greater than 30000, the condition is not established, called false.

while (expression) the expression in parentheses, when True, executes the loop body statement, which is not executed when it is false. Here is not an example, the later encountered when the detailed description.

There is another situation, that is, we C language inside, in addition to expressions, there are constants, in practice, we are not 0 of the constant is considered to be true, only 0 think it is false, so we add a while (1), this number 1, can be changed to 2,3,4 ... and so on, it's a dead loop, Continuously executes the loop body statement, but if the number is changed to 0, then the statement of the loop body will not be executed.

1.5 Simple Introduction to Functions

The general form of a function definition is as follows:

function value type function name (formal parameter list)

{

function body

}

1. The function value type is the type of the function return value. In the back of our program, there are many functions that have return x, the return value that is the type of the function itself. Another situation is that this function only performs operations, does not need to return any value, then its type is empty type void, this void can be omitted, but once omitted, Keil software will report a warning, so we usually do not save.

2, function name. Can be any valid identifier, but cannot be the same name as any other function or variable, and cannot be a keyword. What is the keyword, behind our slowly contact, such as char, such as, are keywords, is our program has a special function of the marker, this thing can not be named function.

3, the form parameter list, we also called the formal parameter, this is the function call time, passes the data with each other. Some functions, we do not need to pass parameters, then can be substituted with void, void can also be omitted, but the parentheses cannot be omitted.

4, function body. The function Body contains the declaration statement part and the EXECUTE statement part. The declaration statement section is mainly used to declare the variables used inside the function, and the execution statements are mainly the statements that some functions need to execute. In particular, it is important to note that all statement statements must be placed before the execution of the statement, otherwise the error will be compiled.

5, an engineering document must have and can only have a main function, when the program executes, it starts from the main function.

6, about the concept of formal parameters and real parameters, we summarize the back, if encountered in the program, we will follow the copy for a period of time. First use, after the explanation, this is more conducive to understanding.

Let's go back to our last lesson. Flashing LED Program section

void Main ()//void is the function type

{

unsigned int i = 0; Defines an unsigned integer i with a variable range of 0~65535

and assign an initial value of 0

enled = 0; First define the variable i, then write the execution part

ADDR0 = 0;

ADDR1 = 1;

ADDR2 = 1;

ADDR3 = 1; 74hc138 Open Transistor

while (1)//program Dead Loop

{

LED = 0; Light Small Light

for (i=0;i<30000;i++); For delay operation

LED = 1; Turn off the small lights

for (i=0;i<30000;i++); For delay operation

}

}

1.6 Keil Software delay

C language commonly used delay method, there are the following 4 kinds of

Figure 4-2 C language delay

Figure 4-2 is one of the 4 time-delay methods commonly used in our programming language, two of which are imprecise delays, two of which are accurate. Both the for statement and the while statement can change the delay time by changing the range value of I, but the C language time is not visible through the program.

Accurate delay has two methods, one method is to use the timer to delay, this method we have to introduce the course in detail, the timer is a single-chip computer focus. The other is to use the library function _nop_ (); A NOP time is the time of a machine cycle, which is also described behind.

Non-precision delay, just in we do some simple, such as small lights flashing, running lights and other simple experiments to use, and actually do the actual development program in fact this kind of non-accurate delay with very little, here we just do demo function use.

Well, the introduction is over, we are going to combat. The last lesson of the LED light flashing program, we use the delay mode is for (i=0;i<30000;i++); If you change this here to 100, download into a single-chip microcomputer, will find that the light has been bright, not flashing state, now we all change this program, all changed to 100, Then download and observe the phenomenon before continuing.

After the observation, there is no doubt that the actual phenomenon is in accord with the theory I mentioned. Here is an introduction to common sense. Our human eye has a minimum resolution for flashing light, and usually the signal we see when the flicker frequency is higher than 50Hz is always bright. That is, the time delay is less than 20ms, our naked eye is not able to tell the light is flashing, it is possible to see the most is a small light bright and dark slightly changed a bit. To clearly see the light flashing, the value of the delay must be a little larger, to what extent, different brightness of the lamp is not exactly the same, we can do their own experiments.

So how do you see how long it takes to delay? Everyone mouse point Keil project-->options for Target ' Target1 ', or point Target1 right icon, go to Settings option, 4-3 shown

Figure 4-3 Options for Target

First we open the Target tab and find the Xtal (MHz) in the inside, which is the crystal oscillator option to fill in our simulation time, which we can see from our schematics and the board. Our microcontroller with crystal oscillator is 11.0592MHz, so this place we have to fill in 11.0592. Then find debug this option, select Use Simulator on the left, then click on the bottom of OK on it, 4-4 is shown.

Figure 4-4 Debug Configuration information

Click on the Debug menu in the Start/stop debug Session, or the mouse click on the left side of the debug icon, will enter a new page, 4-5 shows.


Figure 4-5 Debug Window display

The leftmost column is a microcontroller of some registers and system information, the top of that column is Keil C language into the assembly code, below is our C language program, there are various windows can open, in the View menu can open or close our various windows. In this class we only care about the window we need, other windows to use. So sometimes we think this distribution is not particularly good, so we want to change the window distribution how to do? For example disassembly (assembly) window, we first drag it with the mouse, and then there will be a direction symbol, and then with the mouse point that direction symbol, he gave us a distribution, 4-6 shows.

Figure 4-6 Keil window Movement (i)

We click the arrow at the far right, and the window changes to look like the 4-7 figure. Or we can just turn it off if we don't use the assembly program.

Figure 4-7 shows the Keil window movement (ii)

Careful classmate will see in the C language program has a yellow arrow, this arrow represents the current location of the program, in this debug inside, we can see our program running process. In the upper left corner of the three icons, the first is reset, click once, the program will run to the beginning of the operation, the second icon is full-speed running icon, click on the program will run full speed running, the third icon is the Stop icon, when the program runs at full speed running up, We can stop the program by clicking on the third icon to see where the program is running. Click on the reset, we will find the C program on the left side of the gray or green, and some places still keep the original white, we can in our gray position double-click the mouse to set a breakpoint, that is, for example, a total of 20 lines, after the tenth line set breakpoints, point full run, the program will run to line tenth stop, It is convenient for us to observe the situation of running to this place.

Students will find that some locations can set breakpoints, and some places can not set breakpoints, this is why? Keil software itself has the ability to optimize our program, if you want to set breakpoints in all locations, you can set the optimization option to 0 position, that is, the program is not optimized. As shown in 4-8.



Figure 4-8 Tuning Option settings

The focus of this lesson is to see the C language code run time, in the leftmost register that box, there is an SEC option, this option is the single-chip computer running time statistics options, you click the Reset button, you will find this sec turned 0, and then we in the LED = 0; This sentence adds a breakpoint at the LED = 1; This position adds a breakpoint, we click the Full speed Run button, will stay directly at the LED = 0; we will see that our time changes to 0.000197 seconds, 4-9 as shown.

Figure 4-9 Breakpoint Settings

We'll run at full speed and we'll see the SEC become 0.07530650 seconds, so a for loop is about 75ms, and we can change the interval by changing the number 30000. Of course, we should pay attention to the variable range of I, if you write a value greater than 65535, the program will continue to run, because I no matter what changes, will not be greater than this value, if you want to be larger than this value, I must change the type of the definition. In the back if we ask to see how long a program is running, it can be seen in this way.

1.7 Running Lights Program

We learned a little light led light, and then learn the LED light flashing, below we want to learn more about how to make 8 small lights in turn a light, flow up.

Figure 4-10 led small lamp circuit diagram

Through the previous course, we can understand that the control pin P0.0 through the 74hc245 control db0,p0.1 control DB1 ... P0.7 control DB7. We also learned that a byte is 8 bits, and if we write a P0, it represents a total of 8 bits P0.0 to P0.7. For example, we write P0 = 0xFE, convert to binary is 0b11111110, so light LED light program, in fact, we can change to another way of writing, as shown below.

#include <reg52.h>

Sbit ADDR0 = p1^0;

Sbit ADDR1 = p1^1;

Sbit ADDR2 = p1^2;

Sbit ADDR3 = p1^3;

Sbit enled = p1^4;

void Main ()

{

enled = 0;

ADDR0 = 0;

ADDR1 = 1;

ADDR2 = 1;

ADDR3 = 1; 74hc138 Open Transistor

P0 = 0xFE;

while (1); The program stops here

}

Through the above program we can see that through the P0 to control all 8 LED lights and off. What do we do if we want to light and extinguish the bottom? From here you can get the method, if you want to let the single-chip computer running lights flow, in turn to achieve the result is: 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f.

In our C language, there is a shift operation, where << represents the left shift,>> represents the right shift. For example a = 0x01 << 1 is the result of a that is equal to the left shift of 0x01. Everyone notice that the shift refers to the binary shift, then the shift is over, originally in the No. 0 bit of the 1 moved to the first position, after moving the low is 0. So the value of a is ultimately equal to 0x02.

Also learn another operator ~, which is a bitwise inverse meaning, similarly, bitwise inversion is also for the binary. For example A = ~ (0x01), the binary of 0x01 is 0b00000001, the bitwise inverse is 0b11111110, then the value of a is 0xFE.

After learning these two symbols, then we can write the program of the running lights, first put the program.

#include <reg52.h>

Sbit ADDR0 = p1^0;

Sbit ADDR1 = p1^1;

Sbit ADDR2 = p1^2;

Sbit ADDR3 = p1^3;

Sbit enled = p1^4;

void Main ()

{

unsigned char j = 0;

unsigned int i = 0;

enled = 0;

ADDR0 = 0;

ADDR1 = 1;

ADDR2 = 1;

ADDR3 = 1; 74hc138 Open Transistor Q16

while (1)//program Dead Loop

{

P0 = ~ (0x01 << j + +); P0 equals 1 Left shift J bit, and J + +

for (i=0; i<20000; i++); Delay

if (j = = 8)//If J equals 8, re-assigns value to J 0

{

j = 0;

}

}

}

Here I only talk about two cases, when J equals 0, 1 left 0 or 1, then written in binary is 0b00000001, the number is reversed is 0b11111110, Bright is the right side of the small lights. When J equals 7, 1 left Shift 7 is 0b10000000, the 0b01111111 is reversed, and the left-most light is on. The middle process, let's analyze it ourselves.

After the end of the running lights, the explanation of the little lights, we are temporarily over, behind the high-level use of small lights, we will explain in detail.

1.8 Jobs

1, proficiency in binary, decimal and hexadecimal conversion method.

2, grasp the C language variable type and scope, for, while and other basic statement usage.

3, understand the basic structure of the function, can enter the program debug independently, a lot of hands-on operation, skilled Keil software environment of some basic operations.

4, the running water lamp left to understand thoroughly, the independent completion of the flow lamp right shift operation and the flow lamp first left to move after the right shift, such as simple pattern operation.

Single-chip microcomputer tutorial 4. C Language Foundation and realization of running lights

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.