Outils pour utilisateurs

Outils du site


issue134:great_cow_basic

In FCM#131 I showed you how to recognize the state of a switch and debug over the serial line. After having some troubles with the code in this article, I had to have a break and get things together. So with some delay here we are now learning about interrupts, what they are and what for they are helpful. My apologies if this caused some inconveniences.

Dans le n° 131 du FCM, je vous ai montré comment reconnaître l'état d'un interrupteur et le déboguer à travers une ligne série. Après avoir eu quelques problèmes avec le code de cet article, j'ai dû faire une coupure pour tout remettre d'aplomb. Aussi, avec un peu de retard, nous allons maintenant apprendre ici les interruptions, ce qu'elles sont et à quoi elles peuvent être utiles. Mes excuses si ceci vous a créé des difficultés.

Interrupt service routines An interrupt does what the term describes, it interrupts a running program - or better: causes a jump in the program - and executes another piece of program code. The so called ‘interrupt service routine’. The formerly interrupted program will be executed at the very same stage the interrupt has occurred. The ATTiny13a has ten interrupt vectors (or program spaces) you can place code at, which is handled, if you set the bits in the corresponding interrupt registers. For our purposes we have a closer look at the INT0 and PCINT0 interrupts. Both interrupts can wake the microcontroller from its power saving states which will be evident later on.

Routines de service d'une interruption

Une interruption fait ce que son nom décrit ; elle interrompt un programme qui tourne - ou mieux : elle entraîne un saut dans le programme et exécute un autre morceau du code du programme. C'est la dénommée « routine de service de l'interruption ». L'exécution du programme précédemment interrompu reprendra à l'endroit précis où l’interruption a eu lieu. Le ATTiny13a a dix vecteurs d'interruption (ou espaces programme) où vous pouvez mettre du code, qui sera traité, si vous activez les bits dans les registres d'interruption correspondants. Pour nos besoins, nous regarderons plus particulièrement les interruptions INT0 et PCINT0. Ces deux interruptions peuvent sortir le microcontrôleur de son état de veille, ce qui deviendra plus clair par la suite.

For the INT0 interrupt you only have one PIN available, the PIN B1 (PORTB.1). This restriction comes with some advantages, with INT0 you could set the trigger to four different states: • if the level on PIN is low, • any logical change, • the falling edge or • the rising edge of the signal (the transition from the high to the low level).

Pour l'interruption INT0, vous n'avez qu'une broche disponible, la PIN B1 (PORTB.1). Cette restriction donne aussi quelques avantages ; avec INT0, vous pouvez activer le déclencheur pour quatre états différents : • si le niveau sur la borne est bas (LOW), • sur tout changement logique, • la chute de ou • la montée du signal (la transition de l'état haut à l'état bas).

The PCINT0 interrupt can be used with all remaining PIN except Vcc and GND. The disadvantage of this interrupt is that it triggers on every transition and you have to determine which PCINT has fired. If only using one PIN this is a small caveat, but if using more than one, you would have to determine which one has ‘pulled the trigger’. Another possible solution regarding switches and interrupts would be a polling routine. At certain intervals it would be checked if a switch was pressed or not. For this you would use the service routine of a timer interrupt, but for one button only, it would be kind of over-engineering. So without further ado, here are some sample codes for INT0 interrupt routines. The following page (top left) shows a small pull-up version of an INT0 service routine in Great Cow BASIC. The code to use the Pin Change Interrupt service routine instead is shown on the following page at the bottom left.

L'interruption PCINT0 peut être utilisée avec toutes les broches restantes sauf le + (Vcc) et la masse (GND). Le désavantage de cette interruption est qu'elle se déclenche sur chaque transition et que vous devez déterminer le PCINT qui l'a déclenchée. Si vous n'utilisez qu'une seule connexion, c'est un petit problème ; mais, si vous en utilisez plus d'une, vous devrez déterminer laquelle « a tiré le coup de feu ». Une autre possibilité concernant les interrupteurs et les interruptions serait une routine de scrutation. À certains intervalles, elle regarderait si on a appuyé sur un interrupteur ou non. Pour cela, vous utiliseriez la routine de service de l'interruption du temporisateur ; mais pour un seul bouton seulement, c'est une sorte d'excès technologique. Aussi, sans plus tarder, voici quelques exemples de codes pour les routines d'interruption INT0.

La page suivante (en haut à gauche) montre une petite version de la routine de service d'INT0 en Great Cow Basic, avec résistance de tirage.

