Outils pour utilisateurs

Outils du site


issue131:gcb

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
issue131:gcb [2018/03/31 17:40] – créée auntieeissue131: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.  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). +**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+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'état. Pour des besoins de débogage, j'enverrai finalement des messages sur la ligne série. Comme matériel dans cet article, vous aurez besoin d'un bouton-poussoir - tout le monde devrait avoir un poussoir. Voyez https://learn.sparkfun.com/tutorials/switch-basics/momentary-switches pour un aperçu des poussoirs. Nous en utiliserons un semblable à celui-ci : https://www.sparkfun.com/products/97 car il est pratique pour une plaque d'essai et donne un retour tactile (son du clic). 
 + 
 +**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.  ‘There be Dragons’ using this method so read this part very carefully.+• No resistor.  ‘There be Dragons’ using this method so read this part very carefully.**
  
-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 partFor the microcontroller of choice for this set of articles, the ATtiny13a, there is the Port B data register and data direction register (p57 of the datasheet) those have to be initialised as input.  If you set PIN as an inputthe 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).+Pour lire un interrupteur avec un microcontrôleur, trois méthodes sont possibles :  
 +• Une résistance de tirage interne. 
 +• Une résistance de tirage externe. 
 +• Une résistance d'écoulement externe. 
 +• Pas de résistance« Il y un loup » dans cette méthode ; aussilisez cette partie-ci avec beaucoup d'attention.
  
