当前位置:网站首页>STM32 control LED lamp
STM32 control LED lamp
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"
extern unsigned char flag_1ms;
u8 sysstat=0;
int main (void)
{
SysTick_Config(72000000/1000); // timing 1 millisecond
Led_Init(); //led gpio initialization
while(1)
{
if(flag_1ms==1) //1 Once in milliseconds
{
flag_1ms=0;
Led_Poll(); //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
}
}
边栏推荐
猜你喜欢

Dynamic memory management

记录开发过程中无法使用管理员身份修改系统文件问题

我今年毕业,但我不知道我要做什么

RT thread kernel application development message queue experiment

1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability

Network security - routing principle

Double click the idea to solve the problem of downloading again

next InitializeSecurityContext failed: Unknown error (0x80092012) - 吊销功能无法检查证书是否吊销。

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

C language implements sequential queue, circular queue and chain queue
随机推荐
Linu foundation - zoning planning and use
failed to create symbolic link ‘/usr/bin/mysql’: File exists
Video player (II): video decoding
Introduction to go language pointer
Digital tube EEPROM key to save value
C language implements sequential queue, circular queue and chain queue
Cypress nor flash driver - s29glxxxs
Minecraft 1.16.5 module development (50) guide book
Record the problem that the system file cannot be modified as an administrator during the development process
Use of ecostruxure (2) IEC61499 to establish function blocks
Mailbox application routine of running wild fire RT thread
网络安全-路由原理
视频播放器(二):视频解码
Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
grep命令用法
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 #
Nested if statement in sum function in SQL Server2005
网络安全-VLAN和Tunk方法详解
How to batch modify packaging for DXP schematic diagram
Pit stepping record: Supervisor log return information: redis extension is not installed