Day 4. Interrupt (p1)

You might also like

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

MICROCONTROLLER ENGINEERING

Prof. SI HYUN LEE

November 11, 2021 -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 11, 2021 -2-

1
DAY-4 : Fundamental Interrupt Programming(1)

Learning Object

How to Design a Control Program That Turns On/Off of 1 LED with a switch using the
polling method

Class Application Program Example

Using polling method


Class-1 INT_Ex4_1
Using GPIO Port Setting

Using polling method


Class-2 INT_EX2
Without using GPIO Port Setting

Using polling method INT_EX3


Class-3 Without using GPIO Port Setting Debugging SFR
Using Direct Register Access Function call/return

November 11, 2021 -3-

Atlanta, Georgia
CNN Headquarters
Coca-Cola Company Headquarters
Gone with the wind, Filming location
Stone mountain : 8km, 3200 acre, 318m

https://www.gatech.edu/
https://www.youtube.com/watch?v=RKY2z4O8g2w
https://www.youtube.com/watch?v=tv5XT2gVb_I
November 11, 2021 -4-

2
Class-1 : How to design a control program that turns
on/off 1 LED with a switch using the
polling method

INT_Ex4_1 : refer p.88

November 11, 2021 -5-

Refer p.88

1. 인터럽트 (Interrupt)
• 인터럽트는 프로그램을 실행하는 도중에 예기치 않은 상황이 발생할 경우 현재 실행중인 작업을 즉시 중단하고
발생된 상황을 우선 처리한 후 실행 중이던 작업으로 복귀하여 계속 처리하는 것

2. 인터럽트 발생 종류
• 외부 인터럽트
• 내부 인터럽트
• 소프트웨어 인터럽트

November 11, 2021 -6-

3
Refer p.88

main() 함수 인터럽트 서비스 루틴

• 인터럽트가 요구되면 현재 수행하던


명 령 을 종 료 하 고 interrupt vector • 인터럽트 레지스터 확인 (EXTI_IMR,
table로 무조건 점프 EXTI_EMR, EXTI_RTSR, EXTI_FTSR)

명령어N • PC (Program Counter) 저장 Push Operation


Jump • 레지스터 값을 저장
인터럽트 발생 명령어N+1 Vector Table
명령어N+2
명령어N+3
• 인터럽트 서비스 수행
Return

• PC (Program Counter) 값 반환 Pop Operation


• 레지스터 값을 복구
main() 함수

인터럽트 서비스 루틴을 종료하고 호출 함수로 복귀

November 11, 2021 -7-

Refer p.88
3. 인터럽트 (interrupt) 처리 방식 (1 of 2)

(1) 폴링 (polling) 방식의 인터럽트 : DAY4


• GPIO와 Switch를 사용하여 LED를 On/Off

(2) NVIC (Nested Vectored Interrupt Controller) 방식의 인터럽트 : DAY5


•HAL_NVIC_SetPriority() 함수 : 인터럽트 우선 순위 설정
•HAL_NVIC_EnableIRQ 함수 : NVIC 인터럽트 컨트롤러에서 장치별 인터럽트를 활성화
•HAL_GPIO_EXTI_IRQHandler 함수 : GPIO에서 발생한 EXTI 인터럽트 요청을 처리
•HAL_GPIO_EXTI_Callback 함수 : GPIO에서 발생한 EXTI 인터럽트를 처리하는 Callback 함수이다.

November 11, 2021 -8-

4
Refer p.88
3. 인터럽트 (interrupt) 처리 방식 (2 of 2) : DAY5

RCC->AHB1ENR = (1<<3)|(1<<6); // Provides clocks to RCC, GPIOD, and GPIOG ports


RCC->APB2ENR |= (1<<14); // APB2: EXTI, SYSCFG supply clock to peripherals

// GPIOD PD0 output


GPIOD->MODER |= 00005555;

// GPIOG PG0~PG1 input


GPIOG->OSPEEDR &= 0x00000000;
GPIOG->PUPDR &= 0x00000000;

// SYSCFG
SYSCFG->EXTICR[0] = (SYSCFG_EXTICR1_EXTI0_PG | SYSCFG_EXTICR1_EXTI1_PG);

EXTI->IMR |= 0x0000000F; 외부 인터럽트를 설정하는 레지스터


// EXTI->EMR &= 0x00000003;
EXTI->RTSR |= 0x0000000F; EXTI_IMR (External interrupt mask register )
// EXTI->FTSR &= 0x0000000C; -인터럽트를 개별적으로 허용/금지

