AVR C language excellent programming style
File Structure
There are 8 files and one description file in this project, as shown below: Download program example motor control case .
- 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 valid and the file is skipped. Precompilation has more uses, such as compiling different statements according to different values, as follows: //#pragma REGPARMS #if CPU_TYPE == M128 #include
#endif #if CPU_TYPE == M64 #include #endif #if CPU_TYPE == M32 #include #endif #if CPU_TYPE == M16 #include #endif #if CPU_TYPE == M8 #include #endif - The difference between #include
and #include "filename": the former includes files under the system directory include, while the latter includes files under the program directory.
- Good:
Delay100us();
Bad:Yanshi(); - Good:
init_devices();
Bad:Chengxuchushihua(); - Good:
int temp;
Bad:int dd;
- First define extern in the .h file of the modular program
//Port initialization extern void port_init(void); //T2 initialization void timer2_init(void); //Various parameter initialization extern void init_devices(void);
- Define functions in the .c file of a modular program. Do not call programs in modular programs, and do not use functions like timer2_init(); because you will not know where you called the function later, which will increase the difficulty of debugging the program. You can call other functions as the function body during the definition of a function.
// PWM frequency = system clock frequency / (division coefficient * 2 * counter upper limit)) void timer2_init(void) { TCCR2 = 0x00; //stop TCNT2= 0x01; //set count OCR2 = 0x66; //set compare TCCR2 = (1<
- Call functions in a few files, most of the functions are called in main.c, and service functions are called according to different interrupts in interupts.c.
void main(void) { //initial work init_devices(); while(1) { for_ward(0); //default speed forward operation Delay1s(5); //delay 5s motor_stop(); //stop Delay1s(5); //delay 5s back_ward(0); //default speed reverse operation Delay1s(5); //delay 5s speed_add(20); //accelerate Delay1s(5); //delay 5s speed_subtract(20); //decelerate Delay1s(5); //delay 5s } }
- One is to use macros to simplify frequently used commands or statements.
#ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0 #endif #define MIN(a,b) ((ab)?(a):(b)) #define ABS(x) ((x>)?(x):(-x)) typedef unsigned char uint8; typedef signed char int8; typedef unsigned int uint16; typedef signed int int16; typedef unsigned long uint32; typedef signed long int32;
- The second is to use macro definitions to facilitate hardware interface operations. When the program needs to be modified, you only need to modify the macro definitions, without having to look for command lines throughout the program to make modifications.
//PD4,PD5 motor direction control If you change the pin to control the motor direction, just change PORTD |= 0x10. #define moto_en1 PORTD |= 0x10 #define moto_en2 PORTD |= 0x20 #define moto_uen1 PORTD &=~ 0x10 #define moto_uen2 PORTD &=~ 0x20 //Enable TC2 timing comparison and overflow #define TC2_EN TIMSK |= (<<1OCIE2)|(1<
- Add a single-line comment where a special function is used or a command is called. The usage is:
Tbuf_putchar(c,RTbuf); // Add data to the transmit buffer and enable interrupt extern void Delay1s(uint16 n); // n <= 6 ,when n==7, it is 1.
- Use detailed paragraph comments in modular functions:
- Add file name, file purpose, author, date and other information to the file header.
Previous article:AVR Notes 7: ATmega16 locked
Next article:AVR Notes 5: .c and .h Nature
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- IoT security himqtt firewall data structure ringbuffer ring buffer
- [Erha Image Recognition Artificial Intelligence Vision Sensor] 5. Erha Sensor Serial Port Communication Test
- EEWORLD University Hall----Live Replay: TI's Latest Application of Millimeter-Wave Radar in the Automotive Field
- Changes in car rear lighting design
- A collection of essential basic knowledge for electronic engineers
- Last day! Live broadcast with prizes | RSL15 - ON Semiconductor's more efficient, smarter and safer BLE 5.2 Bluetooth chip
- Studying the road to electric motor driving-1
- TI blog post: The principle of BLDC square wave sensorless control using ADC sampling and integration
- EEWORLD University - Tektronix will teach you the oscilloscope usage skills you don't know
- LM2776 charge pump cannot start