Skip to content

extern使用规范

  1. usart.c 里定义变量(只定义一次):

    c
    volatile int led_flag = 0;
  2. usart.h 里声明变量(供其他文件引用):

    c
    extern volatile int led_flag;
  3. main.c 里通过 #include "usart.h" 使用

    c
    #include "usart.h"
    
    int main(void) {
        led_flag = 1;
        while (1) { }
    }

这样做的好处是:

  • 避免在多个 .c 文件里重复写 extern 声明。
  • 保持声明和定义一致,修改时只需要改 .h 文件。

中断的使用

  • 中断尽量“轻”,只负责计数、置标志,不做耗时操作
  • while(1) 里集中处理,代码逻辑更清晰;

命名

  • 提高可读性(使用更具描述性的变量名)
  • 统一命名风格(采用 snake_caselowerCamelCase,推荐 C 项目中使用 snake_case