[STM32 / MBED] [MED] Switch debouncing

아래 그림의 과 같이 스위치 입력 시 물리적인 문제로 바운싱 (채터링) 문제로 인한 오작동이 발생하는데 이를 해결하기 위한방법으로 하드웨어 적인 방법가 소프웨어 적인 방법이 구글링 하면 여러개 검색 된다.

/media/uploads/4180_1/switch_bounce.jpg


Timer를 할용하여 일정 시간 (200ms)동안 시간 주연을 주는 방식

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "mbed.h"
InterruptIn button(p14);
DigitalOut led(LED1);       
DigitalOut flash(LED4);
Timer debounce;
 
void isr1() {
  if (debounce.read_ms() > 200) {
    led = !led;
    debounce.reset();
  }
}
 
int main() {
  debounce.start();
  button.rise(&isr1);                              
  while(true) {
    flash = !flash;
    wait(0.2);
  }
}


인터럽트의 Enable / Disable 방식 

Enabling and Disabling Interrupts

In some cases, you may want to ensure a section of code is not interrupted. For example, you might be talking to a peripheral where the whole transaction must happen in one go, and an interrupt could cause the operation to fail.

The simplest way to avoid this is to disable interrupts for the critical section of your code:

1
2
3
4
5
__disable_irq();    // Disable Interrupts
 
// do something that can't be interrupted
 
__enable_irq();     // Enable Interrupts

https://os.mbed.com/handbook/Working-with-Interrupts 









0
0
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

임베디드 보드

번호 제목 글쓴이 날짜 조회수
12 아두이노 [강좌] 11. analogRead() 함수 실습 - 습도 센서 icon 양재동메이커 03-14 17,555
11 아두이노 [강좌] 10. 아날로그 기능 - analogRead() 함수 icon 양재동메이커 03-14 21,331
10 아두이노 [강좌] 9. digitalWrite()/digitalRead() 실습 – 초음파 센서 icon 양재동메이커 03-14 17,841
9 아두이노 [강좌] 8. digitalRead() 함수(2) – 풀업 저항과 INPUT_PULLUP 모드 icon 양재동메이커 03-14 20,405
8 아두이노 [강좌] 7. digitalRead() 함수(1) - 스위치를 이용한 DigitalReadSerial 예제 icon 양재동메이커 03-14 18,318
7 아두이노 [강좌] 6. digitalWrite() 함수 실습 - LED 깜빡이기 icon 양재동메이커 03-14 16,915
6 아두이노 [강좌] 5. Blink 예제 해부하기(4) - digitalWrite()와 delay() icon 양재동메이커 03-14 16,328
5 아두이노 [강좌] 4. Blink 예제 해부하기(3) - pinMode() icon 양재동메이커 03-14 16,889
4 아두이노 [강좌] 3. Blink 예제 해부하기(2) - 함수 icon 양재동메이커 03-14 16,466
3 아두이노 [강좌] 2. Blink 예제 해부하기(1) icon 양재동메이커 03-14 16,842
2 아두이노 [강좌] 1. 아두이노 보드와 스케치 툴 icon 양재동메이커 03-13 19,154
1 아두이노 [강좌] 0. 아두이노, 시작. icon 양재동메이커 03-11 16,971