issue131:gcb
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue131:gcb [2018/04/08 07:19] – d52fr | issue131:gcb [2018/04/12 17:16] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
**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. | **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. | ||
- | Dans le dernier numéro, je vous ai montré comment lire la tension avec un potentiomètre et envoyer les valeurs sur une ligne série. Dans ce numéro, nous lirons l'« état » d'un interrupteur en utilisant plusieurs techniques et feront | + | Dans le dernier numéro, je vous ai montré comment lire la tension avec un potentiomètre et envoyer les valeurs sur une ligne série. Dans ce numéro, nous lirons l'« état » d'un interrupteur en utilisant plusieurs techniques et ferons |
**Using a switch as Input | **Using a switch as Input | ||
Ligne 14: | Ligne 14: | ||
Pour lire un interrupteur avec un microcontrôleur, | Pour lire un interrupteur avec un microcontrôleur, | ||
- | • | + | • Une résistance de tirage interne. |
+ | • Une résistance de tirage externe. | ||
+ | • Une résistance d' | ||
+ | • Pas de résistance. « Il y a un loup » dans cette méthode ; aussi, lisez cette partie-ci avec beaucoup d' | ||
**Internal pull-up resistor | **Internal pull-up resistor | ||
Some AVR and PIC microcontroller have internal pull-up resistors which can be activated. | Some AVR and PIC microcontroller have internal pull-up resistors which can be activated. | ||
+ | |||
+ | Résistance de tirage interne | ||
+ | |||
+ | Certains microcontrôleurs AVR et PIC ont des résistances internes de liaison au + qui peuvent être activées. Regardez le manuel de votre microcontrôleur s'il en a un et comment l' | ||
**External pull-up resistor | **External pull-up resistor | ||
Ligne 27: | Ligne 33: | ||
#CHIP tiny13a, 1.2 | #CHIP tiny13a, 1.2 | ||
DIR PORTB IN** | DIR PORTB IN** | ||
+ | |||
+ | Résistance de tirage externe | ||
+ | |||
+ | Si des problèmes se font jour avec le tirage interne - comme indiqué précédemment, | ||
+ | |||
+ | #CHIP tiny13a, 1.2 | ||
+ | DIR PORTB IN | ||
**External pull-down resistor | **External pull-down resistor | ||
Ligne 34: | Ligne 47: | ||
#CHIP tiny13a, 1.2 | #CHIP tiny13a, 1.2 | ||
DIR PORTB IN** | DIR PORTB IN** | ||
+ | |||
+ | Résistance d' | ||
+ | |||
+ | La résistance d' | ||
+ | |||
+ | #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. | **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. | ||
+ | |||
+ | La question est maintenant de savoir s'il y a des différences dans le logiciel entre la résistance de tirage et celle d' | ||
**No Resistor | **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.** | 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.** | ||
+ | |||
+ | Pas de résistance | ||
+ | |||
+ | Si vous comptez lire l' | ||
**Debouncing | **Debouncing | ||
Ligne 46: | Ligne 72: | ||
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. ** | 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. ** | ||
+ | |||
+ | Anti-rebond | ||
+ | |||
+ | L' | ||
+ | |||
+ | Un autre problème concernant un interrupteur est que son état est flottant : le signal n'est pas aussi précis qu'il devrait l' | ||
**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. ** | **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. ** | ||
+ | |||
+ | Comme pour de nombreux sujets, il existe plusieurs méthodes pour l' | ||
**Simple debouncing method | **Simple debouncing method | ||
Ligne 53: | Ligne 87: | ||
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). | 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.** | + | 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. ** | + | 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. ** |
+ | |||
+ | Méthode simple pour un anti-rebond | ||
+ | |||
+ | La version simple d'un programme anti-rebond pour un interrupteur connecté à une résistance de tirage serait quelque chose comme ceci ; pour les besoins du débogage, j'ai mis quelques sorties vers la connexion série (voir le code sur la page suivante, partie gauche). | ||
+ | |||
+ | Juste au cas où vous trouveriez plus facile de travailler avec un signal non-inversé et que vous ne vouliez pas utiliser de résistance de tirage, vous pourriez éviter les rebonds d'une résistance d' | ||
+ | |||
+ | Les deux méthodes lisent l' | ||
**Breadboard circuitry | **Breadboard circuitry | ||
Ligne 63: | Ligne 105: | ||
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. ** | 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 | + | Circuit sur la plaque d' |
- | 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.** | + | D' |
+ | Pour la version par écoulement, | ||
+ | **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.** | ||
+ | Conclusion | ||
+ | |||
+ | La capacité d'un microcontrôleur de n' | ||
**Sources | **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:// | 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:// | ||
+ | |||
+ | Sources | ||
+ | |||
+ | Si vous voulez télécharger les sources plutôt que les copier/ |
issue131/gcb.1523164778.txt.gz · Dernière modification : 2018/04/08 07:19 de d52fr