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

임베디드 보드

번호 제목 글쓴이 날짜 조회수
8 STM32 / MBED [MED] Switch debouncing icon HellMaker 12-30 11,061
7 STM32 / MBED 19년 Mbed OS 동영상 강좌 List icon HelloMaker 03-11 7,859
6 STM32 / MBED MBED 수학 icon HelloMaker 03-11 7,653
5 STM32 / MBED MBED RTOS icon HelloMaker 03-11 7,316
4 STM32 / MBED MBED 파일 시스템 icon HelloMaker 03-11 7,353
3 STM32 / MBED MBED 통신 icon HelloMaker 03-10 7,849
2 STM32 / MBED MBED Timer icon HelloMaker 03-10 9,224
1 STM32 / MBED MBED 기본 구조 icon HelloMaker 03-10 6,670