You are on page 1of 17

MICROCONTROLLER ENGINEERING

Prof. SI HYUN LEE

November 10, 2021 -1-

Silicon Valley
Stanford University
University of California, Berkeley
Napa Valley Yosemite National Park

Next day : Georgia / Georgia Institute of Technology

November 10, 2021 -2-

1
Full Class Schedule
Day Subject Contents Reference (Text Book) Remark
Class-1: ARM STM32F429ZIT Architecture p.1-25
NUCLEO-F429ZI B/D &
Day-01 Class-2: NUCLEO-F429ZI Reference Board p.1-25
Development Environment
Class-3: STM32CubeIDE Installation & Code Analysis p.1-25
Class-1: Introduction, GPIO_EX3_1 p.57
Day-02 GPIO(1) Class-2: GPIO_EX3_4 p.69
Class-3: GPIO_EX3_8, Summary p.73
Class-1: Introduction, GPIO_EX3_3 p.64
Day-03 GPIO(2) Class-2: GPIO_EX3_7 p.71
Class-3: GPIO_EX3_9, Summary p.76
Class-1: Introduction, INTERRUPT_EX4_1 p.88 Polling method
Day-04 Interrupt(1) Class-2: INTERRUPT_EX4_5 p.94
Class-3: INTERRUPT_EX4_6, Summary p.96
NVIC :
Class-1: Introduction, INTERRUPT_EX4_8 p.101
Interrupt method
Day-05 Interrupt(2)
Class-2: INTERRUPT_EX4_11 p.111
Class-3: INTERRUPT_EX4_12, Summary p.112
Fundamental
Class-1: Introduction, ADC_EX5_1 p.120
Reference
Day-06 ADC(1)
Class-2: ADC_EX5_2 p.123
Class-3: ADC_EX5_3, Summary p.132
Class-1: Introduction, ADC_EX5_4 p.134 Advanced
Day-07 ADC(2) Class-2: ADC_EX5_5 p.136 Structure : 3-ways
Class-3: ADC_EX5_6, Summary p.138
Class-1: Introduction, TIMER_EX1
Day-08 Timer(1) Class-2: TIMER_EX2
Class-3: TIMER_EX3, Summary
Class-1: Introduction, TIMER_EX4
Day-09 Timer(2) Class-2: TIMER_EX4
Class-3: TIMER_EX5, Summary
Class-1: Introduction, Buzzer p.178, p.181, p.183 Final Exam. : 90Min
Day-10 Application Programming Class-2: Light_Sensor, Water Sensor p.195, p.201
Class-3: UART, Summary p.241
November 10, 2021 -3-

DAY-3 : Fundamental GPIO Programming(2)

Learning Object

How to program the operation to turn on and off all LEDs in 3 ways, CubeMX, HAL
library function, and register direct access

Class Application Program Example

How to design using CubeMX for GPIO_EX3_3, p.64


Class-1
GPIO port setting Debugging mode testing

How to design without using


Class-2 GPIO_EX3_7, p.71
CubeMX for GPIO port setting

GPIO_EX3_8, p.72
How to design with register
GPIO_EX3_9, p.76
Class-3 direct access and without using
CubeMX for GPIO port setting

November 10, 2021 -4-

2
Class-1 : Implement a program to set GPIO port and
turn on/off all LED sequentially using
CubeMX

November 10, 2021 -5-

GPIO_Ex3_3 : refer p.64

-Using STM32 project


-Using CubeMX
-Same operation as Example GPIO_EX5, GPIO_EX6, GPIO_EX7

GPIO_Ex4 : HAL Library, STM32 Project, CbueMX for GPIO Port setting : O
GPIO_Ex5 : HAL Library, STM32 Project, CbueMX for GPIO Port setting : x
GPIO_Ex6/7 : Register directive access, STM32 Project, CbueMX for GPIO Port setting : x

November 10, 2021 -6-

3
1. Create a Project

1. File / New / Project / STM32 project

2. MCU/MPU Selector
-Core : Arm Cortex-M4
-Series : STM32F4
-Line : STM32F429/439
-Package : LQFP144

3. Next
4. Project Name : GPIO_Ex3_3
5. Finish

November 10, 2021 -7-

2. Clock Configuration

If the clock setting is set as the default setting, the LED ON/OFF time becomes longer.

November 10, 2021 -8-

4
3. Pinout & Configuration (1 of 3)

Must be set like this. If it is set to Disable, the LED does


not turn on.

When BYPASS Clock Source is set, the GPIO port is


set as follows.

November 10, 2021 -9-

3. Pinout & Configuration (2 of 3)

반드시 이렇게 설정해야 함


Disable: LED가 ON되지 않음

set up for debugging

November 10, 2021 -10-

5
3. Pinout & Configuration (3 of 3) Refer p.64

November 10, 2021 -11-

4. Coding : main.h & main.c Refer p.64

main.h (1 of 1)

-No additional code in this file

November 10, 2021 -12-

6
main.c (1 of 2) Refer p.64

int main (void)

