Count external pulses by "polling" (also software debounce)Dr Nathan
Scott & Dr Hiroyuki Kagawa · July 2002 You have seen
how floating inputs and switching noise can cause unwanted behaviour, and how
a hardware circuit can be used to condition a noisy input signal. In this
tutorial we will explore another way to get input from external switches. |
「ポーリング」による外部パルスのカウント(ソフトウェアによるノイズ除去)ネーサン・スコット(訳:香川博之) · 2002年7月 浮いている入力とスイッチングノイズがどのように望ましくない動作を引起すか,またノイズをもつ入力信号を整えるために回路をどのように利用するかについて理解した。ここでは, 外部スイッチからの入力を得るための別の方法について体験する。 |
|
|
|
/*Code for a crash course in AVR programmingExample "poll.c"Dr Nathan Scott, July 2002 This program shows how to respond to an external signal by the "polling"approach, which means "frequent checking". It also demonstrates a simple"software debounce" approach. The hardware should be setup as for the project "seven_seg_drv.prj"i.e. at least one BCD-to-7-segment driver on the low bits of PORTC.It is assumed that "PORTC = 3" will cause the number 3 to be displayedon an external 7 segment LED.*/#include "environment.h"#include "delay.h" #define poll_pin 6 // it is good to have names for constants as it helps// make the meaning of the code clear to the reader // global count of the number of pulses on INT0 pinchar gCount; // it is a good practice to define an interface procedure for each// hardware function. It means we can change the hardware, and the interface// procedure, but may not have to alter the rest of the program.void ShowNumber(char num)// the calling code does not need to know the detail of how we will display// the number, we hide all that internally.{ PORTC = num;} void Idle()// ignore this for now, it is used by the UART code in another example{} void main(){ char debounce = 0; // start the global pulse count from zero. // Note that declared variables contain NOISE until you set them! gCount = 0; // set up control over PORTC PORTC = 0; // put a definite start value into the register for PORTC // - it's best not to leave this to chance DDRC = 0x0F; // set the direction of some of the pins of PORTC to "output". // set up control over PORTD PORTD = 0; DDRD = 0; // set all of PORTD to input mode. // display start count (zero most likely) ShowNumber(gCount); while (1) // endless loop, will run forever { if (!debounce && (PIND & BIT(poll_pin))) { // a valid input event has occurred debounce = 0xFF; // start "countdown" which locks out further // input for a short time. gCount++; gCount %= 10; // we can only display 0 to 9 so only keep // the "remainder" when divided by 10. ShowNumber(gCount); while (PIND & BIT(poll_pin)) ; // do nothing, wait for low voltage on poll_pin } if (debounce) { debounce--; // count down to zero only DelayUs(100); } }} |
Figure 1 Example program that polls PORTD, PIN6
It is very
likely that you will find that your LED counts at about one per second unless
the wire is connected to GND or VCC. This is because of induced electrical
noise on the wire. The solution is the same as for the
previous tutorial - tie the input to
either GND or VCC.
|
It is very
likely that you will find that your LED counts at about one per second unless
the wire is connected to 線がGNDまたはVCCに接続されていなければ,LEDが少なくとも約1秒に1度はカウントされるであろう。これは線に電気ノイズを誘導したためである。この解決法は以前の章の入力をGNDまたはVCCにつなぐことと同じである。
|