1.1 GPIO 실습 LED 토글 실습

Download Report

Transcript 1.1 GPIO 실습 LED 토글 실습

박찬식 교수님
조교 : 양준석, 조민규
Contents
1) 프로젝트 팀 구성, 보드 재료 소개, 개발 환경 구축
2) GPIO 실습(LED, 7-세그먼트)
3) Interrupt 실습 (LED, Switch)
4) Timer를 이용한 시계
5) 모터, PWM 신호 발생
6) 모터, PWM 신호 발생
7) SPI
8) USART
9) 센서 I2C
10) 프로젝트 주제 설정
2
1.1 GPIO 실습
■ LED 토글 실습
 본 예제는 GPIO 핀에 연결된 LED를 1초마다 ON/OFF 시키는 프로그램을 실습
해본다.
 출력 포트: PORTB pin9
 RCC APB 주변장치 허용 레지스터2(RCC_APB2ENR)
• Clock 설정: IOPBEN 비트 enable (I/O port B clock enable)
• 주소: 0x4002 1000(RCC 시작 주소) + 0x18(Offset) = 0x4002 1018
: IOPBEN enable(3번째 비트): (*(volatile unsigned *) 0x40021018) |= 0x8;
 포트 B 설정 레지스터(GPIOB_CRH)
• 출력 모드 설정: CNF9[1:0] = 00, MODE9[1:0] = 01
• 주소: 0x4001 0C00(GPIO Port B 시작주소) + 0x04(Offset) = 0x4001 0C04
 포트 B 세트/리셋 레지스터(GPIOB_BSRR)
• 출력 설정: BS9를 ‘1’로 SET -> Port B 9번 핀으로 ‘1’ (high) 출력
• 주소: 0x4001 0C00(GPIO Port B 시작주소) + 0x10(Offset) = 0x4001 0C10
 포트 B 리셋 레지스터(GPIOB_BRR)
• 출력 설정: BR9를 ‘1’로 SET -> Port B 9번 핀으로 ‘0’ (low) 출력
• 주소: 0x4001 0C00(GPIO Port B 시작주소) + 0x14(Offset) = 0x4001 0C14
1.1 GPIO 실습
■ LED 토글 실습
 본 예제는 GPIO 핀에 연결된 LED를 1초마다 ON/OFF 시키는 프로그램을 실습
해본다.
 출력 포트: PORTB pin9
 RCC APB 주변장치 허용 레지스터2(RCC_APB2ENR)
• Clock 설정: IOPBEN 비트 enable (I/O port B clock enable)
• 주소: 0x4002 1000(RCC 시작 주소) + 0x18(Offset) = 0x4002 1018
: IOPBEN enable(3번째 비트): (*(volatile unsigned *) 0x40021018) |= 0x8;
 포트 B 설정 레지스터(GPIOB_CRH)
• 출력 모드 설정: CNF9[1:0] = 00, MODE9[1:0] = 01
• 주소: 0x4001 0C00(GPIO Port B 시작주소) + 0x04(Offset) = 0x4001 0C04
 포트 B 세트/리셋 레지스터(GPIOB_BSRR)
• 출력 설정: BS9를 ‘1’로 SET -> Port B 9번 핀으로 ‘1’ (high) 출력
• 주소: 0x4001 0C00(GPIO Port B 시작주소) + 0x10(Offset) = 0x4001 0C10
 포트 B 리셋 레지스터(GPIOB_BRR)