{
/* USER CODE BEGIN 2 */

uint16_t LEDALL= (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |


GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);

uint16_t LED[8]=
{
GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_4,
GPIO_PIN_5,GPIO_PIN_6,GPIO_PIN_7
};

/* USER CODE END 2 */

November 10, 2021 -13-

main.c (2 of 2) Refer p.64


F3 : jump to function definition

/* Infinite loop */
/* USER CODE BEGIN WHILE */

while (1) 1. Run Mode


{
for (int i=0; i<8; i++){ 2. Debugging mode
HAL_GPIO_WritePin(GPIOD, LED[i], GPIO_PIN_SET); Step Over
HAL_Delay(500);
}

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_RESET);


HAL_Delay(500);

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_SET);


HAL_Delay(500);

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_RESET);


HAL_Delay(500);

/* USER CODE END WHILE */


}
Cont+Space : Code Completion Function

November 10, 2021 -14-

7
Class-2 : Implement a program to set GPIO port and
turn on/off all LEDs sequentially without
using CubeMX

November 10, 2021 -15-

GPIO_Ex3_7 : refer p.71

-Using STM32 project


-Without using CubeMX
-Same operation as Example GPIO_EX1, GPIO_EX3

GPIO_Ex4 : HAL Library, STM32 Project, CbueMX : O


GPIO_Ex5 : HAL Library, STM32 Project, CbueMX for GPIO Port setting : x
GPIO_Ex6/7/8 : Register directive access, STM32 Project, CbueMX for GPIO Port setting : x

November 10, 2021 -16-

8
Refer p.71

1. Project
-Same as Example GPIO_EX4, GPIO_EX5, GPIO_EX6, GPIO_EX7

2. Clock Configuration
-Same as Example GPIO_EX1

3. Pinout & Configuration


-Using STM32 project
-Add codes for port setting without using CubeMX
-Using HAL Library function
-Same operation as GPIO_EX4, GPIO_EX5, GPIO_EX6, GPIO_EX7

4. Coding : main.h & main.c

November 10, 2021 -17-

4. Coding : main.h & main.c Refer p.71

main.c
main.h (1 of 1)
#include "main.h"
-No additional code in this file
void SystemClock_Config(void);
static void MX_GPIO_Init(void);

int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

HAL_Init();
SystemClock_Config();
MX_GPIO_Init();

/* USER CODE BEGIN 2 */


/* USER CODE END 2 */

while (1)
{
// function definition
}
}

November 10, 2021 -18-

9
main.c (1 of 2) First, deleting the setted port Refer p.71

int main (void)

{
/* USER CODE BEGIN 2 */

uint16_t LEDALL = (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |


GPIO_PIN_5| GPIO_PIN_6| GPIO_PIN_7);

uint16_t LED[8] = {GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_4,


GPIO_PIN_5, GPIO_PIN_6, GPIO_PIN_7};

__HAL_RCC_GPIOD_CLK_ENABLE();

GPIO_InitTypeDef myLEDConfig;

myLEDConfig.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4|


GPIO_PIN_5 | GPIO_PIN_6| GPIO_PIN_7;
myLEDConfig.Mode = GPIO_MODE_OUTPUT_PP;
myLEDConfig.Pull = GPIO_NOPULL;
myLEDConfig.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init (GPIOD, &myLEDConfig);

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_RESET);

/* USER CODE END 2 */

November 10, 2021 -19-

main.c (2 of 2) Refer p.71

/* Infinite loop */
/* USER CODE BEGIN WHILE */

while (1)
{
for (int i=0; i<8; i++) {
HAL_GPIO_WritePin (GPIOD, LED[i], GPIO_PIN_SET);
HAL_Delay (1000);
}

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_RESET);


HAL_Delay (1000);

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_SET);


HAL_Delay(1000);

HAL_GPIO_WritePin (GPIOD, LEDALL, GPIO_PIN_RESET);


HAL_Delay (1000);

/* USER CODE END WHILE */

November 10, 2021 -20-

10
/**
* @brief GPIO Init structure definition
*/

typedef struct
{
uint32_t Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */

uint32_t Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIO_mode_define */

uint32_t Pull; /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.
This parameter can be a value of @ref GPIO_pull_define */

uint32_t Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIO_speed_define */

uint32_t Alternate; /*!< Peripheral to be connected to the selected pins.


This parameter can be a value of @ref
GPIO_Alternate_function_selection */
}GPIO_InitTypeDef;

November 10, 2021 -21-

Class-3 : Implement a program to set GPIO port and


turn on/off 8 LEDs sequentially using
register direct access

November 10, 2021 -22-

11
GPIO_EX3_8 : refer p.72

• Mathod-1 : Register directive access, STM32 Project, without using CbueMX for GPIO Port
setting  port code : x (memory map head file)

• Mathod-2 : Register directive access, STM32 Project, without using CbueMX for GPIO Port
setting  port code : o (memory map no head file)

November 10, 2021 -23-

Refer p.76

Method-1 : GPIO_EX3_8

November 10, 2021 -24-

12
Refer p.76

1. Project
-stm32 project

2. Clock Configuration
-Same as Example GPIO_Ex3_3/7