-External pull-up resistor+**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).** 
 + 
 +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'activer. Généralement, ces résistances sont qualifiées de « weak » (faible qualité) car leur résistance varie avec la température et suivant le composant. Sur le microcontrôleur choisi pour cet ensemble d'articles, le ATtiny13a, le registre de données et le registre de direction du Port B (voir p. 57 du manuel) doivent être initialisés en entrée. Si vous paramétrez une borne en entrée, la résistance de tirage est hors circuit par défaut dans Great Cow BASIC ; aussi, elle doit être activée en premier lieu. Avec ce tirage interne, le microcontrôleur lit une valeur élevée (le bouton étant relâché) ou faible (le bouton étant enfoncé). Voici l'extrait de code pour activer la résistance interne pour l'ensemble des ports ou pour une seule borne dans Great Cow BASIC (voir le code ci-dessus).  
 + 
 +**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: 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**
 +
 +Résistance de tirage externe
 +
 +Si des problèmes se font jour avec le tirage interne - comme indiqué précédemment, la température ou la technique de fabrication peuvent en être le cause - , vous pouvez obtenir le même résultat en ajoutant une résistance externe. Une bonne valeur pour celle-ci est 10 kΩ et vous pouvez utiliser n'importe quelle qualité que vous avez sous la main. La résistance externe opère de la même manière que la résistance interne et gérera l'environnement de travail : la résistance tire le signal vers le haut (quand le poussoir n'est pas enfoncé) et une valeur basse de l'entrée est lue (quand le bouton est enfoncé). Dans ce cas, vous pouvez définir vos entrées comme d'habitude sans activer les résistances de tirage internes. Si vous utilisez un tirage, la définition des entrées en Great Cow BASIC sera comme ceci : 
  
 #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 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: 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**
 +
 +Résistance d'écoulement externe
 +
 +La résistance d'écoulement agit à l'inverse de la résistance de tirage. Au lieu de tirer le signal vers le haut (+), vous tirez le signal vers le bas (Masse). La résistance tire le signal vers le bas (quand le bouton est relâché) et la valeur d'entrée est élevée (quand le bouton est enfoncé). Là encore, une résistance de 10 kΩ est recommandée. Les entrées sont définies de la même manière qu'avec la résistance de tirage externe. La définition des ports dans Great Cow BASIC, en cas d'utilisation d'une résistance d'écoulement, sera comme suit :
  
 #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.  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.+**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+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'écoulement, autres que leurs nom et leur fonctionnement inverse par rapport au + et à la masse. Oui. Si vous lisez un interrupteur qui utilise une résistance de tirage, le signal est inversé. Une valeur haute signifie que le bouton n'est pas enfoncé (FAUX) et une valeur faible qu'il est enfoncé (VRAI). Si vous choisissez de connecter vos interrupteurs par l'intermédiaire d'une résistance d'écoulement, la valeur haute indique que le bouton est enfoncé (VRAI) et, dans le cas d'une valeur basse, le bouton est relâché (FAUX). En pseudo-code, ce sera comme présenté ci-dessous. 
  
-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'interrupteur seul sans aucune résistance, ce qu'on pourrait appeler la quatrième méthode, ce n'est pas une bonne technique. La connexion du + et de la masse à travers un interrupteur sur votre borne d'entrée est connue comme étant un court-circuit. Une résistance de tirage place l'entrée dans un état connu. 
 + 
 +**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. 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. +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'anti-rebond d'un interrupteur est une de ces choses avec lesquelles vous devez généralement vivre quand vous jouez avec des interrupteurs et des circuits numériques. Si vous voulez entrer un signal d'interrupteur manuel dans un circuit numérique, vous devrez supprimer les rebonds de sorte qu'un seul appui ne semble pas être des appuis multiples. 
 + 
 +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'être et le microcontrôleur lit des faux positifs ou des faux négatifs. Peut-être que l'interrupteur produit un signal pendant que vous appuyez dessus ou il produit un signal quand vous le relâchez. Comment pouvez-vous l'identifier ? Le bouton est-il enfoncé ou pas ? Pour de tels problèmes, il existe plusieurs solutions ; cette technique est appelée anti-rebond. 
 + 
 +**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'anti-rebond des interrupteurs avec du matériel, par ex., un filtre RC, ou vous pourriez utiliser un circuit spécialisé contre les rebonds. En plus, plusieurs approches logicielles permettent aussi de traiter les rebonds. Le but des deux idées est que les transitions du bouton soient clairement reconnues. Je prendrai la solution logicielle qui m'apparaît suffisante. Si l'exemple présenté ne répond pas à vos besoins, faites une recherche pour trouver la bonne pour votre champ d'application particulier. 
 + 
 +**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'écoulement avec ce qui est montré dans la partie droite de la page suivante. 
 + 
 +Les deux méthodes lisent l'état de l'interrupteur dès le tout début du programme ; s'il est enfoncé, la variable button est initialisée à zéro. Ensuite, l'interrupteur est relu dans une boucle et la variable button est incrémentée de 1 tant que le bouton est enfoncé. Le court délai de 10 ms est là pour laisser le temps au signal de s'établir et être sûr de lire le bon état. Si le bouton est enfoncé assez longtemps, la LED s'allume ou s'éteint. 
 + 
 +**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'essai
  
-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 inputthe processing and output of data is impossibleAnd you would not always want a small project, eg. a night light, having to connect to your computer just to switch the LED on or offwould you? Because of the speed of the microcontroller the button presses in these examples are recognized very often and only at program startIn 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'abordvous voyez le montage sur la plaque d'essai pour la méthode de l'anti-rebond simpleCompilez le programme et flashez-le sur l'ATtiny13a avec votre programmateur matériel préféréConnectez la LED sur la droite au port B.0 (PB0). Connectez une borne de l'interrupteur au port B.4 (PB4) et l'autre bout à la masse. Pour le premier exemplej'ai utilisé une résistance de tirage pour la démonstrationLa connexion à l'adaptateur série nous permet le débogage et il peut alimenter tout le circuit.
  
 +Pour la version par écoulement, compilez le code et flashez le ATtiny13a comme ci-avant. Connectez une borne de l'interrupteur au 5 V et insérez un fil vers le port B.4 (PB4). L'autre terminaison de l'interrupteur se connecte à la masse à travers une résistance de 10 KΩ.
 +
 +**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'importe quelle marque à lire un interrupteur d'une manière ou d'une autre est importante pour piloter de multiples fonctions du microcontrôleur. Si vous n'êtes pas capable de donner au microcontrôleur quelque chose en entrée, le traitement et la sortie de données sont impossibles. Et vous ne voudriez pas en rester définitivement sur un petit projet, par ex., une veilleuse, en ayant à vous connecter à votre microcontrôleur juste pour allumer et éteindre une LED, n'est-ce pas ? Du fait de la vitesse du microcontrôleur, les appuis sur le bouton de ces exemples sont reconnus très souvent et seulement au début du programme. Dans le prochain article, j'examinerai une méthode pour résoudre cela et je vous présenterai les interruptions d'un microcontrôleur en général et dans Great Cow BASIC en particulier.
  
 +**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.**
  
 Sources Sources
  
-If you want to download the sources instead of copy-pasting ityou 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.+Si vous voulez télécharger les sources plutôt que les copier/collervous pouvez maintenant les récupérer avec git ou un client SVNRegardez à https://github.com/Anobium/Great-Cow-BASIC-Demonstration-Sources/tree/master/Publication%20Solutions/Full%20Circle pour plus d'informations.
issue131/gcb.1522510852.txt.gz · Dernière modification : 2018/03/31 17:40 de auntiee