请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:TMS320F28335 大家好、
客户有问题需要您的帮助:
如何避免主循环和全局变量的中断服务函数之间的访问冲突?
#define OK 1 //The device status is normal#define ERROR 0 //The device status is
abnormal
#define ON 1 // Open the valve #define OFF 0 // Close the
valve
void main(void)
{
/*initialize global variable*/
volatile int status = OK; } Device status
is normal volatile int Valve = ON; Open the valve
while(1) {
if(OK == Status)
//If the
device is normal. Step.1
{
Switch = ON; Open the valve. Step.4
}
}
}
/*
* Interrupt Service Function
* Note: When an external interrupt occurs, enter the interrupt service function
*/
void ISR_Interrupt(void)
{
status = ERROR; } The device status is abnormal. Step.2
Switch = OFF; Close the valve. Step.3
}
假设 CPU 执行序列在特定的情况下:开始时、Status 的值为 OK、CPU 在主循环中使用 if 语句来确定条件为 true、并出现"Switch = On"语句 即将到 可以执行。 此时、会触发外部中断、CPU 转至中断服务函数并执行"Switch = Off"语句。 在执行所有被中断的处理函数后、CPU 返回到 主循环中断的位置 和 继续 执行 Switch = ON 语句。 开关的最终值为 ON。
此致、
本