Le code pour utiliser à la place la routine de service « Pin Change » (modification sur la connexion) est présenté sur la page suivante en bas à gauche.

Debouncing the button presses In addition you might not change the state of the LED everytime the interrupt occurs, because the switch could be bouncing or the signal quality is poor. Therefore you could add a simple debouncing algorithm. An example for the pull-up version with the INT0 interrupt in Great Cow BASIC is shown, page after next, top left. And the debouncing routine with the pin change interrupt in Great Cow BASIC is:

Anti-rebond sur les appuis du bouton

En plus, vous ne voudriez peut-être pas changer l'état de la LED chaque fois qu'il y a une interruption, car l'interrupteur peut rebondir ou le signal peut être de mauvaise qualité. Par conséquent, vous pourriez ajouter un algorithme anti-rebond simple. Un exemple en Great Cow Basic pour la version avec une résistance de tirage et l'interruption INT0 est présenté, deux pages plus loin, en haut à gauche.

Et la routine d'anti-rebond en Great Cow Basic sur interruption lors d'un changement sur le picot est comme suit :

Breadboard circuitry For the test with the internal pull-ups activated you would need an LED, a switch and the microcontroller (circuit on next page, bottom left). Connect one end of the switch to PIN 6 (PB.1) and the other to PIN 4 (GND). The anode of the LED goes to PIN 5 (PB0) and the cathode of the LED goes to PIN 4 (GND). After flashing the hex-file and connecting the power supply the LED should go on and off after pressing the switch. For testing purposes you would not change the hardware test circuit with above codes, with the pull-down version you would need to change code and also the breadboard circuit.

Circuit sur la plaque d'essai

Pour le test avec les résistances de tirage internes activées, vous aurez besoin d'une LED, d'un interrupteur et du microcontrôleur (voir le circuit sur la page suivante, en bas à gauche). Connectez une terminaison de l'interrupteur au picot 6 (PB.1) et l'autre au picot 4 (masse - GND). L'anode de la LED va au picot 5 (PB0) et la cathode de la LED va au picot 4 (GND). Après avoir flashé le fichier en hexa et connecté l'alimentation, la LED devrait s'allumer et s'éteindre en appuyant sur l'interrupteur.

Pour les besoins du test, vous ne changerez pas la circuiterie de test du matériel avec le code ci-dessus ; dans la version avec résistances de tirage, vous auriez besoin de modifier le code ainsi que le circuit de la plaque d'essai.

Conclusion Interrupts permit the programmer to execute code only at a defined situation and give the advantage to give the main program the most computational power while running - except a interrupt is recognized - and do not have a need for a breaking scanning routine (e. g. check all 10 cycles if button is pressed). Or the other way around interrupts gain the developer the possibility to create a power saving device which has the maximum computational power only when needed and does consume as little power as possible the rest of the time.

Conclusion

Les interruptions permettent au programmeur de n'exécuter du code que dans une situation déterminée et donnent au programme principal l'avantage de la meilleure puissance de calcul pendant qu'il tourne - sauf quand une interruption est détectée - et évite le besoin d'une rupture de séquence pour une vérification (par ex. vérifier tous les 10 cycles si le bouton est enclenché). Ou, autre avantage des interruptions, elle donnent au développeur la possibilité de créer un dispositif économe qui n' atteint sa puissance de calcul maximum que quand c'est nécessaire et qui consomme aussi peu de puissance que possible le reste du temps.

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

Si vous voulez télécharger les sources plutôt que de les copier-coller, vous pouvez les récupérer dès maintenant avec git ou un client SVN. Regardez à : https://github.com/Anobium/Great-Cow-BASIC-Demonstration-Sources/tree/master/Publication%20Solutions/Full%20Circle pour plus d'informations.

References 1. pull-up and down circuitry breadboard-friendly explained, in german https://elektronik.skyline-service.de/elektronik/pullup-und-pulldown-widerstand/

Références

1. Circuiterie des résistances de tirage et d'écoulement bien expliquée pour des utilisateurs, en allemand : https://elektronik.skyline-service.de/elektronik/pullup-und-pulldown-widerstand/

Acknowledgement I wish to thank Evan Venn (Anobium) from the Great Cow BASIC Team for his insights and valuable hints.

Remerciements

Je souhaite remercier Evan Venn (Anobium) de l'équipe de Great Cow BASIC pour ses idées et ses précieuses indications.

issue134/great_cow_basic.txt · Dernière modification : 2018/07/20 16:41 de andre_domenech