Outils pour utilisateurs

Outils du site


issue131:gcb

Ceci est une ancienne révision du document !


In the last issue I showed you how to read the voltage with a potentiometer and send the values over the serial line. In this issue we will read the ‘state’ of a switch using different techniques and take some actions after the state or change of state is recognized. For debugging purposes I will eventually send some messages to the serial line. For the hardware in this article you will need a momentary switch - everyone should have a momentary switch. See https://learn.sparkfun.com/tutorials/switch-basics/momentary-switches for an overview of switches. We will use a similar one like this https://www.sparkfun.com/products/97 as it is breadboard friendly and gives a good haptic feedback (clicking sound).

Using a switch as Input

To read a switch with an microcontroller there are three possible methods. • Internal pull-up resistor • External pull-up resistor • External pull-down resistor • No resistor. ‘There be Dragons’ using this method so read this part very carefully.

Internal pull-up resistor

Some AVR and PIC microcontroller have internal pull-up resistors which can be activated. Have a look in the datasheet of your microcontroller if it has one and how to activate the resistor. Mostly they are called ‘weak’ resistor as their resistance vary with temperature and from part to part. For the microcontroller of choice for this set of articles, the ATtiny13a, there is the Port B data register and data direction register (p. 57 of the datasheet) those have to be initialised as input. If you set a PIN as an input, the pull up resistance is off by default in Great Cow BASIC, so it first has to be activated. With the internal pull up the microcontroller reads a high value (button not pressed) or low value (button pressed). See the code snippet to activate the internal resistance for the whole PORT or one PIN only in Great Cow BASIC (see code above).

External pull-up resistor

If you experiencing problems with the internal pull-up, as said previously this can be caused by temperature or manufacturing technique, then you can achieve the same outcome by adding an external resistor. A good value for this is 10 kOhm and you can use any quality you have to hand. The external resistor operates the same as the internal resistor but will handle the operating environment: The resistor pulls the signal high (when the button not pressed) and that the input is read low (when the button is pressed). In this case you can define your inputs as usual without activating the internal pull-ups. Defining the inputs if using a pull-up would be as following in Great Cow BASIC:

#CHIP tiny13a, 1.2 DIR PORTB IN

External pull-down resistor

The pull-down resistor operates in the opposite manner to a pull-up resistor, instead of pulling the signal high (Vcc) you pull the signal low (GND). The resistor pulls the signal low (when the button not pressed) and that the input is read high (when the button is pressed). Again, the recommended resistance is a 10 kOhm resistor. The inputs are defined the same way as with the external pull-up resistor. Defining the inputs if using a pull-down resistor would be as following in Great Cow BASIC:

#CHIP tiny13a, 1.2 DIR PORTB IN

The question is now, are there any differences between the pull-up and the pull-down resistance in software other than the name and the opposite direction with respect to Vcc and GND? Yes. If you read a switch which uses a pull-up resistor, the signal is inverted. A high value means the button is not pressed (FALSE) and a low value means the button is pressed (TRUE). If you consider to connect your switches through a pull-down resistor the high value is button pressed (TRUE) and in case of the low value the button is not pressed (FALSE). In pseudocode it would be as shown below.

No Resistor

If you reckon reading the switch without any resistor by its own, this could be called the fourth method, but this is not a good technique. It means connecting Vcc and GND through a switch on your input PIN, which is also known as shortcut. A pull-up resistor puts an input to a known state.

Debouncing

Switch debouncing is one of those things you generally have to live with when playing with switches and digital circuits. If you want to input a manual switch signal into a digital circuit you'll need to debounce the signal so a single press doesn't appear like multiple presses.

Another problem regarding switches is, that the state of the switch is floating: The signal is not as accurate as it should be and the microcontroller reads false positives or false negatives. Maybe the switch produces a signal while you push it down, or it produces a signal while the button goes up - how would you identify this? Is the button pressed or not? For such problems you have some options to solve this, the technique is called debouncing.

As with many topics, there exist several methods to debounce switches, in hardware, e.g. with a RC-Filter or you could use a specialised debouncing IC. Supplementary there exist several approaches to achieve the debouncing as a software method, as well. The goal of both ideas is that the transitions of the button would be clearly recognized. I will go the software route which seems sufficient to me. If the shown example does not fit your needs, please do a research, to find the right one for your specific application field.

Simple debouncing method

The simple version of a debouncing routine for a switch connected through a pull-up resistor would be something like this, for debugging purposes I provided some output via serial connection (code shown next page, left side).

Just in case you find the non-inverted signal easier and do not want to use the internal pull-up resistors, you would debounce an external pull-down resistor like that shown next page, right side.

Both methods read the state of the switch at the very beginning of the program start, if it is pressed the variable button is initialised as zero. Then, the switch is re-read in a loop and the variable button is incremented by 1 as long the button is pressed. The short time out of 10 ms to let the signal settle and to make sure to read the correct state. If the button is pressed long enough the LED is switched on or off.

Breadboard circuitry

At first you see the breadboard assembling for the simple debouncing method. Compile the program and flash it to the ATtiny13a with your hardware programmer of choice. Connect the LED in the right direction to PortB.0 (PB0). Connect one end of the switch to PortB.4 (PB4) and the other end to GND. For the first example I used the internal pullup as a showcase. The connection to the serial adapter gives the chance to debug and it can power the entire circuit.

For the pull-down version compile the code and flash the ATTiny13a like before. Connect one end of the switch with 5 V and add a wire to PortB.4 (PB4) inbetween. The other end of the switch connects to ground (GND) through a 10 kOhm Resistor.

Conclusion

The ability of a microcontroller of any brand to read a switch in some way is important to control the various functions of the microcontroller. If you will be not able to give the microcontroller some sort of input, the processing and output of data is impossible. And you would not always want a small project, e. g. a night light, having to connect to your computer just to switch the LED on or off, would you? Because of the speed of the microcontroller the button presses in these examples are recognized very often and only at program start. In the next article I will examine a method to overcome this and will introduce you to interrupts of a microcontroller in general and in Great Cow BASIC in special.

Sources

If you want to download the sources instead of copy-pasting it, you can now check it out with git or an SVN client. Have a look at https://github.com/Anobium/Great-Cow-BASIC-Demonstration-Sources/tree/master/Publication%20Solutions/Full%20Circle for more information.

issue131/gcb.1522510852.txt.gz · Dernière modification : 2018/03/31 17:40 de auntiee