3. Pinout & Configuration


-Using STM32 project
-Add codes for port setting without using CubeMX
-In CubeMX, GPIOD is not set, and the code is added with direct access to registers.

4. Coding : main.h & main.c


-.h, .c

November 10, 2021 -25-

4. Coding : main.h & main.c Refer p.77, 74, 76

main.h (1 of 1)

/* Private defines -----------------------------------------------------------*/


/* USER CODE BEGIN Private defines */

#define RCC_BASEAddr 0x40023800


#define GPIOD_BASEAddr 0x40020C00

#define AHB1ENR_Offset 0x30


#define GPIOX_MODER_Offset 0x00
#define GPIOX_OSPEEDR_Offset 0x08
#define GPIOX_PUPDR_Offset 0x0C
#define GPIOX_ODR_Offset 0x14
pointer
#define RCC_AHB1ENR (*(volatile unsigned*)(RCC_BASEAddr+AHB1ENR_Offset))
#define GPIOD_MODER (*(volatile unsigned*)(GPIOD_BASEAddr+ GPIOX_MODER_Offset))
#define GPIOD_OSPEEDR (*(volatile unsigned*)(GPIOD_BASEAddr+ GPIOX_OSPEEDR_Offset))
#define GPIOD_PUPDR (*(volatile unsigned*)(GPIOD_BASEAddr+ GPIOX_PUPDR_Offset))
#define GPIOD_ODR (*(volatile unsigned*)(GPIOD_BASEAddr+GPIOX_ODR_Offset))

/* USER CODE END Private defines */

Since this code is defined in file a, there is no need to add it. (for explanatory purpose only)

November 10, 2021 -26-

13
main.c (1 of 2)LED’s Operation On/Off Refer p.73-76

int main(void)
{

/* USER CODE BEGIN 2 */


Set lower 8 pins as outputs RCC->AHB1ENR 1 1
low-speed in lower 8 pins 8 4 2 1
RCC->AHB1ENR = (1<<3);
GPIOD->MODER |= 0x00005555;
GPIOD->OSPEEDR &= 0x00005555;
GPIOD->PUPDR &= 0x00000000;

/* USER CODE END 2 */ RCC->MODER : 00:input, 01:output, 10:alternate, 11: analog

GPIOD->PUPDR : low 4 port setting


00: no pull-up RCC->OPSPEEDR:00: low speed, 01:mediun speed, 10:fasr speed,
01: pull-up 11: high speed
10: pull-down
11: reserved

November 10, 2021 -27-

main.c (2 of 2) Refer p.76

/* Infinite loop */
/* USER CODE BEGIN WHILE */

while (1)
{
for (int i=0; i<8; i++){
GPIOD->ODR |= (1<<i); // ON sequentially from the first LED
for (int i=0; i<1000000; i++);
}

for (int i=0; i<2000000; i++)


GPIOD->ODR &= 0x00000000; // 16 all LEDs OFF

for (int i=0; i<2000000; i++)


GPIOD->ODR |= 0xFFFFFFFF; // 16 all LEDs ON

for (int i=0; i<2000000; i++)


GPIOD->ODR &= 0x00000000; // 16 all LEDs OFF

/* USER CODE END WHILE */

November 10, 2021 -28-

14
Refer p.76

8421 8421 8421 8421


1111 1111 1111 1111  FFFF
| 0101 0101 0101 0101  5555
1111 1111 1111 1111  FFFF

1111 1111 1111 1111  FFFF


& 0101 0101 0101 0101  5555
0101 0101 0101 0101  5555

November 10, 2021 -29-

Addition Explanation for Example GPIO_Ex3_9 Refer p.76

#define RCC_BASEAddr 0x40023800


#define AHB1ENR_Offset 0x30

#define RCC_AHB1ENR (*(volatile unsigned*) (RCC_BASEAddr+AHB1ENR_Offset))

#define PERIPH_BASE 0x40000000UL


#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000UL)
#define RCC_BASE (AHB1PERIPH_BASE + 0x3800UL)

Define in Stmf429xx.h file

November 10, 2021 -30-

15
main.c (2 of 2) Refer p.76

/* Infinite loop */
/* USER CODE BEGIN WHILE */

while (1)
{

for (int i=0; i<8; i++)


{
GPIOD_ODR |= (1<<i);

for(int i=0; i<1000000; i++);


}

for (int i=0; i<2000000; i++)


GPIOD_ODR &= ~0xFFFFFFFF;

for (int i=0; i<2000000; i++)


GPIOD_ODR |= 0xFFFFFFFF;

for (int i=0; i<2000000; i++)


GPIOD_ODR &= 0x00000000;

/* USER CODE END WHILE */

November 10, 2021 -31-

Refer p.76

November 10, 2021 -32-

16
Summary : Day-3

Class-1: Introduction, GPIO_EX3_3 p.64

Day-03 GPIO(2) Class-2: GPIO_EX3_7 p.71

Class-3: GPIO_EX3_8/9, Summary p.73, p.76

November 10, 2021 -33-

17

You might also like