当前位置:网站首页>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
		}
	}

原网站

版权声明
本文为[weixin_ forty-six million one thousand two hundred and twenty-o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160542201965.html