Outils pour utilisateurs

Outils du site


issue134:great_cow_basic

Ceci est une ancienne révision du document !


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.

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.

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).

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.

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:

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.

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.

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.

References 1. pull-up and down circuitry breadboard-friendly explained, in german 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.

issue134/great_cow_basic.1531805143.txt.gz · Dernière modification : 2018/07/17 07:25 de d52fr