当前位置:网站首页>STM32 single chip microcomputer -- volatile keyword

STM32 single chip microcomputer -- volatile keyword

2022-07-05 08:15:00 chen_ bx

1. Application scenarios

volatile Keyword analysis , It is often used in three occasions

1) When multithreaded programming shares global variables , The global variable needs to be added with volatile To embellish , Let the compiler not optimize the variable .

2) During bare metal programming , When a function shares a global variable with an interrupt service function , The global variable needs to be added with volatile To embellish , Let the compiler not optimize the variable .

3)ARM When the register is defined , A register points to an address , To add volatile To embellish , Let the compiler omit access to the variable without optimization .

The compiler should not optimize this variable, which means to prevent the compiler from over Optimizing , Cause code operation failure .

add volatile The assembly code generated by keywords will change significantly , Also called delay function , The speed of the lamp changes !

2. Example

delay Function in -O2 Grade , Whether to add volatile keyword , Disassembly analysis .
 Insert picture description here

原网站

版权声明
本文为[chen_ bx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140545026840.html