51單片機外部中斷示例
{
TMOD = 0X21;
TH1 = 0XFD;
TL1 = 0XFD;
SM0 = 0;
SM1 = 1;
REN = 1;
TR1 = 1;
//ES = 1;//串口中斷影響外部中斷0 這句話會讓程序無法進入外部中斷服務(wù)
TH0 = 0;
TL0 = 0;
ET0 = 0;//關(guān)閉定時器0
IT0 = 1;//外部中斷下降沿觸發(fā)*/
EX0 = 1;//打開外部中斷
EA = 1;
//ES = 1;
}
void Send_data(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
int j;
void main()
{
unsigned int i;
LED = LED_ON;
//LED = LED_OFF;
flag_test_high = ON;
IN0 = 1;
Usart_INT0_init();
EX0 = 1;
while(1)
{
/*i=40000;
while(i--);*/
/*flag_test_high = ON;
EX0 = 1;
while(flag_test_high == ON);
EX0 = 0;
//Send_data(a);*/
Send_data(result|0x30);
}
}
void EX_int(void) interrupt 0
{
EX0 = 0;
ET0 = 1;//打開定時器
TR0 = 1;
while(IN0 == 0);
TR0 = 0;
tem_TH = TH0;
tem_TL = TL0;
sum = tem_TH*256 + tem_TL;
sum = (int)sum/1000;
result = 9 - sum;
//result = 9-(int)(10*(TH0*256 + TL0)/10000);//分步計算避免出錯
TH0 = 0;
TL0 = 0;
LED = LED_OFF;
//Send_data(result|0x30);
flag_test_high = OFF;
ET0 = 0;//關(guān)閉定時器
EX0 = 1;
}
評論