Abstract: How can a beginner have a good programming style? The following article will introduce the C language programming style of AVR microcontroller.
Introduction to C programming style
1. Variable definition
When defining a variable, the prefix is the type of variable, followed by an English word or abbreviation that represents the purpose of the variable, and the first letter of each word or abbreviation is capitalized. The various prefix abbreviations are as follows:
Unsigned variables use u8, u16, u32; for example: unsigned char u8Temp;
Signed variables use s8, s16, s32; example: char s8Temp;
Floating point variables use f32, d64; example: float f32Temp;
Structure variables use st; for example: struct Temp stTemp;
Use s for string variables; for example: char sTemp;
Array variables use a; for example: unsigned char aTemp[10];
Pointer variable uses p; example: unsigned char *pTemp;
Enumeration variables use e; for example: enum Temp eTemp;
2. Macro definition
Use uppercase + underscore for macro definitions.
3. Program layout
Use the tab key to align the beginning of a line of program, and use the space key to align the middle of a line.
This prevents code confusion when different editing tools are opened.
4. Writing comments
The comments here are divided into function header comments and program code comments.
Function header comments use the following format
/*
*Name:
*Description:
*Created:
*Author:
*/
These four items are required, and others, such as the input and output parameters of the function, can be added appropriately depending on the needs of the function.
5. Test your code
While programming, you need to pay attention to adding appropriate test code, which can reduce the workload when testing the code later.
AVR C language excellent programming style
File Structure
Modular programs should have a good program structure. AVR C language programs have two types of user files, .c program files and .h header files. During the program writing process, the .c file needs to include the .h header file. Beginners often have problems with repeated inclusion or header file inclusion errors. I was often worried about this error at the time. Below I will use the motor drive routine I wrote to illustrate to you the excellent programming file structure.
There are 8 files and one description file in this project, as shown below: Download program example motor control case.
The number of files in the programs I wrote is basically an even number, because each structured function definition .c file will correspond to a .h file. main.c corresponds to config.h. Let's take a look at the inclusion relationship of each file. Let's take a look at the inclusion relationship and content of these files: [Recommended file inclusion order and relationship]
All .c files include the config.h file. For example: #include "config.h"
In config.h there is the following code:
#include "delay.h"
#include "device_init.h"
#include "motor.h"
This makes it less likely that incorrect inclusion relationships will occur. To prevent this, we also introduce macro definitions and precompilation. As follows:
#ifndef _UNIT_H__
#define _UNIT_H__ 1
//100us
extern void Delay100us(uint8 n);
//1s
extern void Delay1s(uint16 n); // n <= 6, when n==7, it is 1.
//1ms
extern void Delay1ms(uint16 n);
#endif
The first time this file is included, it compiles correctly and #define _UNIT_H__ 1 is set. The second time this file is included, #ifndef _UNIT_H__ is no longer true and the file is skipped.
Precompilation has more uses, such as compiling different statements based on different values, as follows:
//#pragma REGPARMS
#if CPU_TYPE == M128
#include 《iom128v.h》
#endif
#if CPU_TYPE == M64
#include 《iom64v.h》
#endif
#if CPU_TYPE == M32
#include 《iom32v.h》
#endif
#if CPU_TYPE == M16
#include 《iom16v.h》
#endif
#if CPU_TYPE == M8
#include 《iom8v.h》
#endif
The difference between #include《filename》 and #include "filename": the former includes files under the system directory include, while the latter includes files under the program directory.
Variable names and function names
Variable and function names should be as short as possible, as long as needed, and meaningful. You can use underscores or a combination of uppercase and lowercase to form variable and function names. The following compares good and bad naming methods:
1. OK: Delay100us();
Bad: Yanshi();
2. OK: init_devices();
Bad: Chengxuchushihua();
3. OK: int temp;
Bad: int dd;
Previous article:Several main advantages of avr microcontroller
Next article:Analysis of the characteristics and disadvantages of AVR microcontrollers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- C51 uses macro definition to replace printf function
- [STM32WB55 Review] +USB Dongle Bricked
- STM32F429 USB_OTG_FS (PA11, PA12) read USB disk problem
- PoE and PoE+, an article to understand Power over Ethernet
- How to control trace impedance of PCB for signal integrity?
- Disassembling the USB wireless network card, the circuit scheme is very classic
- 5G millimeter wave antenna
- Design of Switching Power Supply Using MSP430 Microcontroller
- Analog Electronics Course Selection Test
- About SPI mode setting