|
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;
-
- }
-
-
-
- }
- }
- }
复制代码
|
|