用ATmega16 单片机想测量按键从按下到松开的时间,用AVR stdio4编程。用外部中断0确定第一次和最后一次按键电平变化,用定时器1计量时间。但发现一个问题,就是在中断服务程序中,修改全局变量count时,main函数中的count值不会变化。测试后发现,中断服务程序修改的全局变量只能在中断程序中显示改变,而在main函数中,不显示改变。各位大神,请问怎么解决?
#define F_CPU 8000000UL
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#include"TypeRedefinition.h"
extern uint8 Count = 123;
uint8 Time = 0;
uint8 SwichTimer = 0;
uint8 SwichDis = 0;
uint8 Display[3] = {1,2,3};
//共阳
const uint8 SEG_CODE[] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int main()
{
uint8 i;
DDRC = 0x0f;
DDRA = 0xff;
DDRD &= ~(1 << 2) ;
PORTA = 0xff;
PORTD |= (1 << 2) ; //IO初始化
TCCR0 = 0x05; //预分频:1024
MCUCR = 0x01; //INT0下降沿触发触发,0x00低电平,0x01逻辑电平变化,
//0x02下降沿,0x03上升沿
GICR = 0x40; //INT0中断使能
sei();
while(1)
{
if(SwichDis == 1)
{
Time = Count*128/1000;
Display[0] = Time/100;
Display[1] = Time%100/10;
Display[2] = Time%100%10; //将Time转换为可显示的Display[]
for(i = 0;i < 3;i ++) //显示Display[]
{
PORTC = ~(1 << i);
PORTA = SEG_CODE[Display[i]];
_delay_ms(2);
}
}
}
}
ISR(INT0_vect)
{
if(SwichTimer == 0)
{
TCNT0 = 0;
SwichTimer = 1;
}
else
{
Count = 223;
//Count = TCNT0;
SwichDis = 1;
}
} |