zoukankan      html  css  js  c++  java
  • 嵌入式系统——使用STM32的GPIO固件库编程实现简单按键触发流水灯

    LED.h

    #ifndef LED_H
    #define LED_H
    
    #include "stm32f4xx.h"
    
    typedef enum LED_ID_T{
    	LED_D1 = 1,
    	LED_D2,
    	LED_D3,
    	LED_D4
    } LED_ID;
    
    void led_init(void);
    void led_one_on(LED_ID num);
    void led_one_off(LED_ID num);
    void delay(int num);
    void led_loop(void);
    
    #endif
    
    

    LED.c

    #include "LED.h"
    #include "systick.h"
    #define Total_time 0x2000000
    
    void led_init(){
    	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF, ENABLE);
    	
    	GPIO_InitTypeDef p;
    	p.GPIO_Mode = GPIO_Mode_OUT;
    	p.GPIO_OType = GPIO_OType_PP;
    	p.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
    	p.GPIO_PuPd = GPIO_PuPd_NOPULL;
    	p.GPIO_Speed = 	GPIO_Speed_50MHz;
    	GPIO_Init(GPIOF, &p);
    	p.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;
    	GPIO_Init(GPIOE, &p);
    	
    	GPIO_SetBits(GPIOF, GPIO_Pin_9 | GPIO_Pin_10);
    	GPIO_SetBits(GPIOE, GPIO_Pin_13 | GPIO_Pin_14);
    }
    
    void led_one_on(LED_ID led_now){
    	if(led_now == 1){
    		GPIO_ResetBits(GPIOF, GPIO_Pin_9);
    		GPIO_SetBits(GPIOF, GPIO_Pin_10);
    		GPIO_SetBits(GPIOE, GPIO_Pin_13 | GPIO_Pin_14);
    	}
    	else if(led_now == 2){
    		GPIO_ResetBits(GPIOF, GPIO_Pin_10);
    		GPIO_SetBits(GPIOF, GPIO_Pin_9);
    		GPIO_SetBits(GPIOE, GPIO_Pin_13 | GPIO_Pin_14);
    	}
    	else if(led_now == 3){
    		GPIO_ResetBits(GPIOE, GPIO_Pin_13);
    		GPIO_SetBits(GPIOF, GPIO_Pin_9 | GPIO_Pin_10);
    		GPIO_SetBits(GPIOE, GPIO_Pin_14);
    	}
    	else if(led_now == 4){
    		GPIO_ResetBits(GPIOE, GPIO_Pin_14);
    		GPIO_SetBits(GPIOF, GPIO_Pin_9 | GPIO_Pin_10);
    		GPIO_SetBits(GPIOE, GPIO_Pin_13);
    	}
    }
    
    void led_one_off(LED_ID led_now){
    	if(led_now == 1){
    		GPIO_SetBits(GPIOF, GPIO_Pin_9);
    	}
    	else if(led_now == 2){
    		GPIO_SetBits(GPIOF, GPIO_Pin_10);
    	}
    	else if(led_now == 3){
    		GPIO_SetBits(GPIOE, GPIO_Pin_13);
    	}
    	else if(led_now == 4){
    		GPIO_SetBits(GPIOE, GPIO_Pin_14);
    	}
    }
    
    
    void delay(int num){
    	while(num--);
    }
    
    void led_loop(){
    	LED_ID led_now = LED_D1;
    	for (led_now = LED_D1;led_now <= LED_D4;led_now++){
    		led_one_on(led_now);
    		delay(Total_time);
    		//Delay_ms(2000);
    	}
    }
    
    
    
    

    key.h

    #ifndef KEY_H
    #define KEY_H
    
    #include "stm32f4xx.h"
    #include "beep.h"
    #include "LED.h"
    
    void key_init();
    void key_check();
    #endif
    
    
    

    key.c

    #include "key.h"
    int tmp0 = 0;
    
    void key_init(){
    	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE, ENABLE);
    	GPIO_InitTypeDef P;
    	P.GPIO_Pin= GPIO_Pin_0;
    	P.GPIO_Mode = GPIO_Mode_IN;
    	P.GPIO_Speed = GPIO_Speed_50MHz;
    		//P.GPIO_OType = GPIO_OType_PP;
    	P.GPIO_PuPd = GPIO_PuPd_NOPULL;	
    	GPIO_Init(GPIOA,&P); //初始化key0
    	P.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
    	GPIO_Init(GPIOE,&P); //初始化key1,key2,key3
    }
    
    void key_check(){
    	if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)){//key0被按下
    		//flag1 ? led_one_on(LED_D1) : led_one_off(LED_D1); flag1 ^= 1;
    		//GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
    		tmp0 = 1;
    		//delay(5500);
    	}
    	else if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)){//key1被按下
    		//flag2 ? led_one_on(LED_D2) : led_one_off(LED_D2); flag2 ^= 1;
    		GPIO_ToggleBits(GPIOF, GPIO_Pin_10);
    		delay(5500);
    	}
    	else if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)){//key2被按下
    		//flag3 ? led_one_on(LED_D3) : led_one_off(LED_D3); flag3 ^= 1;
    		GPIO_ToggleBits(GPIOE, GPIO_Pin_13);
    		delay(5500);
    	}
    	else if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)){//key3被按下
    		//flag4 ? led_one_on(LED_D4) : led_one_off(LED_D4); flag4 ^= 1;
    		GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
    		delay(5500);
    		GPIO_SetBits(GPIOF, GPIO_Pin_8);//置1,蜂鸣器响起
    		delay(0x666666);
    		GPIO_ResetBits(GPIOF, GPIO_Pin_8);//置0
    	}
    }
    
    
    
    

    main函数

    extern int tmp0;
    int main(void)
    {
      led_init();//初始化LED
      key_init();//初始化key
    
      /* Infinite loop */
      while (1)
      {
      	key_check();//等待key响应
    	if(tmp0) led_loop();
      }
    }
    
    你将不再是道具,而是成为人如其名的人
  • 相关阅读:
    字节跳动在 Go 网络库上的实践
    TCP报文段的首部格式 20字节的固定首部
    网易公开课 文件描述符 索引
    网易新闻App架构重构实践:DDD正走向流行
    货 | 携程是如何做AB实验分流的
    Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol
    避免重复提交?分布式服务的幂等性设计! 架构文摘 今天 点击蓝色“架构文摘”关注我哟 加个“星标”,每天上午 09:25,干货推送! 来源:https://www.cnblogs.com/QG-whz/p/10372458.html 作者:melonstreet
    前置时间(Lead Time),也称前置期、备货周期
    滴滴业务研发的精益实践
    DevOps运动的缘起 将DevOps想象为一种编程语言里面的一个接口,而SRE类实现了这个接口
  • 原文地址:https://www.cnblogs.com/wsl-lld/p/14804250.html
Copyright © 2011-2022 走看看