Study notes | LED flashing based on Delay | Modular programming | SOS distress light | STC32G microcontroller video development tutorial (Brother Chong) | Episode 6 (Part 2): Realizing LED flashing

2 Use of functions

In modular programming, function usage is divided into the following three steps:

1. Function definition (requires type)

Return value Function name (entry parameter)
{ Function to be performed by the function } @Return value: No return value is void @Function name: Avoid keywords (blue in IDE), no repetitions, non-special characters can be taken @Enter parameters: Type + name, multiple parameters "," separate, write void if empty




2. Function declaration (requires type)

Return value function name (entry parameter);

3. Function call

Function name (entry parameter); when calling, you only need to enter the parameter, no need to bring the type,

3 Create a new file and use modular programming

Create new xxx.c and xxx.h files

Create new xxx.c and xxx.h files, representing a function block. One-to-one correspondence is recommended.
Click New, the default popup is a text file, undefined file type, click Save, save as math.c, and save as math.h:
Insert image description here

xxx.h format:

#ifndef __XXX_H
#define __XXX_H
Add fixed collocation format in math.h.

Call header file

Function declaration…
#endif

xxx.c format
#include "xxx.h"
function definition

Verification code

math.h:

#ifndef __MATH_H  //if not define
#define __MATH_H

int Add(int parm1,int parm2);

#endif

math.c:

int Add(int parm1,int parm2)
{
	return   parm1 + parm2;
}

Add a reference to main.c: #include "math.h".

transfer:

When adding a file, you must remember to reference the path and add it to the project.
Insert image description here
Save all, and double-click the icon to add math.c to the project,
Insert image description here
the compilation is complete, and it will be automatically downloaded to the development board. Open the CDC serial port tool, clear the data area, send the data, and get the printed value:
Insert image description here
By doing this, the main function code is very clean, which is convenient for the management and organization of large projects.

The complete file structure is as follows:

Insert image description here

Verification code 2

math.h:

#ifndef __MATH_H  //if not define
#define __MATH_H

int Add(int parm1,int parm2);  //parm1+parm2
int Sub(int parm1,int parm2);  //parm1-parm2
int Mul(int parm1,int parm2);  //parm1*parm2

#endif

math.c:

#include "math.h"

int Add(int parm1,int parm2)
{
	return   parm1 + parm2;
}

int Sub(int parm1,int parm2)
{
	return   parm1 - parm2;
}

int Mul(int parm1,int parm2)
{
	return   parm1 * parm2;
}

Called in main.c:

			printf("add(10,20)计算结果为: %d\r\n",Add(10,20));+

			printf("sub(5,2)计算结果为: %d\r\n",Sub(5,2));

			printf("Mul(5,6)计算结果为: %d\r\n",Mul(5,6));

Execute normally.
Insert image description here

Summarize

1. Familiar with the cycle process of while
2. Learn how to use functions (definition, declaration, call)
3. Learn to create new files, add file paths and add them to projects

After-class exercise: SOS distress light programming

Distress signal principle

Source: Ask knowledgeable people: How does SOS indicate with lights?
Three short, three long and three short
SOS is a common distress signal in the world. If it is represented by light signals, three short lights represent the letter S, three long lights represent the letter O, and then follow Three short lights represent S.
The long-on time of the light is three times that of the short-bright time, and the short-bright time is the same as the interval between two LED lights, and there is also an extinguishing interval of three times the short-bright time between the letter and the next letter.

Sample code

		//S
		while( time < 4 )
		{
		 P22 = 0; //设置低电平	(屠龙刀三板载led)
		 P24 = 0;
		 delay_ms(500);
		 P22 = 1; //设置低电平	(屠龙刀三板载led)
		 P24 = 1;
		 delay_ms(500);
		 time++;
		}
		delay_ms(1000);
		time = 1;
		//O
		while( time < 4 )
		{
		 P22 = 0; //设置低电平	(屠龙刀三板载led)
		 P24 = 0;
		 delay_ms(1500);
		 P22 = 1; //设置低电平	(屠龙刀三板载led)
		 P24 = 1;
		 delay_ms(500);
		 time++;
		}
		delay_ms(1000);
		time = 1;
		//S
		while( time < 4 )
		{
		 P22 = 0; //设置低电平	(屠龙刀三板载led)
		 P24 = 0;
		 delay_ms(500);
		 P22 = 1; //设置低电平	(屠龙刀三板载led)
		 P24 = 1;
		 delay_ms(500);
		 time++;
		}
		delay_ms(1000);
		time = 1;
    ```


Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/132336734