[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
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

임베디드 보드

번호 제목 글쓴이 날짜 조회수
32 아두이노 [강좌] 31. I2C 통신 (3) - 온도 센서(TMP102) 실습하기 icon 양재동메이커 03-20 28,164
31 아두이노 [강좌] 30. I2C 통신 (2) - Wire 함수 알아보기 icon 양재동메이커 03-20 32,472
30 아두이노 [강좌] 29. I2C 통신 (1) - I2C 통신이란 무엇인가 icon 양재동메이커 03-20 37,194
29 아두이노 [강좌] 28. 시간 관련 함수 (3) - MsTimer2 라이브러리 icon 양재동메이커 03-20 27,728
28 아두이노 [강좌] 27. 시간 관련 함수 (2) - BlinkWithoutDelay 예제 icon 양재동메이커 03-20 21,659
27 아두이노 [강좌] 26. 시간 관련 함수 (1) - delay() / millis() icon 양재동메이커 03-20 27,904
26 아두이노 [강좌] 25. tone()/noTone() 함수 (2) - 부저 실습 icon 양재동메이커 03-20 22,062
25 아두이노 [강좌] 24. tone()/noTone() 함수 (1) - 함수 설명 icon 양재동메이커 03-20 23,783
24 아두이노 [강좌] 23. Interrupt(인터럽트) (3) - 인터럽트 함수 알아보기 icon 양재동메이커 03-20 23,824
23 아두이노 [아두이노 강좌] 22. Interrupt(인터럽트) (2) - 스위치로 LED 켜고 끄기 예제 icon 양재동메이커 03-20 29,775
22 아두이노 [강좌] 21. Interrupt(인터럽트) (1) - 인터럽트란 무엇인가 icon 양재동메이커 03-16 21,988
21 아두이노 [아두이노 강좌] 20. String (3) - 유용한 함수들 icon 양재동메이커 03-16 29,505
20 아두이노 [강좌] 19. String (2) - String() 생성자와 + 연산 icon 양재동메이커 03-16 20,062
19 아두이노 [강좌] 18. String (1) - 문자형(char) 배열과 String icon 양재동메이커 03-16 29,346
18 아두이노 [강좌] 17. Serial 통신(3) - 유용한 기능들 icon 양재동메이커 03-15 23,680
17 아두이노 [강좌] 16. Serial 통신(2) - 시리얼 통신 함수 알아보기 icon 양재동메이커 03-15 30,301
16 아두이노 [강좌] 15. Serial 통신(1) - 시리얼 통신이란 무엇인가 icon 양재동메이커 03-15 26,627
15 아두이노 [강좌] 14. analogRead()/analogWrite() 실습 - 조도 센서 + LED icon 양재동메이커 03-15 19,536
14 아두이노 [강좌] 13. analogWrite() 실습 - LED 밝기 조절하기(Fade 예제) icon 양재동메이커 03-15 19,920
13 아두이노 [강좌] 12. 아날로그 기능(2) - analogWrite() 함수 icon 양재동메이커 03-14 22,469