当前位置:网站首页>Lesson 5 - key control LED
Lesson 5 - key control LED
2022-07-27 02:17:00 【Ruomu·】
Catalog
2. Brief introduction GPIO_ReadOutputDataBit function
Preface
I introduced some GPIO The correlation function of , Integration , Write a small experiment —— Key control LED
One 、 connection

Two 、 Modular programming
Because the experiment involves multiple peripherals , All the code written in one file is too messy , Nor is it suitable for directly using a module in the future
( Because some code is relatively simple , For example, delay function code , Just use it directly in the future ).
1. Write LED part
LED.c
It is recommended to create a new... In the project folder hardware Folder , In the future, the peripherals will be placed in hardware in , As shown in the figure :

On the first code :LED.c
#include "stm32f10x.h" // Device header
void LED_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2); // Keep... On power LED It's out
}
void LED1_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1); // Light on
}
void LED1_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1); // The light goes out
}
void LED1_turn(void) // Press the key If the output 0, Set up 1, Otherwise, set 0, In this way, the level reversal is realized , namely GPIO_ReadOutputDataBit The meaning of function
{
if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_1)==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
}
void LED2_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
void LED2_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
void LED2_turn(void)
{
if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_2)==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
}
stay LED Modular GPIO In the initialization , First of all, I'll give it to you LED High level , Ensure that when powered on LED Is off .
2. Brief introduction GPIO_ReadOutputDataBit function
stay LED Inversion level function involves GPIO_ReadOutputDataBit function , So the following is a brief introduction to this function :
This function functions as , Read the output of the specified port pin
Let's look at the function definition first :
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
uint8_t bitstatus = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
{
bitstatus = (uint8_t)Bit_SET;
}
else
{
bitstatus = (uint8_t)Bit_RESET;
}
return bitstatus;
}
You can see , This function is the same as before SetBits Equal function usage is no different , At this time LED.c In the said : When the key is pressed , If the output 0, Set up 1, Otherwise, set 0, In this way, the level reversal is realized , This is also used at this moment GPIO_ReadOutputDataBit The meaning of function .
LED.h
Definition of header file and 51 There is no difference :
Code up :
#ifndef __LED_H__
#define __LED_H__
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_turn(void);
void LED2_turn(void);
#endif
3. Write key part
key.c
Code up :
#include "stm32f10x.h" // Device header
#include "Delay.h"
// Key initialization
void key_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStrucTure;
GPIO_InitStrucTure.GPIO_Mode = GPIO_Mode_IPU; // Key , Pull up with
GPIO_InitStrucTure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
GPIO_InitStrucTure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStrucTure);
}
// Key detection
uint8_t key_GetNum(void)
{
uint8_t key_Num = 0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0) // Press the key
{
Delay_ms(20); // Press the key to shake off
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);// Test release , Because there is no action until the key is pressed and the hand is released
Delay_ms(20); // Press the key to shake off
key_Num = 1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
{
Delay_ms(20); // Press the key to shake off
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
Delay_ms(20); // Press the key to shake off
key_Num = 2;
}
return key_Num;
}
Because it involves two buttons , So when writing key initialization , Structure sub item GPIO_PIN The value of is GPIO_Pin_1 | GPIO_Pin_11, Adopt the mode of pull-up input , The key part is also similar to 51 Also pay attention to the problem of key shaking elimination , In this way, the key part is finished .
Last , Call in the main function .
main.c
#include "stm32f10x.h" // Device header
#include "LED.h"
#include "key.h"
#include "Delay.h"
uint8_t key_Num;
int main(void)
{
LED_Init();
key_Init();
while(1)
{
key_Num=key_GetNum();
if(key_Num==1)
{
LED1_turn();
}
if(key_Num==2)
{
LED2_turn();
}
}
}
边栏推荐
- 6.30 didi surface warp (one side + two sides)
- 定时器中断实验
- Experiment of OSPF in mGRE environment
- RIP V2 的简单应用(v2的配置、宣告、手工汇总、RIPV2的认证、沉默接口、加快收敛)
- NAT (network address translation protocol)
- ospf协议概述以及基础概念
- [explain C language in detail] takes you to play with functions
- Golang bufio Reader 源码详解
- ACM mode input and output exercise
- [FPGA tutorial case 29] the second DDS direct digital frequency synthesizer based on FPGA - Verilog development
猜你喜欢

HCIA静态路由基础模拟实验

初识C语言(1)

定时器中断实验

7.16 多益网络笔试

Simple application of rip V2 (V2 configuration, announcement, manual summary, ripv2 authentication, silent interface, accelerating convergence)

【volatile原理】volatile原理

OSPF在MGRE环境下配置及LSA的优化---减少LSA更新量(汇总、特殊区域)

动态路由ofps协议配置

OSPF protocol knowledge summary

Text to image论文精读DF-GAN:A Simple and Effective Baseline for Text-to-Image Synthesis一种简单有效的文本生成图像基准模型
随机推荐
6.28 Dahua written examination
7.16 written examination of Duoyi network
C语言——字符和字符串、算术运算符、类型转换
微信小程序:用户微信登录流程(附:流程图+源码)
Three methods that can effectively fuse text and image information -- feature stitching, cross modal attention, conditional batch normalization
Text to image paper intensive reading rat-gan: recursive affine transformation for text to image synthesis
静态综合实验(静态路由、环回接口、缺省路由、空接口、浮动静态的综合练习)
ospf协议概述以及基础概念
C语言——第一个程序、打印、变量和常量
Nat网络地址转换实验
Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
STM32 HAL库串口(UART/USART)调试经验(一)——串口通信基础知识+HAL库代码理解
Specify that SQL only supports select syntax
三种能有效融合文本和图像信息的方法——特征拼接、跨模态注意、条件批量归一化
广域网技术实验
OGeek Meetup第一期,携手CubeFS火热来袭
Simple application of rip V2 (V2 configuration, announcement, manual summary, ripv2 authentication, silent interface, accelerating convergence)
Golang - sync包的使用 (WaitGroup, Once, Mutex, RWMutex, Cond, Pool, Map)
[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction
7.7 SHEIN希音笔试