请教各位大虾!!请各位帮忙看看!!!!! 我程序中的static变量无故变化! 变量无故的变量:static alt_u8 edge_capture和static alt_u8 edge_capture1,它们都在按钮中断中负值,其中,只要一进按钮中断 edge_capture1=5 ! 无故变化情况:时钟频率为200MHz,定时器周期为800个clock。 用jtag口,在nios的dedug窗口调试,单步运行。首先,按按钮,进入按钮中断程序。然后,出中断,可以看到 edge_capture1=5已经成立。接着,运行若干个循环(包括进入定时中断)edge_capture1=5仍然成立。最后,若干个循环(可能是几十个,也可能是上百个循环,我试过的)过后,突然,edge_capture1=0! 但是把定时中断关掉就是正常的,很奇怪!! 源程序~~~~~~~~~~~~~~~~~~~~~~~~ #include <stdio.h> #include <stdlib.h> #include "sys/alt_sys_init.h" #include "system.h" #include "altera_avalon_pio_regs.h" #include "altera_avalon_timer_regs.h" #include "alt_types.h" #include "sys/alt_irq.h" #include "priv/alt_file.h" #include "sys/alt_sys_init.h" static alt_u8 edge_capture; static alt_u8 edge_capture1; static alt_u16 count=0; void delay () /*延时子程序*/ { volatile int i; while (i<3000000) {i++;} i=0; } alt_u8 bcd(alt_u8 b,alt_u8 d)/*二进制码转十进制程序*/ { d=b/10; d*=16; b%=10; d+=b; return (d); } static void time_interrupts(void* context, alt_u32 id)/*定时中断服务程序*/ { volatile alt_u16 buffer; volatile alt_u32 *countptr = (volatile alt_u32*)context; IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);//清TO标志 if(count<9999) count++; else *countptr=0; buffer=count; buffer=buffer|0x04000; IOWR(DCA_BASE,0,buffer); buffer&=0x3fff; IOWR(DCA_BASE,0,buffer);//DCA_BASE用于驱动正弦波译码器 } static void handle_button_interrupts(void* context, alt_u32 id)/*按钮中断服务程序*/ { edge_capture=IORD_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE); edge_capture1=5; //static alt_u8 edge_capture1==5,该变量仅用于检测 IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE, 0); IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_BASE, 0xf); } static void init_time()/*定时中断初始化*/ { alt_irq_register(TIMER_0_IRQ,(void *)&count,time_interrupts ); IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE, 799); IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE, 0); IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, 7); } static void init_button_pio()/*按钮中断初始化*/ { IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_BASE, 0xf); IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE, 0x0); alt_irq_register(SW_IRQ,(void*)&edge_capture,handle_button_interrupts ); } int main (void) __attribute__ ((weak, alias ("alt_main")));/*主程序*/ int alt_main (void) { alt_irq_init (ALT_IRQ_BASE); alt_u8 led=0; alt_u8 out; alt_u8 a; alt_u8 b; init_time(); //Initialize the time. init_button_pio();// Initialize the button_pio. // IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, 0);//但是如果把定时中断关掉就是正常的,很奇怪!! while (1) { a=edge_capture; a&=7; b=edge_capture1;//static alt_u8 edge_capture1==5,该变量仅用于检测 switch(a) //三个按钮 { case 0:led=0;//无按钮按下 break; case 1:led++; break; case 2:if (led==0) led=59; led--; break; case 4:led=5; break; default :break; } if(led>59) led=0; out=bcd(led,out); //二进制码转十进制,准备输出 IOWR(LED_BASE,0,out) ; //把led的值输给io口 delay(); } return(0); }
|