搜索
bottom↓
回复: 6

马老师,已加volatile,为什么WINAVR在AD的中断中修改全局变量,全局变量却没有被更新

[复制链接]

出0入0汤圆

发表于 2008-5-10 22:06:06 | 显示全部楼层 |阅读模式
如题,我加了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[7]={0,0,0,0,0,0,0};                                //AD转换结果  
volatile int ad_data1;  

char str0[10]={0,0,0,0,0,0};                                                        //整数转字符串缓冲区  
char str1[10];  
char str2[10];  
char str3[10];  
char str4[10];  
char str5[10];  
char str6[10];  

unsigned char s=0;  
unsigned char count=0,flag=0,flag1=0;  

unsigned int  H_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[0]=1250;  
        sei();  
}  

/*  
void check()                                                                                                //软件滤波  
{  
                if((ad_data[0]>L_min)||(ad_data[0]>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[0],str0);  
        show1(ad_data[1],str1);  
        show2(ad_data[2],str2);  
        show3(ad_data[3],str3);  
        show4(ad_data[4],str4);  
        show5(ad_data[5],str5);  
        show6(ad_data[6],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[1],str1);  
        ad_data[2]=1250;  
        show3(ad_data1,str0);  
//        show0(ad_data[0],str0);  
//        flag1=0;  
//        scan1();  



}  



在AVRStudio仿真时发现全局变量能被更新,但是在板子上跑起来就不是这样的了.

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2008-5-11 12:51:24 | 显示全部楼层
请熟悉WINAVR的朋友解释吧,我不了解WINAVR.

出0入0汤圆

发表于 2008-5-11 13:44:11 | 显示全部楼层
没用过仿真
建议用一个按钮控制一下,按一次,采样一次,显示一次
我想可能是采样太快,显示跟不上

出0入0汤圆

 楼主| 发表于 2008-5-11 19:05:15 | 显示全部楼层
已解决,谢谢了!

===========================
对呀,解决问题很好,但希望把问题讲清楚,可以让别人借鉴学习.

出0入0汤圆

发表于 2008-5-11 19:33:23 | 显示全部楼层
我以前遇到个也是不被更新,后来加了volatile就解决了,稀里糊涂的,你的是怎么解决的?为什么呢?

出0入0汤圆

发表于 2009-8-1 23:44:59 | 显示全部楼层
volatile就不会被优化掉的,

出0入0汤圆

发表于 2009-8-6 01:18:09 | 显示全部楼层
没有非常好C的基础,还是用CVAVR吧。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 07:24

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表