NVIC_SetPriority (EXTI0_IRQn,0); EXTI_EMR (EXTernal Event Mask Register


NVIC_EnableIRQ (EXTI0_IRQn); -EXTI를 인터럽트가 아닌 이벤트로 사용할 경우 설정

NVIC_SetPriority (EXTI1_IRQn,0); EXTI_RTSR (EXTernal Rising trigger)


NVIC_EnableIRQ (EXTI1_IRQn); -인터럽트 또는 이벤트를 Rising으로 동작할 경우 설정

EXTI_FTSR (EXTernal Falling Trigger)


-레지스터로 인터럽트 또는 이벤트를 Falling으로 동작할 경우 설정

November 11, 2021 -9-

Refer p.88

4. 인터럽트 요청 시 처리 과정

• 인터럽트 발생  인터럽트 요구

• 프로그램 수행 중단 -현재 수행중인 명령의 수행이 완료된 시점에서 중단 ...

• Push Stack (레지스터 복사(대피), 현재 수행 중인 번지 저장)

• 인터럽트 벡터 테이블로 점프  인터럽트 전처리 실행 : Refer, App./p.372

• 인터럽트 처리 루틴 수행

• Pop Stack (레지스터 복사, 이전 수행 다음 번지로 복귀)

November 11, 2021 -10-

5
1. Create a Project Refer p.88

1. File / New / Project / stm32 project

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

-Next
-Project Name : INT_EX4_1
-Finish

November 11, 2021 -11-

2. Clock Configuration Refer p.88

It does not matter whether the clock setting is the default setting value

November 11, 2021 -12-

6
3. Pinout & Configuration (1 of 3) Refer p.88

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


not turn on.

November 11, 2021 -13-

3. Pinout & Configuration (2 of 3) Refer p.88

반드시 이렇게 설정해야 함


Disable: LED가 ON되지 않음

set up for debugging

November 11, 2021 -14-

7
3. Pinout & Configuration (3 of 3) Refer p.88

-PD0  LED1 Output


-PG0/1  SW1, SW2 (Polling 방식)

-Using STM32 project


-using CubeMX for PD0 Port
-using polling mode
-press SW1  LED1 ON,
-press SW2  LED1 OFF

November 11, 2021 -15-

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

main.h (1 of 1)

-No additional code in this file

November 11, 2021 -16-

8
main.c (1 of 4) Refer p.89

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

while (1)
{
if(HAL_GPIO_ReadPin (GPIOG, GPIO_PIN_0) == HAL_OK)
HAL_GPIO_WritePin (GPIOD, GPIO_PIN_0, GPIO_PIN_SET);

if(HAL_GPIO_ReadPin (GPIOG, GPIO_PIN_1) == HAL_OK)


HAL_GPIO_WritePin (GPIOD,GPIO_PIN_0, GPIO_PIN_RESET);

/* USER CODE END WHILE */

This example uses CubeMX to set GPIOD0 and GPIOG0/1 to generate code, so no separate code is
needed.

November 11, 2021 -17-

Class-2 : How to design a control program that turns


on/off 1 LED with a switch using the polling
method

INT_Ex4_3 : refer p.91

-Using STM32 project


-Without using CubeMX for PD0 Port setting
-using polling mode
-press SW1  LED1 ON,
-press SW2  LED1 OFF
-Same operation as Example INT_EX1, INT_EX3

November 11, 2021 -18-

9
Refer p.91

1. Project
-Same as Example INT_EX1

2. Clock Configuration
-Same as Example INT_EX1

3. Pinout & Configuration


-Using STM32 project
-Without using CubeMX for PD0 and PG0/PG1

4. Coding : main.h & main.c


-Same as Example INT_EX4_1

November 11, 2021 -19-

5. Coding : main.h & main.c Refer p.91

main.h (1 of 1)

-No additional code in this file

November 11, 2021 -20-

10
main.c (1 of 2) Refer p.91
int main(void)
{

/* USER CODE BEGIN 2 */

GPIO_InitTypeDef LEDConfig, SWConfig;

__HAL_RCC_GPIOD_CLK_ENABLE();
LEDConfig.Mode = GPIO_MODE_OUTPUT_PP;
LEDConfig.Pin = GPIO_PIN_0;
LEDConfig.Speed = GPIO_SPEED_FREQ_LOW;
LEDConfig.Pull = GPIO_NOPULL;

__HAL_RCC_GPIOG_CLK_ENABLE(); If this code is not present, the switch


SWConfig.Mode = GPIO_MODE_INPUT; will not work.
SWConfig.Pin = GPIO_PIN_0 | GPIO_PIN_1;
SWConfig.Speed = GPIO_SPEED_FREQ_LOW;
SWConfig.Pull = GPIO_NOPULL;

HAL_GPIO_Init (GPIOD, &LEDConfig);


HAL_GPIO_Init (GPIOG, &SWConfig);

/* USER CODE END 2 */

November 11, 2021 -21-

main.c (1 of 2) Refer p.91

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

while (1)
{
if (HAL_GPIO_ReadPin (GPIOG, GPIO_PIN_0) == HAL_OK)
HAL_GPIO_WritePin (GPIOD, GPIO_PIN_0, GPIO_PIN_SET);

if (HAL_GPIO_ReadPin (GPIOG, GPIO_PIN_1) == HAL_OK)


HAL_GPIO_WritePin (GPIOD,GPIO_PIN_0, GPIO_PIN_RESET);

/* USER CODE END WHILE */

November 11, 2021 -22-

11
Class-3 : How to design a control program that turns
on/off 1 LED with a switch using the polling
method

INT_Ex4_6 : refer p.95

-Using STM32 project


-Without using CubeMX for PD0 Port setting
-Also, using register direct access method
-using polling mode
-press SW1  LED1 ON,
-press SW2  LED1 OFF
-Same operation as Example INT_EX4_1

November 11, 2021 -23-

Refer p.95

1. Project
-Same as Example INT_EX1

2. Clock Configuration
-Same as Example INT_EX1

3. Pinout & Configuration


-Using STM32 project
-Without using CubeMX for PD0 and PG0/PG1

4. Coding : main.h & main.c


-Same as Example INT_EX1

November 11, 2021 -24-

12
5. Coding : main.h & main.c Refer p.95

main.h (1 of 1)

-No additional code in this file

November 11, 2021 -25-

RCC_AHB1ENR Refer p.96


• GPIOx 의 클 럭 을 활 성 화 하 는 레 지 스 터 , 즉 , 포 트 GPIOD 와
GPIOG에 클럭을 활성화(공급)
int main(void) • Refer App./p.180
{

/* USER CODE BEGIN 2 */

RCC->AHB1ENR = (1<<0x03)|(1<<0x06);
RCC->APB2ENR |= (1<<14); RCC->AHB1ENR 1 1 1
8 4 2 1
// GPIOD PD0 output
GPIOD->MODER |= 0x00005555; Refer p.106
GPIOG->OSPEEDR &= 0x00000000;
GPIOG->PUPDR &= 0x00000000;

// GPIOG PG0 input


GPIOG->MODER |= 0x00000000;
GPIOD->OSPEEDR &= 0x00000000;
GPIOG->PUPDR &= 0x00000000;
RCC->APB2ENR
1
/* USER CODE END 2 */ SYSCFG EN
0 : 인터럽트(EXTI)를 사용하기 위한 시스템 설정 컨트롤러 클럭을 비활성화
1 : 인터럽트(EXTI)를 사용하기 위한 시스템 설정 컨트롤러 클럭을 활성화
Debugging SFR
Refer App./p.187
Refer App./p.64 : memory map

November 11, 2021 -26-

13
Refer p.73-76

RCC->AHB1ENR 1 1
8 4 2 1

RCC->MODER : 00:input, 01:output, 10:alternate, 11: analog

GPIOD->PUPDR : low 4 port setting


00: pull-down(floating), 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 11, 2021 -27-

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

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

while (1) GPIOG->IDR


{
if ((GPIOG->IDR & 0x01) == 0) GPIG->IDR : SW1 no press1 : 0xf7af
GPIOD->ODR |= 0x01; GPID->ODR : SW1 pressed0 : 0xf6ae

if ((GPIOG->IDR & 0x02) == 0) GPIG->IDR : SW2no press1 : 0xf6ad


GPIOD->ODR &= ~0x01; GPID->ODR : SW2pressed0 : 0x52ae

/* USER CODE END WHILE */

November 11, 2021 -28-

14
Summary : Day-4

Class-1: Introduction, INTERRUPT_EX4_1 p.88

Day-04 Interrupt(1) Class-2: INTERRUPT_EX4_5 p.94

Class-3: INTERRUPT_EX4_6, Summary p.96

November 11, 2021 -29-

15

You might also like