当前位置:网站首页>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);
}
边栏推荐
- Detailed analysis of message signals occupying multiple bytes and bits
- FreeRTOS timer group
- Keil serial port redirection
- MAX6675 usage notes
- 期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.
- Application of stack -- using stack to realize bracket matching (C language implementation)
- Promise async/await
- Can introduction
- [most complete] install MySQL on a Linux server
- Binary tree traversal
猜你喜欢

FreeRTOS timer group

Basic knowledge of system software development

Application of stack -- using stack to realize bracket matching (C language implementation)

Network security ARP protocol and defense

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)

期末复习-PHP学习笔记6-字符串处理

halcon:读取摄像头并二值化

MySQL encounters the problem of expression 1 of select list is not in group by claim and contains nonaggre
![Experiment 1: comprehensive experiment [process on]](/img/19/6c6e18d7e1f042bfd3ee4832b78542.png)
Experiment 1: comprehensive experiment [process on]

Realization of dissolve effect in unity and its principle analysis
随机推荐
网络安全-VLAN和Tunk方法详解
MySQL encounters the problem of expression 1 of select list is not in group by claim and contains nonaggre
What does the real name free domain name mean?
Minecraft 1.16.5 module development (50) guide book
All errors reported by NPM
LabVIEW program code update is slow
How to quickly delete routing in Ad
app quits unexpectedly
LabVIEW程序代码更新缓慢
Network security - packet capture and IP packet header analysis
QT common macro definitions
RT thread kernel application development message queue experiment
PMIC power management
Qtcreator debug code after configuring CDB debugger view variable value display card
已解决:initialize specified but the data directory has files in it. Aborting
网络安全-ARP协议和防御
C language - student achievement management system
[introduction to Expo application] v Expert recommendation letter template
TC397 QSPI(CPU)
B站首个UP主付费观看视频还是来了!价格“劝退”网友