void INT0_init(void) { EX0 = 1; IT0 = 1; EA = 1; }解释这段代码
时间: 2024-05-29 20:11:34 浏览: 27
这段代码是用于初始化外部中断INT0的函数。具体解释如下:
1. EX0 = 1; 表示使能外部中断INT0。
2. IT0 = 1; 表示设置外部中断INT0触发方式为边沿触发。
3. EA = 1; 表示使能总中断。
综上所述,这段代码的作用是使能外部中断INT0,并设置其触发方式为边沿触发,同时使能总中断。
相关问题
代码改错#include <REGX51.H> #include <intrins.h> #define uchar unsigned char #define uint unsigned int #define P2_0 P2|0x01 #define P2_1 P2|0x02 #define P2_2 P2|0x04 #define P2_3 P2|0x08 #define P2_4 P2|0x10 #define P2_5 P2|0x20 #define P2_6 P2|0x40 #define P2_7 P2|0x80 #define P0_0 P0|0x01 #define P0_1 P0|0x02 #define P0_2 P0|0x04 #define P0_3 P0|0x08 #define P0_4 P0|0x10 #define P0_5 P0|0x20 #define P0_6 P0|0x40 #define P0_7 P0|0x80 #define EXTI0_IT_RISING 0x01 #define EXTI1_IT_RISING 0x02 void INT0_Init(void); void INT1_Init(void); void EXTI0_Handler(void); void EXTI1_Handler(void); void delay(unsigned int xms) { while(xms--) } void main(void) { P2=0x00; EXTI0_Init(); EXTI1_Init(); while(1){ if(P2_0==1){ delay(500); P2=~P2; } } } void INT0_Init(void) { IT0=EXTI0_IT_RISING; EX0=1; EA=1; } void INT1_Init(void) { IT1=EXTI1_IT_RISING; EX1=1; EA=1; } void EXTI0_Handler(void) { if(P2_0==0){ P0=_crol_(_cror_(P0,7),1); } } void EXTI1_Handler(void) { if(P2_1==1){ delay(500); P2=~P2; } }
这段代码有几个错误:
1. 缺少头文件:此代码使用了一些特定的寄存器和函数,需要包含对应的头文件。常用的头文件有"reg51.h"和"8051.h",需要添加其中一个头文件。
2. delay函数没有实现:在delay函数的定义后面没有实现具体的延时功能,需要在函数体内添加具体的延时实现。
3. 宏定义错误:在宏定义中,应该使用位运算或者逻辑运算来设置对应的位,而不是使用按位或运算。例如,P2_0应该定义为P2 & 0x01。
4. 函数声明错误:在函数声明中,需要指定函数的返回类型。例如,void delay(unsigned int xms)应该改为unsigned int delay(unsigned int xms)。
5. EXTI0_Handler函数中的代码错误:在此函数中,应该判断P2_0是否为0,而不是是否为1。
6. INT1_Init函数中的中断类型错误:在此函数中,应该指定中断类型为下降沿触发,而不是上升沿触发。例如,IT1=EXTI1_IT_FALLING。
正确的代码应该是这样的:
```
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define P2_0 (P2 & 0x01)
#define P2_1 (P2 & 0x02)
#define P2_2 (P2 & 0x04)
#define P2_3 (P2 & 0x08)
#define P2_4 (P2 & 0x10)
#define P2_5 (P2 & 0x20)
#define P2_6 (P2 & 0x40)
#define P2_7 (P2 & 0x80)
#define P0_0 (P0 & 0x01)
#define P0_1 (P0 & 0x02)
#define P0_2 (P0 & 0x04)
#define P0_3 (P0 & 0x08)
#define P0_4 (P0 & 0x10)
#define P0_5 (P0 & 0x20)
#define P0_6 (P0 & 0x40)
#define P0_7 (P0 & 0x80)
#define EXTI0_IT_RISING 0x01
#define EXTI1_IT_FALLING 0x02
unsigned int delay(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
return xms;
}
void INT0_Init(void);
void INT1_Init(void);
void EXTI0_Handler(void);
void EXTI1_Handler(void);
void main(void)
{
P2=0x00;
EXTI0_Init();
EXTI1_Init();
while(1)
{
if(P2_0==1)
{
delay(500);
P2=~P2;
}
}
}
void INT0_Init(void)
{
IT0=EXTI0_IT_RISING;
EX0=1;
EA=1;
}
void INT1_Init(void)
{
IT1=EXTI1_IT_FALLING;
EX1=1;
EA=1;
}
void EXTI0_Handler(void)
{
if(P2_0==0)
{
P0=_crol_(_cror_(P0,7),1);
}
}
void EXTI1_Handler(void)
{
if(P2_1==1)
{
delay(500);
P2=~P2;
}
}
```
#include <STC32G.H> #include <stdio.h> sbit input = P3^2; unsigned long num,fre; unsigned char flag = 0; void timer0_init() { AUXR |= 0x80; TMOD &= ~0X03; // 清空定时器0的模式位 TMOD |= 0X01; // 设置定时器0的模式为16位定时器模式 TH0 = 0; // 定时器0计数初值高字节 TL0 = 0; // 定时器0计数初值低字节 TR0 = 1; // 定时器0开始计数 ET0 = 1; // 允许定时器0中断 EA = 1; // 打开总中断开关 } void int0_init() { // 设置外部中断0为下降沿触发方式,并允许外部中断0中断 IT0 = 1; EX0 = 1; EA = 1; } void main() { timer0_init(); int0_init(); while(1) { if(flag == 1) { fre = num/65.536; flag = 0; // 清除标志位 } } } void timer0_isr() interrupt 1 { TH0 = 0; TL0 = 0; flag = 1; } void int0_isr() interrupt 0 { num++; }
这段代码是关于定时器和外部中断的初始化和使用的代码。其中,定时器0被设置为16位定时器模式,并开启中断,外部中断0被设置为下降沿触发,并开启中断。在主函数中,不断检测标志位flag是否为1,如果是,则计算频率并清除标志位。在定时器中断中,每次中断都会将计数器清零,并设置标志位为1。在外部中断中,每次中断都会将计数器num加1。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)