Professional Documents
Culture Documents
Microcontroller Electronics Lab
Microcontroller Electronics Lab
PRUM SOPHEARITH
E20170706
LECTURED BY:
CHHORN SOPHEAKTRA
ENGINEERING’S DEGREE
DEPARTMENT OF ELECTRICAL AND ENERGY ENGINEERING
INSTITUTE OF TECHNOLOGY OF CAMBODIA
PHNOM PENH
2019-2020
TP3: SYSTICK TIMER AND TASK SCHEDULING
1. Objective
Understanding internal timer Systick
Using Systick timer to set the timing of different task (task scheduling)
Application of timer
2. Equipment
Black pill
ST-link
Break board
Male wire
3 LEDs
2 Buttons
3. Schematic
As shown in figure 1, Led1, Led2, L3are connected to pin A0, A1, and A2of black-pill
respectively. Moreover, the button1 and button2 are connected to pin A3and A4in order to
controlled to the state of each led. (note: if led is used with 5V signal, a 1Kresistance should be
connected in series with each led to prevent the damage due to high current).
4. Problems
Problem 1. By using internal timer (systick), write a program to control two difference tasks as
follow:
Implementation code
MX_GPIO_Init();
uint32_t c_value1=0;
uint32_t p_value1=0;
uint32_t c_value2=0;
uint32_t p_value2=0;
while (1)
{
c_value1 = HAL_GetTick();
c_value2 = HAL_GetTick();
if (c_value1-p_value1>=250)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
p_value1=c_value1;
}
if (c_value2-p_value2>=1000)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
p_value2=c_value2;
}
}
Wiring Implementation
Figure 1
As you can see in figure 1, we connect Pin A0 to green LED and Pin A1 to red
LED. By using this code with this circuit we can see that LED1 toggle every
250ms, and LED2 toggle every 1000ms.
Problem2. Create a task with 50ms timing to remove bouncing effect of the button 1and the
Led 1will be toggled when the button 1 is pressed.
Implementation code
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
__HAL_RCC_GPIOA_CLK_ENABLE();
//configure led 2 on A1
GPIO_InitTypeDef ledStruct_1={0};
ledStruct_1.Mode = GPIO_MODE_OUTPUT_PP;
ledStruct_1.Pull = GPIO_NOPULL;
ledStruct_1.Pin = GPIO_PIN_1;
ledStruct_1.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &ledStruct_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);
uint16_t button = 1;
uint16_tbutton_state = 1;
uint16_t led1 = 0;
uint32_t current;
uint32_t previous=0;
while (1)
{
current=HAL_GetTick();
if (current-previous>=50)
{
previous=current;
button = HAL_GPIO_ReadPin(GPIOA,
GPIO_PIN_3);
if (button != button_state)
{
if (button == 0)
{
if (led1 == 1)
{
led1 = 0;
}
else
{
led1 =1;
}
}
button_state=button;
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,
led1);
}
}
}
Wiring Implementation
Figure 2
In figure 2 we connect green LED to Pin A1 and button1 to Pin A3. By using this
code with this circuit we can see that Led1 toggled when the button 1 is pressed.
Problem3. Based on the code in problem 1-2, write a code to perform the task as below:
When button 2 is pressed, toggle Led 3 at200ms blinking rate
When button 2 is pressed again, stop the toggling
Implementation code
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
//configure led 3 on A2
GPIO_InitTypeDef ledStruct_1={0};
ledStruct_1.Mode = GPIO_MODE_OUTPUT_PP;
ledStruct_1.Pull = GPIO_NOPULL;
ledStruct_1.Pin = GPIO_PIN_2;
ledStruct_1.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &ledStruct_1);
//configure button 2 on A9
GPIO_InitTypeDef buttonStruct_2={0};
buttonStruct_2.Mode = GPIO_MODE_INPUT;
buttonStruct_2.Pull = GPIO_PULLUP;
buttonStruct_2.Pin = GPIO_PIN_9;
buttonStruct_2.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &buttonStruct_2);
uint16_t button2;
uint16_t button2_state=1;
uint32_tcurrent_value;
uint32_tprevious_value=0;
uint32_t current1_value;
uint32_t previous1_value=0;
uint8_t blink=0;
while (1)
{
current_value=HAL_GetTick();
current1_value=HAL_GetTick();
if (current_value-previous_value>=50)
{
previous_value=current_value;
button2=HAL_GPIO_ReadPin(GPIOA,
GPIO_PIN_9);
if (button2 == 0 && button2_state==1)
{
blink =! blink;
button2_state=0;
}
if (button2==1 && button2_state==0)
{
button2_state=1;
}
}
if (blink == 1)
{
if (current1_value-previous1_value>=200)
{
previous1_value=current1_value;
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);
}
}
}
Wiring Implementation
Figure 3
In figure 3 we connect green LED3 to Pin A2 and button2 to Pin A9. By using
this code with this circuit we can see that when button 2 is pressed, LED3 toggle
at 200ms blinking rate; on the other hand, when button 2 is pressed again it will
stop toggling.
Implementation code
Wiring Implementation
Figure 4
In figure 4 we connect green LED2 to Pin A1,red LED1 to Pin A0 and button1 to
Pin A3. By using this code with this circuit we can see that when button 1 is
pressed less than 1000ms, LED2 will toggle. Otherwise, when button 1 is pressed
more than 1000ms, LED1 will toggle.
CONCLUSION
After the experiment, we can get a better understanding of using task scheduling and
SysTick timer in applied tasks. The SysTick generates interrupt requests on a regular basis.
This allows an operation system to carry out context switching to support multiple tasking.
Moreover, it allows us to remove bouncing effect of buttons without using time delay and it
makes it more convenient to manage LEDs while doing the experiment.