During this 8 * 8 button, no matter whether it is written or referred to the procedure of others, it is found that there is a lot of basic knowledge points and programming specifications for C language, and some are their previous program habits. Ok, some is the basic knowledge is not solid, so it is concluded.
First, .h file and .C file relationship:
So far, the procedures written are some very simple programs, never think of writing .H files yourself, and don't know what. What is the use of .C files. Just recently write keyboard programs, when referring to other procedures, find a strict program written by others with a "key.h", defined in the .C file to write the function, such as Keyhit (), Keyscan ()Wait.
After finding the information, .h file is the header file, it is estimated that it means to mean. This is the need for structural design of the specification program, which can achieve the modularity of the large program, and the connection debugging of the root module can be realized.
1, .h file introduction:
In the single-chip C program design, the project is generally modularly designed according to functional modularization. Divide a project into multiple functions, and each function is placed in a C program document, called a module, the corresponding file name is the module name. A module is usually composed of two documents, one is a header file * .H, describes the data structure and function prototype in the module; the other is C file * .c, definition of data instance or object, and function algorithm accomplish.
2, .h file role
As a project design, in addition to the detailed description of the project's overall function, the detailed definition of each module is to give the header file of the module. Usually the H header files want to define the functions of each function in the module, and the requirements for input and output parameters. The specific implementation of the module is designed, programmed, and debugged by the project composition according to the H file. For confidentiality and security, the module is implemented to be available to the project other member using the accessible file OBJ, or the library library. Since there is no need to provide source program documentation, on the one hand, it can be publicly issued, to ensure the ownership of the developer; on the other hand, it can prevent someone from intentional or unintentional modification to generate non-consistency, resulting in version confusion. Therefore, the H header file is the basis for the detailed design of the project and the basis of the team's work division, and the function description of the module is tested. To reference data or algorithms within the module, just use the include the module H header file with the include.
3, the basic composition of .h file
/ * As shown in the keyboard driven header * / / anti-repertrous reference, if it is not defined _key_h_, compile the next sentence #define _key_h_// This symbol is unique, indicating that as long as the reference is once, ie #i nclude , Define symbols_Key_H_////////////////////////////////// //////////////////////////bit value ////// //////////////////////////////////////> ///////// # Endif
Second, try to use macro definition #define
When I started watching the program, I found the beginning of the program, there were many #define statements behind the file included, I thought that so many indicators were replaced, the Ma will not bother, did not understand the benefits of this way of writing. It turns out that a constant is represented by a sign, which is conducive to future modification and maintenance, as long as the program starts to change, all the places used in the program are all modified, saving time.
#define keynum 65 // Number of keys, used for keycode [keynum] #define Linenum 8 // Keyboard Row #define rownum 8 // keyboard column number
Note place:
1, macro name generally use uppercase
2, macro definition is not a C statement, ending does not add the semicolon
Third, do not define variable types
Previous writes, when a new variable is required, no matter how function is still in the function, it is directly defined in the program, although it is not a principled error, but it is very uncomfortable.
Let's talk about the concept of variable types in C language:
From the scope of the variable, it is divided into local variables and global variables:
1. Global variable: It is a variable defined outside the function. It is a global variable that I used to define the beginning of the program. Here I made a taboo and used excessive global variables.
There are two questions: First, the global variable takes up resources during all the execution of the program; the other, the global variable is too much to make the process's versatility variable, because global variables are one of the reasons for the inter-module coupling.
2. Local variable: variables defined inside the function are valid only in the function.
The time exists from the variable value of the variables is divided into two types:
1. Static storage variable: Assign fixed storage space during program operation.
2. Dynamic Storage Variables: Dynamically allocate storage space as needed during operation.
Suites also include four storage methods: Auto Static Register Extern
1. Local variables, do not add default to the AUTO type, that is, dynamic storage, if not assigned, will be an uncertain value. If the local variable is defined as a Static type, its value is constant in the function, and the initial value defaults to 0.
Static unsigned char STS; // button status variable
Static unsigned char nowKeycode; // That time
Static unsigned char prekeycode; // last key code
Static unsigned char keydowntime; // Rectangular keyboard Press the time variable
Static unsigned char keyuptime; // Rectangular keyboard release debounce time variable
Static unsigned char onoffdowntime; // Press the time variable
Static unsigned char onoffuptime; // Shuttles to release the time variable
Static unsigned char onoff_10ms; // Judgment the shutdown button interrupt number variable, accumulated 150 times is approximately 3s, because two 10ms interrupts have been entered before and after
2, global variables, compile time to static storage area, can be referenced by each function in this document. If you are multiple files, if you reference the variables in another file in a file, you want to use extern descriptions in this file. However, if a global variable is defined as static, it can only be used in this file.
Fourth, the use of special keyword const volatile
1, Const
Const is used to declare a read-only variable
Const unsigned char A = 1; // Definition a = 1, the compiler does not allow the value of A.
Role: Protect the parameters that do not want to be modified
Const unsigned char key_code [keynum] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
0x41
}; // key code
Const unsigned char line_out [linenum] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xDF, 0xBF, 0x7F}; // Row Output Code
Const unsigned char row_in [rownum] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xDF, 0xBF, 0x7F}; // Enter the encoding
2, Volatile
A variable defined as Volatile is that this variable may be unveasuating, so that the compiler will not assume that this variable is worth the value. Precisely said that the optimizer must reread the value of this variable every time when used this variable, rather than using the backup stored in the register.
Static INT I = 0; int main (void) {. ..WHILE (1) {if (i) dosomething ();}} / * interrupt service routine. * / void ISR_2 (void) {i = 1;}
The original meaning of the program is to call the DOSMETHING function during the main display, but because the compiler determines that there is no modification in the main function, it may only be executed once to the read operation from I to a register, and then each time IF judgment only uses "I copy" in this register, causing DOSMETHING never call. If the variable will be added to Volatile, the compiler guarantees that the read and write operations for this variable will not be optimized (affirmation).
Generally speaking, Volatile is used in the following places:
1. The variables modified for other programs in the interrupt service program requires plus Volatile;
2, the logo of each task between the multi-task environments should be added to Volatile;
3. The hardware register of the memory maps typically also adds to the Volatile description because each time you read and write it.
Responsible editor LK, read full text
Our other product: