利用库函数编程实现按下按键KEY1发光LED从下到上依次点亮,按下按键KEY2发光LED从上到下依次点亮。 要求,在PROTEUS中画出如下页的电路图,并在keil中编程,生成hex文件,将该hex文件导入到PROTEUS的STM32F103R6元器件中,仿真实现上述功能
时间: 2023-06-20 18:06:52 浏览: 117
由于本人无法提供PROTEUS仿真软件,以下是基于STM32F103C8T6开发板的代码示例,可供参考:
```
#include "stm32f10x.h"
void GPIO_Config(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
GPIO_Config();
while (1)
{
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0) // 按下KEY1
{
for(int i=0; i<8; i++)
{
GPIO_SetBits(GPIOB, GPIO_Pin_0 << i); // 依次点亮LED
Delay(1000000);
}
}
else if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0) // 按下KEY2
{
for(int i=7; i>=0; i--)
{
GPIO_SetBits(GPIOB, GPIO_Pin_0 << i); // 依次点亮LED
Delay(1000000);
}
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_All); // 关闭所有LED
}
}
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
```
在上述代码中,我们通过GPIO_Config函数进行GPIO口的初始化配置,其中PA0和PA1为输入端口,PB0~PB7为输出端口。在主函数中,我们通过GPIO_ReadInputDataBit函数读取按键状态,并通过GPIO_SetBits和GPIO_ResetBits函数控制LED灯的亮灭状态。其中Delay函数为自定义的延时函数,用于控制LED灯点亮的时间。需要注意的是,本代码中按键为低电平有效,即按下时会输出0,未按下时会输出1。
编译并生成HEX文件后,将其烧录到STM32F103C8T6开发板中即可实现LED灯随按键按下的效果。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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)