gpt4 book ai didi

C 全局在 ISR 中声明

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:04 25 4
gpt4 key购买 nike

我正在评估 Freescale 的 Kinetis Design Studio 的 ARM 系列微 Controller 。我在看 example作为我的第一个“闪烁 LED”项目的基础。当使用在我的主程序和 ISR(例如计数器)之间共享的变量时,我通常会在 main.c 中定义一个 volatile 全局变量,并在 ISR 中将其引用为 extern。他们的例子恰恰相反,他们也不使用 volatile 关键字。我从未见过这样做的。这有什么好处吗?顺便说一句,无论哪种方式,我的程序都运行良好。

最佳答案

缺少 volatile 是一个错误 ( albeit a common one ),它本身应该是一个质量警告。选择在何处实例化全局变量是任意的,但从内聚的角度来看,将与中断相关的数据 保留在中断中是有意义的。也就是说,使用 global data本身就是质量和做法可疑的迹象。

利用数据封装的更好模式是:


中断.c

...

volatile static int counter = 0 ;
void interruptHandler()
{
counter++ ;
}

int interruptCounter{ return counter } ;

...

中断.h

...

extern int interruptCounter() ;

...

main.c

#include "interrupt.h"

...

int icount = interruptCount() ;

...

关于C 全局在 ISR 中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30791529/

25 4 0
文章推荐: javascript - Node.JS 加密解密不起作用
文章推荐: css - 删除 Navbar 和 Navbar.Header 之间的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com