基于库函数STM32流水灯的实现

不想详细说了,直接甩代码!里面乱码的先不用管,kell编辑器惹的祸

KEY_LED.c文件:

/**
  ******************************************************************************
  * @file    KEY_LED.c
  * @author  fire
  * @version V1.0
  * @date    2016-06-11
  * @brief   °´¼ü¿ØÖÆled
  ******************************************************************************
  * @attention
**/
	
#include "stm32f10x.h"

void setDelay(__IO uint32_t nCount)	 //¼òµ¥µÄÑÓʱº¯Êý
{
	for(; nCount != 0; nCount--);
}
void setGPIO(GPIO_TypeDef * GP,uint16_t IO,int TYPE){
	switch(TYPE){
		case 0:GP->BRR=IO; break; 
		case 1:GP->BSRR=IO; break;
		case 2:GP->ODR ^=IO; break;
		default:break;
	}
}
void LED_Init(void)
{		
		
		GPIO_InitTypeDef GPIO_InitStructure;//¶¨ÒåÒ»¸öGPIO_InitTypeDefÀàÐ͵ĽṹÌå
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); //¿ªÆôGPIOFµÄÍâÉèʱÖÓ
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//ÉèÖÃÒý½ÅģʽΪͨÓÃÍÆÍìÊä³ö 
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ÉèÖÃÒý½ÅËÙÂÊΪ50MHz 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;//Ñ¡ÔñÒª¿ØÖƵÄGPIOFÒý½Å
		GPIO_Init(GPIOF,&GPIO_InitStructure);//µ÷Óÿ⺯Êý£¬³õʼ»¯GPIO_Pin_All
		setGPIO(GPIOF,GPIO_Pin_All,0);//¹Ø±ÕËùÓÐledµÆ
}
void KEY_Init(void) //IO³õʼ»¯
{ 
		GPIO_InitTypeDef GPIO_InitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOC,ENABLE);//ʹÄÜPORTDʱÖÓ
		GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13;//PC13
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀ­ÊäÈë GPIO_Mode_IPU/GPIO_Mode_IPD
		GPIO_Init(GPIOC,&GPIO_InitStructure);//³õʼ»¯GPIOE0
		GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;//PE0
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀ­ÊäÈë GPIO_Mode_IPU/GPIO_Mode_IPD
		GPIO_Init(GPIOE,&GPIO_InitStructure);//³õʼ»¯GPIOE0
}
int scanKey(){	 
	int key1=GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_0);
	int key2=GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13);
	if(key1==0&&key2==0){
			setDelay(0x000100);//È¥¶¶¶¯ 
			if(key1==0&&key2==0){
				return 3;
			}
			return 0;
	}else if(key1==0||key2==0){
			setDelay(0x000100);//È¥¶¶¶¯ 
			if(key1==0){return 1;}
			else{
				return 2;
			}
	}else{		
			return 0;// ÎÞ°´¼ü°´ÏÂ
	}
}
void key_led(int key){
	__IO uint32_t delaytime=0x030000;
	/* ·½·¨2£¬Ê¹Óù̼þ¿â¿ØÖÆIO */
		if(key==1){
			setGPIO(GPIOF,GPIO_Pin_0,2);			  //±ä»»×´Ì¬
			setGPIO(GPIOF,GPIO_Pin_7,2);
			setDelay(delaytime*3);
			setGPIO(GPIOF,GPIO_Pin_1,2);
			setGPIO(GPIOF,GPIO_Pin_6,2);
			setDelay(delaytime*3);
			setGPIO(GPIOF,GPIO_Pin_2,2);
			setGPIO(GPIOF,GPIO_Pin_5,2);
			setDelay(delaytime*3);
			setGPIO(GPIOF,GPIO_Pin_3,2);
			setGPIO(GPIOF,GPIO_Pin_4,2);
			setDelay(delaytime*3);
		}else if(key==2){
			setGPIO(GPIOF,GPIO_Pin_4,2);			  //±ä»»×´Ì¬
			setGPIO(GPIOF,GPIO_Pin_3,2);
			setDelay(delaytime*3);
			setGPIO(GPIOF,GPIO_Pin_5,2);
			setGPIO(GPIOF,GPIO_Pin_2,2);
			setDelay(delaytime*3);
			setGPIO(GPIOF,GPIO_Pin_6,2);
			setGPIO(GPIOF,GPIO_Pin_1,2);
			setDelay(delaytime*3);
			setGPIO(GPIOF,GPIO_Pin_7,2);
			setGPIO(GPIOF,GPIO_Pin_0,2);
			setDelay(delaytime*3);
		}else if(key==3){
			setGPIO(GPIOF,GPIO_Pin_All,2);
			setDelay(delaytime);
		}else{			
			setGPIO(GPIOF,GPIO_Pin_0,2);			  //±ä»»×´Ì¬
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_1,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_2,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_3,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_4,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_5,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_6,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_7,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_0,2);			  //±ä»»×´Ì¬
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_1,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_2,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_3,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_4,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_5,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_6,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_7,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_7,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_6,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_5,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_4,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_3,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_2,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_1,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_0,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_7,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_6,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_5,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_4,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_3,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_2,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_1,2);
			setDelay(delaytime);
			setGPIO(GPIOF,GPIO_Pin_0,2);
			setDelay(delaytime);
		}
}

KEY_LED.h文件,之前使用AVR都是可以直接include点C文件的,这个好像不行,还必须自己弄个点h文件过度

#include "stm32f10x.h"
void setDelay(__IO uint32_t nCount);	 //¼òµ¥µÄÑÓʱº¯Êý
void setGPIO(GPIO_TypeDef * GP,uint16_t IO,int TYPE);
void LED_Init(void);
void KEY_Init(void); //IO³õʼ»¯
int scanKey(void);
void key_led(int key);

main.c文件,带有执行入口的哦!

#include "stm32f10x.h"
#include "KEY_LED.h"
#include <stdio.h>

int main(void)
{	
  int key=0,flag=0;
	LED_Init();	 
	KEY_Init();
	NVIC_Configuration();
	while(1){
		key=scanKey();
		if(key==1)flag=1;
		if(key==2)flag=2;
		if(key==3)flag=3;
		key_led(flag);
	}
}

 

转载于:https://my.oschina.net/hibony/blog/703005

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值