0% found this document useful (0 votes)
15 views4 pages

Kody

Uploaded by

maciek3015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Kody

Uploaded by

maciek3015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

static const uint16_t LED_PIN[]={ LED1_Pin,LED2_Pin,LED3_Pin,LED4_Pin };

static GPIO_TypeDef* const LED_PORT[]={ LED1_GPIO_Port, LED2_GPIO_Port,


LED3_GPIO_Port, LED4_GPIO_Port };
void led_set(int led, bool turn_on)
{
GPIO_PinState state = turn_on ? GPIO_PIN_SET : GPIO_PIN_RESET;

if (led >= 0 && led < 4) {


HAL_GPIO_WritePin(LED_PORT[led], LED_PIN[led], state);

}
}

typedef struct {
GPIO_TypeDef* port;
uint16_t pin;
}PIN_T;

static const PIN_T LED[]={


{LED1_GPIO_Port, LED1_Pin},
{LED2_GPIO_Port, LED2_Pin},
{LED3_GPIO_Port, LED3_Pin},
{LED4_GPIO_Port, LED4_Pin}
};
void led_set(int led, bool turn_on)
{
GPIO_PinState state = turn_on ? GPIO_PIN_SET : GPIO_PIN_RESET;

if (led >= 0 && led < 4) {


HAL_GPIO_WritePin(LED[led].port, LED[led].pin, state);

}
}
typedef struct
{
GPIO_TypeDef* port;
uint16_t pin;
}led_t;

static const led_t LED[]={


{LED1_GPIO_Port, LED1_Pin},
{LED2_GPIO_Port, LED2_Pin},
{LED3_GPIO_Port, LED3_Pin},
{LED4_GPIO_Port, LED4_Pin},
};

void led_set (int led, bool turn_on)


{
GPIO_PinState state= (turn_on) ? GPIO_PIN_SET : GPIO_PIN_RESET;

if (led>=0 && led<5)


{
HAL_GPIO_WritePin(LED[led].port, LED[led].pin, state);
}}
while (1)
{
for (int i = 0; i < 4; i++) {
// zapal diodę
led_set(i, true);
// poczekaj 100 ms
HAL_Delay(100);
// zgaś diodę
led_set(i, false);
}

if (zamiana) {
int i;
for (i = 0; i < 4; ++i) {
led_set(i, true);
HAL_Delay(200);
led_set(i, false);
}
zamiana = false;
}
else{
int i;
for (i = 3; i >= 0; --i) {
led_set(i, true);
HAL_Delay(200);
led_set(i, false);
}
zamiana = true;
}
bool button_is_press(void)
{
if (HAL_GPIO_ReadPin(button_GPIO_Port, button_Pin)==GPIO_PIN_RESET) {
return true;
}
else
return false;

int i= 0;
led_set(i, true);
while (1)
{
if (button_is_press()) {
led_set(i, false);
if (++i==4) {
i=0;
}
led_set(i, true);
while (button_is_press())
{}
}

bool is_button_pressed(int button) {


switch (button) {
case 0:
if (HAL_GPIO_ReadPin(USER_BUTTON_GPIO_Port, USER_BUTTON_Pin) ==
GPIO_PIN_RESET) {
return true;
} else {
return false;
}
case 1:
if (HAL_GPIO_ReadPin(USER_BUTTON2_GPIO_Port, USER_BUTTON2_Pin) ==
GPIO_PIN_RESET) {
return true;
} else {
return false;
}

default:
return false;
}
}

while (1)
{
if (is_button_pressed(0)) {
// Po wcisnieciu przycisku wylacz diodę
led_set(led, false);

// Zwieksz zawartosc zmiennej led


led++;
// Sprawdz czy nie przekracza zakresu
if (led >= 10) {
led = 0;
}

// Wlacz kolejna diode


led_set(led, true);

// czekamy na zwolnienie przycisku


while (is_button_pressed(0)) {}
}

if (is_button_pressed(1)) {
// Po wcisnieciu przycisku wylacz diodę
led_set(led, false);

// Zmniejsz zawartosc zmiennej led


led--;
// Sprawdz czy nie przekracza zakresu
if (led < 0) {
led = 9;
}

// Wlacz kolejna diode


led_set(led, true);

// czekamy na zwolnienie przycisku


while (is_button_pressed(1)) {}
}

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */


}

You might also like