This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
volatile Uint32 RunF;
volatile Uint16 Duty;
volatile Uint16 ADC_U_out;
volatile int16 Phase_erro;
interrupt void adc_isr(void)
{
static volatile Uint16 temp16,cnt=0;
LedOn;LedOff;
ADC_U_out= AdcResult.ADCRESULT0;
if( ADC_U_out>512 )
{
if(Duty>10) Duty--;
}
else
{
if(Duty<100) Duty++;
}
volatile Uint16 ADC_U_out; 必须要要用 volatile ,否则运行出错。 这是为什么?
首先要知道,我们写的C代码并不是最终下载到芯片的代码,这中间经过了编译器的翻译,这里的编译器就是CCS了。这个编译器翻译的时候有时候会“自作聪明”,把我们写的代码给改掉,这样的修改很多时候会提高程序的效率,但是有时候则会使程序运行得跟我们想要的效果不同。例如,我们在程序中写了:a=b;b=c;那么CCS最后翻译的时候很可能翻译成a=c,把b给拿走了,但是如果这个b实际上是芯片自身的寄存器呢?就比如是AD里的存储器,这下翻译后的程序就会出现和我们想象不一样了。所以例程里面所有定义寄存器的地方,基本都用了volatile,就是不想让CCS瞎改。