当前位置:网站首页>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
}
}
边栏推荐
- Stm32g0 porting FreeRTOS
- Introduction to ecostruxure (1) IEC61499 new scheme
- 03 - programming framework: Division of application layer, middle layer and driver layer in bare metal programming
- 网络安全-ARP协议和防御
- Can introduction
- How to use string branches for switch case
- Mailbox application routine of running wild fire RT thread
- QT elementary notes
- js创建pdf文件
- Keil serial port redirection
猜你喜欢

Resolution: div failed to get keyboard event

All errors reported by NPM

期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.

C language - student achievement management system

Raspberry pie trivial configuration

How to determine the size of the platform byte order?

Binary tree traversal

Variable storage unit and pointer

Local unloading traffic of 5g application

Network security ARP protocol and defense
随机推荐
Quick placement of devices by module in Ad
视频播放器(一):流程
Linu foundation - zoning planning and use
DXP software uses shortcut keys
Socket socket programming -- UDP
Assembly learning register
What does the real name free domain name mean?
How crazy are young people in sideline industry: 3000 monthly salary and 3W sideline income
Video player (I): process
nRF52832 GPIO LED
已解决:initialize specified but the data directory has files in it. Aborting
网络安全-路由原理
期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.
手机开户股票开户安全吗?开户需要准备什么?
Stm32g0 porting FreeRTOS
Ad usage notes
Keil plug-in Usage Summary
The first up Master of station B paid to watch the video still came! Price "Persuading" netizens
Write and run the first go language program
PMIC power management