issue131:gcb
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue131:gcb [2018/03/31 17:40] – créée auntiee | 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. |
- | Using a switch as Input | + | 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 des actions après la reconnaissance de cet état ou d'un changement d' |
+ | |||
+ | **Using a switch as Input | ||
To read a switch with an microcontroller there are three possible methods. | To read a switch with an microcontroller there are three possible methods. | ||
Ligne 7: | Ligne 9: | ||
• External pull-up resistor | • External pull-up resistor | ||
• External pull-down resistor | • External pull-down resistor | ||
- | • No resistor. | + | • No resistor. |
- | Internal pull-up resistor | + | Utilisation d'un interrupteur comme entrée |
- | 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. | + | 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' | ||
- | External pull-up resistor | + | **Internal pull-up resistor |
+ | |||
+ | 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 | ||
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. | 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. | ||
+ | |||
+ | #CHIP tiny13a, 1.2 | ||
+ | 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 | #CHIP tiny13a, 1.2 | ||
DIR PORTB IN | DIR PORTB IN | ||
- | External pull-down resistor | + | **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 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. | ||
+ | |||
+ | #CHIP tiny13a, 1.2 | ||
+ | DIR PORTB IN** | ||
+ | |||
+ | Résistance d' | ||
+ | |||
+ | La résistance d' | ||
#CHIP tiny13a, 1.2 | #CHIP tiny13a, 1.2 | ||
DIR PORTB IN | 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. |
- | No Resistor | + | 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' |
- | 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. | + | **No Resistor |
- | Debouncing | + | 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 | ||
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' | 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' | ||
- | 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. |
- | 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. | + | Anti-rebond |
- | Simple debouncing method | + | 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. ** | ||
+ | |||
+ | Comme pour de nombreux sujets, il existe plusieurs méthodes pour l' | ||
+ | |||
+ | **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). | 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). | ||
Ligne 47: | Ligne 89: | ||
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. ** |
- | Breadboard circuitry | + | 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 | ||
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. | 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. | + | 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 | ||
+ | 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 | Sources | ||
- | If you want to download the sources | + | Si vous voulez télécharger les sources |
issue131/gcb.1522510852.txt.gz · Dernière modification : 2018/03/31 17:40 de auntiee