基于STM32F103ZE库函数版本以PF1~4为引脚在一个独立按钮控制下,按下第一次独立按钮第一个灯亮按下第二次独立按钮第一个灯灭第二个灯亮按下第三次独立按钮第二个灯灭第三个灯亮,按下第四次独立按钮第三个灯灭第四个灯亮的程序
时间: 2024-06-12 17:05:17 浏览: 9
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); //使能GPIOF时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4; //设置PF1~4为输出模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出速度为50MHz
GPIO_Init(GPIOF, &GPIO_InitStructure); //初始化GPIOF
GPIO_SetBits(GPIOF, GPIO_Pin_1); //初始状态PF1亮
GPIO_ResetBits(GPIOF, GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4); //初始状态PF2~4灭
while (1)
{
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0) //独立按钮按下
{
if (GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_1) == 1) //第一次按下独立按钮
{
GPIO_ResetBits(GPIOF, GPIO_Pin_1); //第一个灯灭
GPIO_SetBits(GPIOF, GPIO_Pin_2); //第二个灯亮
}
else if (GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_2) == 1) //第二次按下独立按钮
{
GPIO_ResetBits(GPIOF, GPIO_Pin_2); //第二个灯灭
GPIO_SetBits(GPIOF, GPIO_Pin_3); //第三个灯亮
}
else if (GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_3) == 1) //第三次按下独立按钮
{
GPIO_ResetBits(GPIOF, GPIO_Pin_3); //第三个灯灭
GPIO_SetBits(GPIOF, GPIO_Pin_4); //第四个灯亮
}
else if (GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_4) == 1) //第四次按下独立按钮
{
GPIO_ResetBits(GPIOF, GPIO_Pin_4); //第四个灯灭
GPIO_SetBits(GPIOF, GPIO_Pin_1); //第一个灯亮
}
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0); //等待独立按钮松开
}
}
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)