您的位置 首页 ADAS

51单片机外部中止示例

voidUsart_INT0_init(){TMOD=0X21;TH1=0XFD;TL1=0XFD;SM0=0;SM1=1;REN=1;TR1=1;//ES=1;//串口中断影响…

void Usart_INT0_init()

{
TMOD = 0X21;
TH1 = 0XFD;
TL1 = 0XFD;
SM0 = 0;
SM1 = 1;
REN = 1;
TR1 = 1;

//ES = 1;//串口中止影响外部中止0 这句话会让程序无法进入外部中止服务

TH0 = 0;
TL0 = 0;
ET0 = 0;//封闭定时器0

IT0 = 1;//外部中止下降沿触发*/
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;//封闭定时器
EX0 = 1;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/263846.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部