当前位置:网站首页>STM32 key control LED
STM32 key control LED
2022-06-30 07:28:00 【weixin_ forty-six million one thousand two hundred and twenty-o】
mai.c
#include "stm32f10x.h" //STM32 The header file
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "key.h"
extern unsigned char flag_1ms;
u8 sysstat=0;
int main (void)
{
uint8_t key;
SysTick_Config(72000000/1000); // timing 1 millisecond
Led_Init(); //led gpio initialization
Key_Init(); // Key initialization
while(1)
{
if(flag_1ms==1) //1 Once in milliseconds
{
flag_1ms=0;
Led_Poll(); //LED Cycle show
key = Key_Scan();
if(key==1)
{
sysstat++;
if(sysstat>3){
sysstat=0;}
}
else if(key==2)
{
if(sysstat==0){
sysstat=3;}
else{
sysstat--;}
}
switch(sysstat)
{
case 0:LED1_ON;LED2_OFF;LED3_OFF;break; //led1 bright
case 1:LED2_ON;LED1_OFF;LED3_OFF;break; //led2 bright
case 2:LED3_ON;LED1_OFF;LED2_OFF;break; //led3 bright
case 3:Led_Poll();break; //LED Cycle show
}
}
}
}
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
/* Definition LED Connected GPIO port , The user only needs to modify the following code to change the control LED Pin */
// R- Red
#define LED1_GPIOB GPIOB /* GPIO port */
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO Port clock */
#define LED1_GPIO_PIN GPIO_Pin_5 /* Connect to SCL Clock line GPIO */
// G- green
#define LED2_GPIOB GPIOB /* GPIO port */
#define LED2_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO Port clock */
#define LED2_GPIO_PIN GPIO_Pin_0 /* Connect to SCL Clock line GPIO */
// B- Blue
#define LED3_GPIOB GPIOB /* GPIO port */
#define LED3_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO Port clock */
#define LED3_GPIO_PIN GPIO_Pin_1 /* Connect to SCL Clock line GPIO */
// Method 1:
#define LED1_ON GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_RESET) // Lighten up LED1
#define LED1_OFF GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_SET) // Extinguish LED1
#define LED2_ON GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_RESET) // Lighten up LED2
#define LED2_OFF GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_SET) // Extinguish LED2
#define LED3_ON GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_RESET) // Lighten up LED2
#define LED3_OFF GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_SET) // Extinguish LED2
// Method 2:
//#define LED1_ON GPIO_WriteBit(GPIOB,GPIO_Pin_0,(BitAction)(0)); // Lighten up LED1
//#define LED1_OFF GPIO_WriteBit(GPIOB,GPIO_Pin_0,(BitAction)(1)); // Extinguish LED1
//#define LED2_ON GPIO_WriteBit(GPIOB,GPIO_Pin_1,(BitAction)(0)); // Lighten up LED2
//#define LED2_OFF GPIO_WriteBit(GPIOB,GPIO_Pin_1,(BitAction)(1)); // Extinguish LED2
//#define LED3_ON GPIO_WriteBit(GPIOB,GPIO_Pin_5,(BitAction)(0)); // Lighten up LED2
//#define LED3_OFF GPIO_WriteBit(GPIOB,GPIO_Pin_5,(BitAction)(1)); // Extinguish LED2
// Method 3:
//#define LED1_OFF GPIO_SetBits(GPIOB,GPIO_Pin_0); // LED The lights are high level
//#define LED1_ON GPIO_ResetBits(GPIOB,GPIO_Pin_0); // LED The lights are all low level
//#define LED2_OFF GPIO_SetBits(GPIOB,GPIO_Pin_1); // LED The lights are high level
//#define LED2_ON GPIO_ResetBits(GPIOB,GPIO_Pin_1); // LED The lights are all low level
//#define LED3_OFF GPIO_SetBits(GPIOB,GPIO_Pin_5); // LED The lights are high level
//#define LED3_ON GPIO_ResetBits(GPIOB,GPIO_Pin_5); // LED The lights are all low level
void Led_Init(void); //led gpio initialization
void Led_OFF(void); // LED3 Total destruction
void Led_Poll(void); //LED Cycle show
#endif /* __LED_H */
led.c
#include "led.h"
typedef struct //led Variable members
{
u8 timer;
u16 num;
}led_ctr_type;
led_ctr_type led_ctr = {
0,0}; // Variable initialization
void Led_Init(void)
{
/* Define a GPIO_InitTypeDef Type of structure */
GPIO_InitTypeDef GPIO_InitStructure;
/* Turn on LED dependent GPIO Peripheral clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
/* Choose what you want to control GPIO Pin */
GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN|LED2_GPIO_PIN|LED3_GPIO_PIN;
/* Set pin mode to universal push pull output */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/* Set the pin rate to 50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* Call library function , initialization GPIO*/
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Led_OFF(void) // LED3 Total destruction
{
LED1_OFF;
LED2_OFF;
LED3_OFF;
}
void Led_Poll(void) // LED Cycle show
{
switch(led_ctr.timer) //index Every time 500 Millisecond plus once , Alternate execution 500 millisecond case(X) Jump once
{
case 0:LED1_ON; break; // LED1 bright
case 1:Led_OFF(); break; // LED Total destruction
case 2:LED2_ON;break; //LED2 bright
case 3:Led_OFF(); break; // LED Total destruction
case 4:LED3_ON;break; //LED3 bright
case 5:Led_OFF(); break; // LED Total destruction
}
led_ctr.num++;
if(led_ctr.num>=500) // timing 500 millisecond
{
led_ctr.num=0;
led_ctr.timer++; //index Every time 500 Millisecond plus once
if(led_ctr.timer>5){
led_ctr.timer=0;} //timer Set less than 5, because case Only write 5
}
}
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#include "led.h"
#include "delay.h"
#define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)
void Key_Init(void);// initialization
u8 Key_Scan(void); // Key scan function
#endif
key.c
#include "key.h"
void Key_Init(void){
// Interface initialization of microswitch
GPIO_InitTypeDef GPIO_InitStructure; // Definition GPIO Initialization enumeration structure
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // Select pin number (0~15 or all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // choice IO How the interface works // Pull down resistance
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; // Select pin number (0~15 or all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // choice IO How the interface works // Pull down resistance
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
u8 Key_Scan(void) // Key scan function
{
static u16 timer1=0,timer2=0;
// Key 1 Handle
if(KEY1==1)
{
timer1++;
if(timer1==10) // timing 10 Millisecond return KEY1
{
return(1);
}
else if(timer1==1000){
timer1=500;return(1);} // Otherwise, press and hold to return the valid value
}
else{
timer1=0;}
// Key 2 Handle
if(KEY2==1)
{
timer2++;
if(timer2==10) // timing 10 Millisecond return KEY1
{
return(2);
}
else if(timer2==1000){
timer2=500;return(2);} // Otherwise, press and hold to return the valid value
}
else{
timer2=0;}
return(0);
}
边栏推荐
- [resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password
- 线程池——C语言
- [resolved] error 1290 (HY000): unknown error 1290
- El input can only input numbers and has a decimal point. At most two digits can be reserved
- SwiftUI打造一款美美哒自定义按压反馈按钮
- Write and run the first go language program
- Promise async/await
- Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
- Adjacency matrix representation of weighted undirected graph (implemented in C language)
- FreeRTOS timer group
猜你喜欢

Basic knowledge of system software development

Ad usage notes
![[resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password](/img/8a/4f369be1d23ced40994626cd3a9561.png)
[resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password

Dynamic memory management

QT common macro definitions

线程池——C语言

03 - programming framework: Division of application layer, middle layer and driver layer in bare metal programming

Adjacency matrix representation of weighted undirected graph (implemented in C language)

Nested if statement in sum function in SQL Server2005

FreeRTOS timer group
随机推荐
Variable storage unit and pointer
Install go language development tools
Sublime text 3 configuring the C language running environment
Cypress nor flash driver - s29glxxxs
I graduated this year, but I don't know what I want to do
SwiftUI打造一款美美哒自定义按压反馈按钮
Detailed analysis of message signals occupying multiple bytes and bits
Merge: extension click the El table table data to expand
Cmake generate map file
FreeRTOS timer group
Digital tube EEPROM key to save value
Graphic explanation pads update PCB design basic operation
C language - student achievement management system
June 29, 2022 -- take the first step with C # -- add decision logic to the code using the "if", "else" and "else if" statements in C #
视频播放器(二):视频解码
解决:div获取不到键盘事件
Implementation of binary search in C language
Network security - routing principle
嵌入式测试流程
视频播放器(一):流程