本文介绍了C 中的 volatile 变量或 volatile 结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的头文件(Header.h)

Here is my header file (Header.h)

#include <stdio.h>
#include <string.h>
void Process(void);

和Header.C"

#include <stdio.h>
#include <string.h>
#include "Header.h"

struct St{
    unsigned long int volatile Var1;
    unsigned long int volatile Var2;
    unsigned char volatile Flag;
};

extern struct ST Variable;

void Process(void){
Variable.Var1=Variable.Var2;
}

和主文件:

#include <stdio.h>
#include <string.h>
#include "Header.h"

struct St{
    unsigned long int volatile Var1;
    unsigned long int volatile Var2;
    unsigned char volatile Flag;
};

struct ST Variable;

//Interrupt each 10us

void TIM_IRQHandler(){

//Do something

    if(something==True)
    {
    Variable.Flag=1;
    Variable.Var2=AVariable; //AVariable is between 30-40
    }

}

int main(){

    while(1)
    {
    //wait 10ms
    if(Variable.Flag==1)
        {
        Process();
        Variable.Flag=0;
        }

    }

}

如您所见,每 10us 发生一次中断,如果它正确执行某些代码,它将更改 Var2 的值,其值介于 30-40 之间,并设置一个 Flag 变量.在主代码中,如果 Flag 已设置,则应调用进程函数并将 Var1 更改为 Var2 值.但是有时我会收到带有其他不正确的奇怪值的 var1.我测试了我的中断,我发现我从来没有用奇怪的值填充我的 Var2.

As you can see an Interrupt occurs each 10us and if it does some codes correctly it will change Var2 with a value between 30-40 and set a Flag variable. in Main code if Flag has been set it should be call process function and change Var1 with Var2 value.But sometimes I receive var1 with other strange values that they are not correct.I have tested my interrupt and I find out I never fill my Var2 with strange values.

void TIM_IRQHandler(){

//Do something


    if(something==True)
    {
    Variable.Flag=1;
    Variable.Var2= < 30-40>;
    }
   if(Variable.Var2>40 || Variable.Var2<30){
      printf("ERROR");
     }

}

并且所有中断功能都可以正常工作,但在 Process 功能中它让我很生气.

and all Interrupt function works fine but in Process function it makes me angry.

对于我没有注意的任何技巧,我将不胜感激.

I will appreciate for any tricks that I didn't pay attention.

推荐答案

在任何类型的 typedef 中使用关键字volatile"时,永远不要期待任何事情.您正在声明类型struct St",包括关键字.您的描述暗示您期望变量Variable"的易变行为,该变量是在没有关键字的情况下定义和声明的.根据我的经验,关键字仅有时(取决于平台和编译器)在类型内起作用.如果在变量的定义和声明中都使用它,它似乎可靠地产生效果.

Never expect anything when using the keyword "volatile" inside any kind of typedef. You are declaring the type "struct St", including the keyword. Your description implies that you expect a volatile behaviour on the variable "Variable", which is defined and declared without the keyword.In my experience the keyword only sometimes (depending on platform and compiler) has an effect inside a type. It seems to reliably have an effect if used in both, the definition and the declaration of a variable.

尝试改变

struct ST Variable;

volatile struct ST Variable;

extern struct ST Variable;

extern volatile struct ST Variable;

此外,St"!=ST"周围是否有错别字,struct ST"类型在其他地方声明?
另外,作为旁注,您可能希望将类型声明移动到标题中.

Also, is there a typo around "St" != "ST", with a "struct ST" type being declared elsewhere?
Also, as a side note, you might want to move your type declarations into the header.

我目前无法访问 C 环境,所以请原谅我没有测试我的答案.

I currently do not have access to a C environment, so please excuse me not testing my answer.

这篇关于C 中的 volatile 变量或 volatile 结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:03