Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_rcc.h"
- #define LED1 GPIOD, GPIO_Pin_12
- #define LED2 GPIOD, GPIO_Pin_13
- #define LED3 GPIOD, GPIO_Pin_14
- #define LED4 GPIOD, GPIO_Pin_15
- #define button GPIOA, GPIO_Pin_0
- GPIO_InitTypeDef GPIO_InitStruct;
- static void GPIO_setup(void)
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12; // we want to configure all LED GPIO pins
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; // we want the pins to be an output
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this sets the GPIO modules clock speed
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this sets the pullup / pulldown resistors to be inactive
- GPIO_Init(GPIOD, &GPIO_InitStruct);
- }
- static void button_setup(void)
- {RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; // we want to configure PA0
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; // we want it to be an input
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//this sets the GPIO modules clock speed
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; // this enables the pulldown resistor --> we want to detect a high level
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- void clearBits()
- {
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15);
- }
- void lightLed(int number)
- {
- clearBits();
- switch(number)
- {
- case 0:
- {
- clearBits();
- GPIO_SetBits(GPIOD, GPIO_Pin_12);
- }
- case 1:
- {
- clearBits();
- GPIO_SetBits(GPIOD, GPIO_Pin_13);
- }
- case 2:
- {
- clearBits();
- GPIO_SetBits(GPIOD, GPIO_Pin_14);
- }
- case 3:
- {
- clearBits();
- GPIO_SetBits(GPIOD, GPIO_Pin_15);
- }
- case 4:
- {
- }
- }
- }
- void lock()
- {
- while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
- {
- }
- }
- void Delay(__IO uint32_t nCount)
- {
- while(nCount--)
- {
- }
- }
- void task_1()
- {
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
- {
- GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
- }
- else
- {
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15);
- }
- }
- void task_2()
- {
- int a = 0;
- while(1)
- {
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
- {
- a++;
- lock();
- Delay(0xFFFF);
- }
- if(a == 0)
- {
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
- }
- if(a == 1)
- {
- GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
- }
- if(a == 2) a = 0;
- }
- }
- void task_3()
- {
- int a = 0;
- while(1)
- {
- if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0 == 1))
- {
- a++;
- lock();
- Delay(0xFFFF);
- }
- if(a == 0)
- {
- GPIO_SetBits(GPIOD, GPIO_Pin_12);
- GPIO_ResetBits(GPIOD, GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15 );
- }
- if(a == 1)
- {
- GPIO_SetBits(GPIOD, GPIO_Pin_13);
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_14 |GPIO_Pin_15 );
- }
- if(a == 2)
- {
- GPIO_SetBits(GPIOD, GPIO_Pin_14);
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_15 );
- }
- if(a == 3)
- {
- GPIO_SetBits(GPIOD, GPIO_Pin_15);
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 );
- }
- if(a == 4) a = 0;
- //Delay(0x9FFFF);
- }
- }
- int main(void)
- {
- GPIO_setup();
- button_setup();
- GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15 );
- task_2();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement