马老师,已加volatile,为什么WINAVR在AD的中断中修改全局变量,全局变量却没有被更新
如题,我加了VOLATILE的,原代码如下,用的是2007版的WINAVR.
#include "8803.c"
#include <avr/io.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <avr/interrupt.h>
volatile int ad_data={0,0,0,0,0,0,0}; //AD转换结果
volatile int ad_data1;
char str0={0,0,0,0,0,0}; //整数转字符串缓冲区
char str1;
char str2;
char str3;
char str4;
char str5;
char str6;
unsigned char s=0;
unsigned char count=0,flag=0,flag1=0;
unsigned intH_min,H_max,L_max,L_min;
void show0(unsigned int i, char *str )
{
itoa(i,str,10);
text_8803(6,0,0x11,str,(unsigned int) strlen(str));
}
void show1(unsigned int i, char *str )
{
itoa(i,str,10);
text_8803(6,16,0x11,str,(unsigned int) strlen(str));
}
void show2(unsigned int i,char *str )
{
itoa(i,str,10);
text_8803(6,32,0x11,str,(unsigned int) strlen(str));
}
void show3(unsigned int i, char *str )
{
itoa(i,str,10);
text_8803(6,48,0x11,str,(unsigned int) strlen(str));
}
void show4(unsigned int i, char *str )
{
itoa(i,str,10);
text_8803(6,64,0x11,str,(unsigned int) strlen(str));
}
void show5(unsigned int i, char *str )
{
itoa(i,str,10);
text_8803(6,80,0x11,str,(unsigned int) strlen(str));
}
void show6(unsigned int i, char *str )
{
itoa(i,str,10);
text_8803(6,96,0x11,str,(unsigned int) strlen(str));
}
void led_inital()
{
DDRA=0xef;
PORTA=0xff; //PA7上拉电阻使能,灭PA0~PA6七个LED
}
void infrared_inital()
{
DDRC=0xef;
PORTC=0xff; //PC7上拉电阻可使能,灭所有对管
}
void AD_inital()
{
DDRF=0xfe;
PORTF=0xfe; //PF0作AD时,上拉电阻不可使能
ADMUX=0xc0; //内部2.56V,结果右对齐,PF0单通道
ADCSRA=0x8f; //中断使能,128分频
}
ISR(ADC_vect) //AD转换完成中断
{
ad_data1=ADCL;
ad_data1|=ADCH<<8;
show0(ad_data1,str0);
ad_data1=125;
// show0(ad_data1,str0);
// ad_data=1250;
sei();
}
/*
void check() //软件滤波
{
if((ad_data>L_min)||(ad_data>H_max)) //?
{
SBI(ADCSRA,ADSC); //重新扫描一次
}
}
*/
void scan1()
{
unsigned char i;
for(i=1;i<=7;i++)
{
PORTC&=~(1<<(i-1)); //开对管
flag1=0;
flag=i-1;
SBI(ADCSRA,ADSC); //开启转换
while(flag1==0);
flag1=0;
PORTC|=1<<(i-1);
}
show0(ad_data,str0);
show1(ad_data,str1);
show2(ad_data,str2);
show3(ad_data,str3);
show4(ad_data,str4);
show5(ad_data,str5);
show6(ad_data,str6);
}
int main()
{
inital_8803();
cls_8803();
text_8803(1,0,0x11, "PA0:",5);
text_8803(1,16,0x11,"PA1:",5);
text_8803(1,32,0x11,"PA2:",5);
text_8803(1,48,0x11,"PA3:",5);
text_8803(1,64,0x11,"PA4:",5);
text_8803(1,80,0x11,"PA5:",5);
text_8803(1,96,0x11,"PA6:",5);
led_inital();
infrared_inital();
AD_inital();
sei();
// CBI(PORTC,0);
SBI(ADCSRA,ADSC);
flag1=0;
flag=0;
while(1)
{
if(flag1==1);break;
}
show2(ad_data,str1);
ad_data=1250;
show3(ad_data1,str0);
// show0(ad_data,str0);
// flag1=0;
// scan1();
}
在AVRStudio仿真时发现全局变量能被更新,但是在板子上跑起来就不是这样的了. 请熟悉WINAVR的朋友解释吧,我不了解WINAVR. 没用过仿真
建议用一个按钮控制一下,按一次,采样一次,显示一次
我想可能是采样太快,显示跟不上 已解决,谢谢了!
===========================
对呀,解决问题很好,但希望把问题讲清楚,可以让别人借鉴学习. 我以前遇到个也是不被更新,后来加了volatile就解决了,稀里糊涂的,你的是怎么解决的?为什么呢? volatile就不会被优化掉的, 没有非常好C的基础,还是用CVAVR吧。
页:
[1]