Controlled LED With Button: Microcontroller Lab TP - 02

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

MICROCONTROLLER LAB

TP– 02
Controlled LED with Button

LECTURED BY:
Mr.CHHORN Sopheaktra

Student’s name :
NOU PHANITH ID: E20170586
PHENG PANHAVATH ID: E20170645
PRUM SOPHEARITH. ID: E20170706

ENGINEERING’S DEGREE
DEPARTMENT OF ELECTRONIC AND ENERGY ENGINEERING
INSTITUTE OF TECHNOLOGY OF CAMBODIA
PHNOM-PENH
2019-2020
Control LED with Button

1. Objective
In this practice, we are expected to learn:
1. Configuration GPIO for reading state of button
2. Controlled LED state via button state
3. Bouncing state of button and their problem

2. Schematic

Figure 1. LEDs connect of Pin A0 and A1, Buttons connect to Pin A2 and A3

In figure1, Led1 and Led2 is connected to pin A0 and A1 of black-pill respectively. Moreover,
the button1 and button2 is connected to pin A2 and A3 in order to controlled to the state of
each led. (note: if led is used with 5V signal, a 1K resistance should be connected in series
with each led to prevent the damage due to high current).

1
3. Problem
Problem1). Write a program that allow the led1 and button1 to work as follow:

• The led1 is bright when a button is pressed


• The led1 is dark when a button is released
Problem2). Write a program to perform a task as following:

• The led2 will change its state when the button2 is pushed (press then release)

4. Configuration

Figure 2. Clock Configuration

2
5. Code
The below code is for both problem 1 and problem 2:
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
// enable clock on GPIOA
__HAL_RCC_GPIOA_CLK_ENABLE();

// configure button 1 on A2
GPIO_InitTypeDef buttonStruct_1={0};
buttonStruct_1.Mode = GPIO_MODE_INPUT;
buttonStruct_1.Pull = GPIO_PULLUP;
buttonStruct_1.Pin = GPIO_PIN_2;
buttonStruct_1.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &buttonStruct_1);

//configure button 2 on A3
GPIO_InitTypeDef buttonStruct_2={0};
buttonStruct_2.Mode = GPIO_MODE_INPUT;
buttonStruct_2.Pull = GPIO_PULLUP;
buttonStruct_2.Pin = GPIO_PIN_3;
buttonStruct_2.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &buttonStruct_2);

//configure led 1 on A0
GPIO_InitTypeDef ledStruct_1={0};
ledStruct_1.Mode= GPIO_MODE_OUTPUT_PP;
ledStruct_1.Pull= GPIO_NOPULL;
ledStruct_1.Pin= GPIO_PIN_0;
ledStruct_1.Speed= GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &ledStruct_1);

//configure led 2 on A1
GPIO_InitTypeDef ledStruct_2={0};
ledStruct_2.Mode = GPIO_MODE_OUTPUT_PP;
ledStruct_2.Pull = GPIO_NOPULL;
ledStruct_2.Pin = GPIO_PIN_1;
ledStruct_2.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &ledStruct_2);

uint16_t current = 0;
uint16_t previous = 0;
uint16_t led2 = 0;

/* USER CODE END 2 */

/* Infinite loop */

3
/* USER CODE BEGIN WHILE */

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

uint8_t button1_state = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2);


current = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3);
//Problem 1
if (button1_state == 0)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 1);
}
else
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 0);
}

//Problem 2
if (current != previous)
{
if (current == 1)
{
if (led2 == 1) {
led2 = 0;
}
else {
led2 =1;
}
}
previous=current;
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, led2);
}
}

/* USER CODE END 3 */

You might also like