标题:
stm32一个按键控制两个led灯
[打印本页]
作者:
hhkk
时间:
2018-6-24 22:01
标题:
stm32一个按键控制两个led灯
stm32一个按键控制两个led灯
/**
******************************************************************************
* @file GPIO/IOToggle/main.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main program body.
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
//#include "stm32_eval.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/
/** @addtogroup GPIO_IOToggle
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//GPIO_InitTypeDef GPIO_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
//#define LED0_OFF GPIO_SetBits(GPIOC,GPIO_Pin_8)
//#define LED0_ON GPIO_ResetBits(GPIOC,GPIO_Pin_8)
#define LED0_ON GPIO_SetBits(GPIOC,GPIO_Pin_8)
#define LED0_OFF GPIO_ResetBits(GPIOC,GPIO_Pin_8)
#define LED1_ON GPIO_SetBits(GPIOC,GPIO_Pin_9)
#define LED1_OFF GPIO_ResetBits(GPIOC,GPIO_Pin_9)
#define S1_DOWN GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0
#define S1_UP GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==1
void LEDKEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//GPIO_SetBits(GPIOB,GPIO_Pin_15);
}
void delay_nms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--);
}
}
int main(void)
{
u8 kcnt=0;
SystemInit();
LEDKEY_GPIO_Config();
while (1)
{
if(S1_DOWN)
{
delay_nms(10);
if(S1_DOWN)
{
while(S1_DOWN);
kcnt++;
}
if(kcnt%4==0)
{
LED0_OFF;
LED1_OFF;
}
if(kcnt%4==1)
{
LED0_ON;
LED1_OFF;
}
if(kcnt%4==2)
{
LED0_OFF;
LED1_ON;
}
if(kcnt%4==3)
{
LED0_ON;
LED1_ON;
}
}
}
}
复制代码
一个按键控制两个灯.7z
2018-6-24 22:00 上传
点击文件名下载附件
下载积分: 黑币 -5
510.01 KB, 下载次数: 45, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1