임베디드시스템개론: Arduino 활용
Lecture #3: Button Input & FND Control
2012. 3. 25 by 김 영 주
강의 목차
디지털 입력 Button switch 입력 Button Debounce
7-Segment FND : 직접 제어 7-Segment FND : IC 제어
2
디지털 입력 : Switch 입력 (1) 실습 목표
아두이노 디지털 입력 처리 실습 digitalRead()
스위치 입력 처리 이해
3
디지털 입력 : Switch 입력 (2) 사전 지식
디지털 입력(Digital Input) 아두이노는 디지털 입출력핀을 “입력 모드”로 설정할 경우
“HIGH”(5V) 또는 “LOW”(0 V)의 전기 신호가 입력 가능
digitalRead() 함수
특정 입력핀의 현재 입력 신호값을 읽어옴(HIGH/LOW)
스위치 (Switch) 연결을 만들거나 끊는데 사용하는 전자 부품
다양한 형태의 스위치를 사용 가능
간이 형태의 스위치를 만들어 사용 가능
4
HIGH
LOW
디지털 입력 : Switch 입력 (3) 사전 지식 : 스위치 종류
5
Knife Switch
Toggle Switch
Penny Switch
Magnetic Hexadecimal Tilt Lever
Button Switch
디지털 입력 : Switch 입력 (4) 사전 지식
스위치 연결 디지털 입력핀은 아무런 회로연결을 갖지
않는 경우
디지털 입력은 0V와 5V 사이를 움직이며(floating) 일정한 값을 갖지 못함
Pull-up Register / Pull-down Register 입력 핀을 5V 또는 0V 연결할 때 적용 Pull input up to 5V Pull input down to ground(0V)
스위치 연결방식
스위치 동작 Press HIGH Not Press LOW
6
[참고] 저항 컬러코드 식별법 (1)
7
[참고] 저항 컬러코드 식별법 (2)
8
디지털 입력 : Switch 입력 (5) 회로도 및 회로 구성
9
회로 연결회로도
디지털 입력 : Switch 입력 (6) 아누이노 프로그램 : Button Input
10
int btnPin = 22;Int ledPin = 9;int btnState;
void setup(){
pinMode(ledPin, OUTPUT);pinMode(btnPin, INPUT);
}
void loop(){
btnState = digitalRead(btnPin);if (btnState == HIGH) {
digitalWrite(ledPin, HIGH);} else {
digitalWrite(ledPin, HIGH);}
}
디지털 입력 : Switch 입력 (7) Button Bounce 현상 버튼을 누르거나 땔 때에 일시적으로 전압이 불안해지는 현상
Button Debounce Button bounce를 제거하는 동작
H/W 해결책 – capacity 활용
S/W 해결책 – 버튼 입력 상태가 바뀔 때에 약간의 지연을 허용
11
디지털 입력 : Switch 입력 (8) 아누이노 프로그램 : Button Debounce
12
int btnPin = 22;Int ledPin = 9;int btnState;Int lastBtnState = LOW;
long lastDebounceTime = 0; long debounceDelay = 50;
void setup(){
pinMode(ledPin, OUTPUT);pinMode(btnPin, INPUT);
}
void loop(){
int reading = digitalRead(btnPin);
if (reading == lastBtnState) {lastDebounceTime = millis();
}
if (millis()-lastDebounceTime> debounceDelay) {btnState = reading;
}
digitalWrite(ledPin, btnState);
lastBtnState = reading;}
디지털 입력 : Switch 입력 (9) Debounce 라이브러리 Arduino 플랫폼에서는 button debounce 라이브러리를 제공
13
#include <Debounce.h>
int btnPin = 22;Int ledPin = 9;
Debounce debouncer = new Debounce(50, btnPin);
void setup(){
pinMode(ledPin, OUTPUT);pinMode(btnPin, INPUT);
}
void loop(){
debouncer.update();
digitalWrite(ledPin, debouncer.read());}
7-Segment FND 출력 (1) 실습 목표 FND 장치를 이용하여 16진수(10진수) 출력 MCU를 이용하여 FND 장치 제어 방법 학습
14
7-Segment FND 출력 (2) 사전 지식 7-Segment FND
8개의 LED로 1자리 숫자를 출력
종류 Common-Anode FND Common-Cathode FND
10진수 출력
15
7-Segment FND 출력 (3) 사전 지식 7-Segment FND
16진수 출력
16
7-Segment FND 출력 (4) 회로도
17
7 6 4 2 1 9 10 5
7-Segment FND 출력 (5) 회로 연결
18
7-Segment FND 출력 (6) 아두이노 프로그램 : FND 출력
19
int fndStartPin = 24;int fndEndPin = 31;
void fndWrite(int num){
static unsigned char fnd_segcode[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C,0x39, 0x5E, 0x79, 0x71 };
unsigned char outcode;int value;
if (num < 0) num = 0;else if (num >= 16) num = 15;outcode = fnd_segcode[num];
for (int i=fndStartPin; i<=fndEndPin; i++) {value = (outcode & 0x01) ? HIGH : LOW;digitalWrite(i, value);outcode >>= 1;
}}
7-Segment FND 출력 (7) 아두이노 프로그램 : FND 출력
20
void setup(){
for (int i=fndStartPin; i<=fndEndPin; i++)pinMode(i, OUTPUT);
}
void loop(){
for (int num=0; num<16; num++) {fndWrite(num);delay(1000);
}}
7-Segment FND 출력 (8) 아두이노 프로그램 : Button 입력 & FND 출력
21
#include <Debounce.h>
int fndStartPin = 24;int fndEndPin = 31;
int btnPin = 2;int lastBtnState = LOW;Int btnInputCnt = 0;
Debounce debouncer = new Debounce(20, btnPin);
boolean checkRisingEdge() {boolean edge;int state;
debouncer.update();state = debouncer.read();edge = (state && (state != lastBtnState)) ? true : false;lastBtnState = state;
return edge;}
7-Segment FND 출력 (9) 아두이노 프로그램 : Button 입력 & FND 출력
22
void fndWrite(int num){
static unsigned char fnd_segcode[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C,0x39, 0x5E, 0x79, 0x71 };
unsigned char outcode;int value;
if (num < 0) num = 0;else if (num >= 16) num = 15;outcode = fnd_segcode[num];
for (int i=fndStartPin; i<=fndEndPin; i++) {value = (outcode & 0x01) ? HIGH : LOW;digitalWrite(i, value);outcode >>= 1;
}}
7-Segment FND 출력 (10) 아두이노 프로그램 : Button 입력 & FND 출력
23
void setup(){
for (int i=fndStartPin; i<=fndEndPin; i++)pinMode(i, OUTPUT);
fndWrite(btnInputCnt);}
void loop(){
if (checkRisingEdge()) {if (++btnInputCnt > 15)
btnInputCnt = 0;fndWrite(btnInputCnt);
}}
버튼입력신호의rising edge에 동기화하여 카운터값
을 증가시킴
전용 IC를 이용한 FND 출력 (1) 실습 목표 전용 제어 IC를 이용하여 FND 장치 제어 방법 학습
I/O 전용 IC를 사용함으로써 MCU의 제어 동작과 관련하여 입출력 핀 및제어 프로그램을 줄일 수 있음
24
MCU Blackbox
BCD code
Control signal
전용 IC를 이용한 FND 출력 (2) 사전 지식 FND I/O IC : HEF4511BP
IC 데이터쉬트 참조 BCD to 7-segment latch/decoder/driver
25
전용 IC를 이용한 FND 출력 (3) 사전 지식 HEF4511BP
26
전용 IC를 이용한 FND 출력 (4) 사전 지식 HEF4511BP
27
제어신호 타이밍:
전용 IC를 이용한 FND 출력 (5) 회로도
28
전용 IC를 이용한 FND 출력 (6) 회로 연결
29
전용 IC를 이용한 FND 출력 (7) 아두이노 프로그램 : HEF4511BP 제어
30
#include <Debounce.h>
int fndnELPin = 22;int fndBCDStartPin = 23;int fndBCDEndPin = 26;
int btnPin = 2;int lastBtnState = LOW;Int btnInputCnt = 0;
Debounce debouncer = new Debounce(20, btnPin);
boolean checkRisingEdge() {boolean edge;int state;
debouncer.update();state = debouncer.read();edge = (state && (state != lastBtnState)) ? true : false;lastBtnState = state;
return edge;}
전용 IC를 이용한 FND 출력 (8) 아두이노 프로그램 : HEF4511BP 제어
31
void fndWrite(int bcd){
if (bcd < 0) bcd = 0;else if (bcd >= 10) bcd = 9;bcd &= 0x0f;
digitalWrite(fndnELPin, LOW);
for (int i=fndBCDStartPin; i<=fndBCDEndPin; i++) {digitalWrite(i, (bcd & 0x08) ? HIGH : LOW);bcd <<= 1;
}
digitalWrite(fndnELPin, LOW);asm(“nop”); asm(“nop”); asm(“nop”);digitalWrite(fndnELPin, HIGH);
}
전용 IC를 이용한 FND 출력 (9) 아두이노 프로그램 : HEF4511BP 제어
32
void setup(){
pinMode(fndnELPin, OUTPUT);for (int i=fndBCDStartPin; i<=fndBCDEndPin; i++)
pinMode(i, OUTPUT);
fndWrite(btnInputCnt);}
void loop(){
if (checkRisingEdge()) {if (++btnInputCnt > 15)
btnInputCnt = 0;fndWrite(btnInputCnt);
}}
과제물 과제 내용 HEF4511BP IC를 이용한 FND 프로그램에서 2자리 십진수를 출
력할 수 있도록 회로를 설계하고 프로그램을 작성하여라. 되도록 제어 신호 수를 줄일 수 있도록 설계를 제안하여라.
제출물 회로도 프로그램 소스 실행 예(사진)
제출일 차주 수업 시간
33