1 | #include "stm32f4xx_conf.h"
|
2 |
|
3 | void Delay(__IO uint32_t nCount);
|
4 | void initGPIO(void);
|
5 | void WaitForHSE(void);
|
6 |
|
7 | GPIO_InitTypeDef GPIO_InitStructure;
|
8 |
|
9 | int main(void)
|
10 | {
|
11 | SystemInit();
|
12 | WaitForHSE();
|
13 | initGPIO();
|
14 |
|
15 | uint8_t switch_counter = 0;
|
16 | bool button_state = false;
|
17 |
|
18 | while(1)
|
19 | {
|
20 | switch_counter++;
|
21 |
|
22 | if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1)
|
23 | {
|
24 | if(button_state == true)
|
25 | button_state = false;
|
26 | else
|
27 | button_state = true;
|
28 | }
|
29 |
|
30 | if(button_state == true)
|
31 | {
|
32 | switch(switch_counter)
|
33 | {
|
34 | case 1:
|
35 | GPIO_SetBits(GPIOD, GPIO_Pin_12);
|
36 | break;
|
37 | case 2:
|
38 | GPIO_SetBits(GPIOD, GPIO_Pin_13);
|
39 | break;
|
40 | case 3:
|
41 | GPIO_SetBits(GPIOD, GPIO_Pin_14);
|
42 | break;
|
43 | case 4:
|
44 | GPIO_SetBits(GPIOD, GPIO_Pin_15);
|
45 | break;
|
46 | default:
|
47 | GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
|
48 | switch_counter = 0;
|
49 | }
|
50 | }
|
51 | else
|
52 | GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
|
53 |
|
54 |
|
55 | Delay(0x3FFFFF);
|
56 | }
|
57 | }
|
58 |
|
59 | void initGPIO(void)
|
60 | {
|
61 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
62 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
63 |
|
64 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
65 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
66 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
67 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
68 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
69 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
70 |
|
71 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
72 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
73 | GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
74 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
75 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
76 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
77 | }
|
78 |
|
79 | void Delay(__IO uint32_t nCount)
|
80 | {
|
81 | while(nCount--) {}
|
82 | }
|
83 |
|
84 | void WaitForHSE(void)
|
85 | {
|
86 | RCC_HSEConfig(RCC_HSE_ON);
|
87 | while(!RCC_WaitForHSEStartUp()) {}
|
88 | }
|