• 출력 설정: BR9를 ‘1’로 SET -> Port B 9번 핀으로 ‘0’ (low) 출력
• 주소: 0x4001 0C00(GPIO Port B 시작주소) + 0x14(Offset) = 0x4001 0C14
1.1 GPIO 실습
■ LED 토글 실습
1.1 GPIO 실습
■ LED 토글 실습
static void delay_int_count(volatile unsigned int time)
{
for(; time > 0; time--);
}
int main(){
1.1 GPIO 실습
■ LED 토글 실습
int main(){
(*(volatile unsigned *) 0x40021018) |= 0x8;
(*(volatile unsigned *) 0x40010C04) |= 0x10;
(*(volatile unsigned *) 0x40010C14) |= 0x200;
delay_int_count(10000);
(*(volatile unsigned *) 0x40010C10) |= 0x200;
while(1)
{;
}}
1.1 GPIO 실습
■ Memory map
1.1 GPIO 실습
■ Memory map
1.1 GPIO 실습
■ LED 토글 실습
 Volatile ?
 휘발성, 컴파일러에게 절대로 효율적이라고 판단하지 말고 적혀있는 그대로 컴파일 하
라고 알려주는 keyword
(*(volatile unsigned *) 0x40021018) |= 0x8; // RCC_APB2ENR에서 PORTB enable
1.1 GPIO 실습
■ LED 토글 실습
(*(volatile unsigned *) 0x40010C04) |= 0x10; // PORTB의 9번핀의 output mode 설정
1.1 GPIO 실습
■ LED 토글 실습
(*(volatile unsigned *) 0x40010C14) |= 0x200; // PORTB의 9번핀 Reset high LED킴
delay_int_count(10000);
1.1 GPIO 실습
■ LED 토글 실습
(*(volatile unsigned *) 0x40010C10) |= 0x200; // PORTB의 9번핀 Set LED끔
1.1 GPIO 실습
■ LED shift 실습
 본 예제는 그림 8.6과 같은 회로를 이용하여 GPIO 핀에 연결된 3개의 LED를 1
초마다 shift되어 점등되도록 하는 프로그램을 실습해본다.
 출력 포트: PORTB pin9, pin8, pin5
• RCC APB 주변장치 허용 레지스터2(RCC_APB2ENR)
• LED 토글 프로그램 설정과 동일
 포트 B 설정 레지스터(GPIOB_CRL, GPIOB_CRH)
• 출력 모드 설정: CNFy[1:0] = 00, MODEy[1:0] = 01 (y=5, 8, 9)
 포트 B 세트/리셋 레지스터(GPIOB_BSRR)
• 출력 설정: BSy를 ‘1’로 SET -> Port B 9번 핀으로 ‘1’ (high) 출력 (y=5, 8, 9)
 포트 B 리셋 레지스터(GPIOB_BRR)
• 출력 설정: BRy를 ‘1’로 SET -> Port B 9번 핀으로 ‘0’ (low) 출력 (y=5, 8, 9)
1.1 GPIO 실습
■ LED shift 실습
static void delay_int_count(volatile unsigned int nTime)
{
for(; nTime > 0; nTime--);
}
void delay_1_second(void)
{
delay_int_count(806596);
}
1.1 GPIO 실습
■ LED shift 실습
int main(void)
{
(*(volatile unsigned *)0x40021018) |= 0x8;
// PIN #5
(*(volatile unsigned *)0x40010C00) &= 0xFF0FFFFF;
(*(volatile unsigned *)0x40010C00) |= 0x100000;
// PIN #9
(*(volatile
(*(volatile
(*(volatile
& #8
unsigned *)0x40010C04) &= 0xFFFFFF00;
unsigned *)0x40010C04) |= 0x10;
unsigned *)0x40010C04) |= 0x1;
1.1 GPIO 실습
■ LED shift 실습
while(1)
{
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
}
}
*)0x40010C14) |= 0x20;
*)0x40010C10) |= 0x20;
*)0x40010C14) |= 0x200;
*)0x40010C10) |= 0x200;
*)0x40010C14) |= 0x100;
*)0x40010C10) |= 0x100;
1.2 LED_Test (Library)
■ Mango-M32 보드에 구현된 3가지의 LED 점등 Test를 Library를 이용하여 구현
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
delay_1_second();
(*(volatile unsigned
*)0x40010C14) |= 0x200;
*)0x40010C10) |= 0x200;
*)0x40010C14) |= 0x100;
*)0x40010C10) |= 0x100;
GPIOB_BRR |= GPIO_LED1_PIN;
delay_1_second();
GPIOB_BSRR |= GPIO_LED1_PIN;
delay_1_second();
GPIOB_BRR |= GPIO_LED3_PIN;
delay_1_second();
GPIOB_BSRR |= GPIO_LED3_PIN;
1.2 LED_Test (Library)
■ Mango-M32 보드에 구현된 3가지의 LED 점등 Test를 Library를 이용하여 구현
#include "platform_config.h"
/* functions */
/*
* GPIO_LED1_PIN // RED
* GPIO_LED2_PIN // YELLOW
* GPIO_LED3_PIN // BLUE
*/
void LED_On_Red (void)
{
GPIOB_BRR |= GPIO_LED1_PIN;
}
void LED_Off_Red (void)
{
GPIOB_BSRR |= GPIO_LED1_PIN;
}
void LED_On_Yellow (void)
{
GPIOB_BRR |= GPIO_LED2_PIN;
}
void LED_Off_Yellow (void)
{
GPIOB_BSRR |= GPIO_LED2_PIN;
}
void LED_On_Blue (void)
{
GPIOB_BRR |= GPIO_LED3_PIN;
}
void LED_Off_Blue (void)
{
GPIOB_BSRR |= GPIO_LED3_PIN;
}
1.2 LED_Test (Library)
■ Mango-M32 보드에 구현된 3가지의 LED 점등 Test를 Library를 이용하여 구현
void LED_On_All (void)
{
LED_On_Red();
LED_On_Yellow();
LED_On_Blue();
}
void LED_Off_All (void)
{
LED_Off_Red();
LED_Off_Yellow();
LED_Off_Blue();
}
void LED_OnOffAll_Mult(uint32_t count)
{
for(; count > 0; count --)
{
LED_Off_Red();
LED_On_Yellow();
LED_On_Blue();
delay_1_second();
LED_On_Red();
LED_Off_Yellow();
LED_On_Blue();
delay_1_second();
}
}
LED_On_Red();
LED_On_Yellow();
LED_Off_Blue();
delay_1_second();
1.2 LED_Test (Library)
■ Mango-M32 보드에 구현된 3가지의 LED 점등 Test를 Library를 이용하여 구현
void LED_Test (void)
{
#if 0
LED_On_All();
delay_1_second();
LED_Off_All();
delay_1_second();
LED_On_All();
#else
LED_OnOffAll_Mult(30);
#endif
}
1.2 LED_Test (Library)
■ Mango-M32 보드에 구현된 3가지의 LED 점등 Test를 Library를 이용하여 구현
void LED_Test (void)
{
#if 0
LED_On_All();
delay_1_second();
LED_Off_All();
delay_1_second();
LED_On_All();
#else
LED_OnOffAll_Mult(30);
#endif
}
1.2 Key Test
■ Mango-M32 보드에 구현된 3가지의 key중 WKUP, USER Key를 이용한 Led 점
등 실험을 진행할 것이다.
1.2 Key Test
■ Key.c
#include "platform_config.h"
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx,
uint16_t GPIO_Pin)
{
uint8_t bitstatus = 0x00;
}
if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
{
bitstatus = (uint8_t)Bit_SET;
}
else
{
bitstatus = (uint8_t)Bit_RESET;
}
return bitstatus;
1.2 Key Test
■ Key.c
void KEY_Test (void)
{
uint32_t i = 0;
LED_Off_All();
while(1)
{
delay_100_milli_second();
if((i++ & 0x1) == 0x0)
{
LED_On_Blue();
}
else
{
LED_Off_Blue();
}
if(GPIO_ReadInputDataBit(GPIO_KEY,
GPIO_KEY1_PIN) == Bit_SET)
{
LED_On_Red();
}
else
{
LED_Off_Red();
}
if(GPIO_ReadInputDataBit(GPIO_KEY,
GPIO_KEY2_PIN) == Bit_SET)
{
LED_On_Yellow();
}
else
{
LED_Off_Yellow();
}
}
}