Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
디지털 입력 : 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
디지털 입력 : 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 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) {
lastBtnState = reading;
}
digitalWrite(ledPin, lastBtnState);
}
디지털 입력 : Switch 입력 (9)
Debounce 라이브러리 (1)
Arduino 플랫폼에서 button debounce 라이브러리를 제공하였으나 현재는 지원하지 않음
Bounce2 Library – open-source library
https://github.com/thomasfredericks/Bounce2
설치:
Bounce2-master.zip 파일 다운로드 및 압축 해제
압축 해제된 폴더의 이름을 “bounce2”로 변경
“bounce2” 폴더를 $(HOME)/Arduino/libraries 폴더로 이동
Arduino IDE를 재실행
13
디지털 입력 : Switch 입력 (10)
Debounce 라이브러리 (2)
14
#include <bounce2.h> // 아두이노 스케치/라이브러리 추가 메뉴에 추가
int btnPin = 22;
int ledPin = 9;
Bounce debouncer = new Bounce();
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT);
debouncer.attach(btnPin);
debouncer.interval(5);
}
void loop()
{
debouncer.update();
digitalWrite(ledPin, debouncer.read());
}
7-Segment FND 출력 (2)
사전 지식
7-Segment FND
8개의 LED로 1자리 숫자를 출력
종류
Common-Anode FND
Common-Cathode FND
10진수 출력
16
7-Segment FND 출력 (6)
아두이노 프로그램 : FND 출력
20
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 출력
21
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 출력
22
#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 출력
23
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 출력
24
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의 제어 동작과 관련하여 입출력 핀 및제어 프로그램을 줄일 수 있음
25
MCU Blackbox
BCD
code
Control
signal
전용 IC를 이용한 FND 출력 (2)
사전 지식
FND I/O IC : HEF4511BP
IC 데이터쉬트 참조
BCD to 7-segment latch/decoder/driver
26
전용 IC를 이용한 FND 출력 (7)
아두이노 프로그램 : HEF4511BP 제어
31
#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 제어
32
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 제어
33
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);
}
}