Monolithic computer C language programming, all the experience is here ... __ programming

Source: Internet
Author: User
Tags function prototype volatile

In the process of this 8*8 key program, whether in the process of writing or referring to someone else's program, I found that some basic knowledge of C language and programming norms have a lot of places, some of their previous programming habits are not good, and some is the basic knowledge is not solid performance, so summed up.


one,. H The relationship between the file and the. c File:

So far, the written procedures are some very simple procedures, never thought to write their own. h file, also do not know. H file exactly what to use, and the. c File what relation. Just recently write keyboard program, refer to other people's program, find others write strict procedures with a "KEY." H ", inside defined. c file in the use of their own written functions, such as Keyhit (), Keyscan () and so on.


After searching for information, I know. H file is the head file, it is the meaning of the heads, this is the need to standardize the structural design of the program, not only can realize the modularization of large programs, but also can realize the connection debugging of the root modules.


1,. H file Description:

In the SCM C program design, the project generally according to the function of modular structure. A project is divided into several functions, each function of the relevant program is placed in a C program document, called a module, the corresponding file name is the module name. A module usually consists of two documents, one for the header file *.h, the data structure and function prototype in the module, the other is the C file *.c, the data instance or object definition, and the function algorithm is implemented concretely.


2,. The function of the H file

As a project design, in addition to a detailed description of the overall function of the project, it is a detailed definition of each module, that is, give all the module header files. Normally the H header file defines the functions of each function in the module, as well as the requirements for input and output parameters. The specific implementation of the module, the project composition according to the H file design, programming, debugging completed. For confidentiality and security, the module is implemented to be available to other members of the project in the form of a connection file obj, or library file lib. Due to the need not to provide source program documentation, on the one hand can be published publicly, to ensure the ownership of developers, on the other hand can prevent others intentionally or unintentionally modified to create inconsistency, resulting in version confusion. So the H-header file is the detailed design of the project and the basis of Team work division, but also the function description of the module test. To refer to data or algorithms within a module, simply use a module h header file containing include.


3,. The basic composition of the H file

/* The following is a keyboard-driven header document * *

#ifndef _key_h_//Anti-duplicate references, if _key_h_ is not defined, compile the next sentence

#define _KEY_H_//This symbol is unique, meaning that the symbol is defined only once, that is, #i nclude _key_h_

/////////////////////////////////////////////////////////////////

Char keyhit (void); Keystroke No

unsigned char keyscan (void); Fetch key value

/////////////////////////////////////////////////////////////////

#endif


second, try to use the macro definition #define

Start to look at someone else's program, found that the beginning of the program, in the file contains a lot of #define statements, at that time, to do so many identifiers replacement to replace to go, hemp not trouble ah, completely did not understand the benefits of this formulation. Originally, the use of a symbol to express the constant, is conducive to future changes and maintenance, as long as the change in the beginning of the program, the program all the use of the place to modify all, save time.


#define KEYNUM 65//Key number, for Keycode[keynum]

Number of LineNum 8//keyboard lines #define

Number of keyboard columns #define ROWNUM 8//


Notice of the place:

1, macro name generally used in uppercase

2, the macro definition is not a C statement, the end without a semicolon


third, do not randomly define variable types

Before writing a program, when a new variable is needed, whether within the function or outside the function, directly at the beginning of the program definition, although not a principle error, but it is not desirable.


Here are some ideas about the types of variables in C language:


From the scope of the variables, divided into local variables and global variables:


1, global variables: is defined outside the function variables, like I previously defined at the beginning of the program variables are global variables, here I made a big bogey, using too many global variables.

There are two problems: first, the global variable in the process of all the implementation of the resource consumption; second, the global variable makes the program's versatility worse because global variables are one of the reasons for coupling between modules.


2. Local variables: variables defined within a function that are valid only within the function.


There are two types of time from the variable value of a variable:

1, static storage variables: The program runs during the allocation of fixed storage space.


2, dynamic storage variables: When the program is running, dynamically allocate storage space as needed.


Including four storage modes: auto static register extern

1, local variables, do not add the default for the auto type, that is, dynamic storage, if not assigned initial value, will be an indeterminate. When a local variable is defined as a static type, its value is invariant within the function, and the initial values default to 0.


static unsigned char sts;//key state variable

static unsigned char nowkeycode;//the key code at this time

Static unsigned char prekeycode;//previous key code

static unsigned char keydowntime;//rectangular keyboard Press down shake time variable

static unsigned char keyuptime;//rectangular keyboard release shake time variable

static unsigned char onoffdowntime;//shutdown key Press down shake time variable

static unsigned char onoffuptime;//shutdown key release shake time variable

static unsigned char onoff_10ms; To determine the number of shutdown key interrupts, the cumulative 150 times is approximately 3S, because there are two 10ms interrupts


2, global variables, compile-time allocation as a static storage area, can be referenced in the various functions in this file. If you have multiple files, if you refer to a variable in another file in one file, use an extern description in this file. However, if a global variable is defined as static, it can only be used in this file.


Iv. use of the Special keyword const volatile

1. Const

Const is used to declare a read-only variable

The const unsigned char a=1;//defines a=1, and the compiler does not allow you to modify the value of a

Role: Protect parameters that you 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};//line output encoding

const unsigned char row_in[rownum]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//column input encoding


2, volatile

A variable defined as volatile means that the variable may be unexpectedly altered so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully reread the value of the variable every time it uses it, instead of using a 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 intention of the program is to call the DoSomething function in main when the isr_2 interrupt is generated, but since the compiler determines that I is not modified in the main function, it is possible to perform only one read operation from I to a register, and then every if judgment only uses the "I copy", causing DoSomething to never be invoked. If you add volatile to the variable, the compiler guarantees that the read-write operation on this variable will not be optimized (definitely executed).


Generally speaking, volatile is used in several places as follows:


1, the interruption of the service program for other procedures to detect the variables need to add volatile;

2. The volatile should be added to the signs shared between tasks in a multitasking environment;

3, memory mapping of the hardware registers are usually added volatile description, because each of its read and write may be different meanings.


1. An embedded veteran's heartfelt words: empty cavity blood is still far from enough.

The 2.Linux Foundation opens the ACRN management program for the Internet of Things and embedded device design.

3. Detailed development experience of embedded Linux engineers

4. What is a C-language portability.

5. Block chain technology's most valuable destination: Internet of Things

6. For a full eight years of power, but said that they did not get started, what happened.

Disclaimer: This article is a network reprint, the copyright belongs to the original author. If you are involved in copyright issues, please contact us and we will confirm the copyright and pay royalties or delete the content according to the copyright certificate you provided.

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.