当前位置:网站首页>STM32F103按键控制LED程序
STM32F103按键控制LED程序
2022-07-21 00:06:00 【卖身买镜头】
按键的key.h和KEY.C
按键KEY1为GPIOA的0脚
按键KEY1为GPIOA的1脚
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
//#define KEY1 PAin(0)// PA0
//#define KEY2 PAin(1)// PA1
#define KEYPORT GPIOA //定义IO接口组
#define KEY1 GPIO_Pin_0 //定义IO接口按键KEY1为GPIOA的0脚
#define KEY2 GPIO_Pin_1 //定义IO接口按键KEY1为GPIOA的1脚
void KEY_Init(void);//初始化
#endif
KEY.C
#include "key.h"
void KEY_Init(void){
//微动开关的接口初始化
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO的初始化枚举结构
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY1 | KEY2; //选择端口号(0~15或all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //选择IO接口工作方式 //上拉电阻
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置IO接口速度(2/10/50MHz) 输入模式不需要设置端口速率
GPIO_Init(KEYPORT,&GPIO_InitStructure);
}
/**************************************************** 程序名: 按键控制LED程序 硬件支持: STM32F103C8 外部晶振8MHz RCC函数设置主频72MHz 说明: # 本模板加载了STM32F103内部的RCC时钟设置,并加入了利用滴答定时器的延时函数。 # 可根据自己的需要增加或删减。 *********************************************************************************************/
#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "key.h"
int main (void){
//主程序
//初始化程序
RCC_Configuration(); //时钟设置
LED_Init();//LED初始化
KEY_Init();//按键初始化
while(1){
//主循环
//示例1:无锁存
// if(GPIO_ReadInputDataBit(KEYPORT,KEY1)){ //读按键接口的电平
// GPIO_ResetBits(LEDPORT,LED1); //LED灯都为低电平(0)
// }else{
// GPIO_SetBits(LEDPORT,LED1); //LED灯都为高电平(1)
// }
//示例2:无锁存
// GPIO_WriteBit(LEDPORT,LED1,(BitAction)(!GPIO_ReadInputDataBit(KEYPORT,KEY1)));
//示例3:有锁存
//key1
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1))
{
//读按键接口的电平
delay_ms(20); //延时去抖动
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1))
{
//读按键接口的电平
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); //LED取反
while(!GPIO_ReadInputDataBit(KEYPORT,KEY1)); //等待按键松开
}
}
//key2
if(!GPIO_ReadInputDataBit(KEYPORT,KEY2))
{
//读按键接口的电平
delay_ms(20); //延时去抖动
if(!GPIO_ReadInputDataBit(KEYPORT,KEY2))
{
//读按键接口的电平
GPIO_WriteBit(LEDPORT,LED2,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED2))); //LED取反
while(!GPIO_ReadInputDataBit(KEYPORT,KEY2)); //等待按键松开
}
}
//示例4:有锁存
// if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){ //读按键接口的电平
// delay_ms(20); //延时20ms去抖动
// if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){ //读按键接口的电平
// //在2个LED上显示二进制加法
// a++; //变量加1
// if(a>3){ //当变量大于3时清0
// a=0;
// }
// GPIO_Write(LEDPORT,a); //直接数值操作将变量值写入LED(LED在GPIOB组的PB0和PB1上)
// while(!GPIO_ReadInputDataBit(KEYPORT,KEY1)); //等待按键松开
// }
// }
}
}
边栏推荐
猜你喜欢
王者荣耀商城异地多活部署设计
Rsync combined with inotify to realize real-time file synchronization (2)
ICML 2022 | 教程效度,可靠性和意义:可复现机器学习的统计方法教程
PHP FPM custom ZABBIX monitoring
What have I experienced to become a harder tester than development?
Yii framework installation steps (Yii Framework version 1.1.20, time is November 2018)
Design of online blog system
音视频开发进阶|第四讲:音频自动增益控制 AGC
Some functions of C (39) about string
[featured] the structure of MySQL B-tree and b+tree?
随机推荐
XML parsing
C#(四十三)之线程Mutex互斥
Practical guidance of interface automation testing (Part 2): idea of setting assertion of interface automation testing
Redis implements distributed global unique ID
模块学习(五)——矩阵键盘
[CS231N]Notes_1-Introduction
SAP Fiori topic 2: using webide to build Fiori with navigation bar
传输层 使用Web代理访问网站
ICML2022奖项公布:15篇杰出论文,复旦、厦大、上交大研究入选
rsync 结合 inotify 实现文件实时同步(二)
Code之模拟
The transport layer uses a web proxy to access websites
Path of C (33)
Advanced audio and video development | Lecture 4: audio automatic gain control AGC
国外LEAD行业到底什么情况?心态崩溃直到放弃
Flutter | print 及 dio 打印不完整的问题
成为比开发更硬气的测试人,我都经历了什么?
Math.abs一定大于等于0吗?
XML解析
YII框架安装步骤(yii框架版本1.1.20,时间是2018/11)