对于中断方式控制LED灯,查询方式控制LED灯两个实验的实验结论
时间: 2023-08-22 17:04:24 浏览: 37
对于中断方式控制LED灯的实验结论是:中断方式可以实现较为精确地控制LED灯的亮灭,但是在中断处理函数中需要尽可能地快速完成操作,否则会影响系统的稳定性。
对于查询方式控制LED灯的实验结论是:查询方式可以简单地控制LED灯的亮灭,但是在需要同时处理多个任务时会出现效率低下的问题。另外,查询方式需要占用较多的CPU时间,影响系统的响应速度。
相关问题
STM32两个按键中断控制LED灯
以下是STM32两个按键中断控制LED灯的代码实现:
```c
#include "stm32f10x.h"
void LED_GPIO_Config(void);
void KEY_GPIO_Config(void);
void EXTI_Config(void);
void NVIC_Config(void);
int main(void)
{
LED_GPIO_Config();
KEY_GPIO_Config();
EXTI_Config();
NVIC_Config();
while (1)
{
// 等待中断
}
}
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void KEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void EXTI_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource6);
EXTI_InitStructure.EXTI_Line = EXTI_Line5 | EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI9_5_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line5) != RESET)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_0)));
EXTI_ClearITPendingBit(EXTI_Line5);
}
if (EXTI_GetITStatus(EXTI_Line6) != RESET)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_1)));
EXTI_ClearITPendingBit(EXTI_Line6);
}
}
```
外部中断写按键控制一个LED灯的两灭
假设你使用的是8051单片机,可以使用外部中断来检测按键的状态,并且使用P0口控制LED灯的状态。
以下是代码示例:
```
#include <reg52.h>
sbit LED = P0^0; // LED连接到P0.0口
sbit KEY = P3^2; // 按键连接到P3.2口
void main()
{
EA = 1; // 打开总中断允许
EX0 = 1; // 打开外部中断0允许
IT0 = 1; // 设置外部中断0为下降沿触发
while(1); // 循环等待中断
}
void External0() interrupt 0
{
int i;
for(i = 0; i < 2; i++) // 控制LED两次闪烁
{
LED = 0; // 点亮LED
delay(500); // 延时一段时间
LED = 1; // 熄灭LED
delay(500); // 延时一段时间
}
}
void delay(int ms) // 延时函数
{
int i, j;
for(i = 0; i < ms; i++)
{
for(j = 0; j < 120; j++);
}
}
```
在上面的代码中,我们使用了外部中断0来检测按键的状态。当按键被按下时,外部中断0会被触发,进入External0()函数中。在这个函数中,我们使用了一个for循环来控制LED灯的闪烁,每次循环都会让LED灯点亮和熄灭一次,并且延时一段时间。最后,我们使用了一个简单的延时函数delay()来实现延时操作。
需要注意的是,这里的延时函数是非常简单粗暴的,只是简单地循环了一段时间。在实际应用中,你可能需要使用更加精确的延时函数来控制LED灯的闪烁时间。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)