Introduction to C language programming style for AVR microcontrollers

Publisher:tmgouziLatest update time:2019-12-17 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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.

  Introduction to C language programming style for AVR microcontrollers

  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;

Reference address:Introduction to C language programming style for AVR microcontrollers

Previous article:Several main advantages of avr microcontroller
Next article:Analysis of the characteristics and disadvantages of AVR microcontrollers

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号