外部中断2,IO口输出上升沿触发中断,响应中断程序后num值加1,并显示在数码管上。ICC AVR编译烧写程序运行正常,用GCC AVR编译烧写运行有误,数码管上数值始终显示000。代码优化为none,无优化。求教是什么问题造成的
/*
* INT2.c
*
* Created: 2014-8-18 15:01:21
* Author: Administrator
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000UL
#include <util/delay.h>
unsigned char DuanMa[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char WeiMa[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
volatile unsigned char TempData[3];
volatile unsigned char num;
void led_init(void)
{
DDRA = 0xff;
DDRF |= (1 << 3);
PORTA = 0xff;
PORTF |= (1 << 3);
PORTF &= ~(1 << 3);
}
void seg_init(void)
{
DDRA = 0xff;
DDRC = 0xff;
DDRF |= (1 << 1) | (1 << 2);
PORTC = 0xff;
PORTF |= (1 << 1);
PORTF &= ~(1 << 1);
PORTA = 0xff;
PORTF |= (1 << 2);
PORTF &= ~(1 << 2);
}
void seg_display(unsigned char firstbit,unsigned char weishu) //数码管显示函数
{
static unsigned char i;
PORTC = 0xff; //数码管消隐
PORTF |= (1 << 1);
PORTF &= ~(1 << 1);
PORTC = TempData[i];
PORTF |= (1 << 1);
PORTF &= ~(1 << 1);
PORTA = WeiMa[i+firstbit];
PORTF |= (1 << 2);
PORTF &= ~(1 << 2);
_delay_ms(1);
i++;
if(i == weishu)
i = 0;
}
void int0_init(void)
{
EICRA |= (1 << ISC21) | (1 << ISC20); //INT2上升沿触发
EIMSK |= (1 << INT2); //INT2使能
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFD; //setup
TCNT1L = 0x8F;
TCCR1B = 0x03; //start Timer
TIMSK = 0x04; //timer interrupt sources
}
int main(void)
{
DDRD |= (1 << 2);
PORTD |= (1 << 2);
led_init();
seg_init();
int0_init();
timer1_init();
SREG |= (1 << 7);
while(1)
{
TempData[0] = DuanMa[num/100];
TempData[1] = DuanMa[(num%100)/10];
TempData[2] = DuanMa[(num%100)%10];
PORTD |= (1 << 2);
_delay_ms(200);
PORTD &= ~(1 << 2);
_delay_ms(200);
}
}
ISR(INT0_vect)
{
_delay_ms(10); //去抖动
num++;
if(num == 200)
num = 0;
}
ISR(TIMER1_OVF_vect)
{
TCNT1H = 0xFD; //setup
TCNT1L = 0x8F;
seg_display(0,3);
} |