9.1. ATTiny44 info
9.2. Interrupt function
9.3. Board programming
9.3.1. Blink function
9.3.2. New blink
- Compare the performance and development workflows for other architectures.
- Read a microcontroller data sheet. Program your board to do something, with as many different programming languages and programming environments as possible.
For the individual assignment I am using the ATtiny44 board I made for week07 assignment.
The program I am setting on the ATTiny board will make the LED blink. For that goal, I will need the FabISP programmer I created on my week05 assignment, and the Arduino software. For this assignment I am taking Oscar Gonzalez's tutorial as a reference to explain the contents and steps of the project.
First of all, it is essential to read the board's datasheet to understand the properties and possible functions I can work out using this board. This is some of the basic info about the board:
The high-performance, low-power Microchip AVR RISC-based CMOS 8-bit microcontroller combines 4KB ISP flash memory, 256-Byte EEPROM, 256B SRAM, 12 general purpose I/O lines, 32 general purpose working registers, an 8-bit timer/counter with two PWM channels, a 16-bit timer/counter with two PWM channels, internal and external interrupts, an 8-channel 10-bit A/D converter, a programmable gain stage (1x, 20x) for 12 differential ADC channel pairs, programmable watchdog timer with internal oscillator, a internal calibrated oscillator, and three software selectable power saving modes. By executing powerful instructions in a single clock cycle, the device achieves throughputs approaching 1 MIPS per MHz, balancing power consumption and processing speed.
ATTiny44-Arduino corresponding pinout board:
As the main goal for this assigment is to program the board with a function that includes an LED and a button, it is important to understand the board's specifications about interrupt funcions.
What is an interrupt?
An interrupt is basically an event that requires immediate attention by the microcontroller. When an interrupt occurs the microcontroller pauses its current task and attend to the interrupt by executing an Interrupt Service Routine (ISR) at the end of the ISR the microcontroller returns to the task it had pause and continue its normal operations.
The interrupt action and triggering must be enabled before sending an Interrupt Request to the Handler (ISR - Interrupt Service Routine). There are three registers associated with this: an interrupt enable register (associated with the interrupt itself), an interrupt flag, which is set whenever the interrupt event occurs, even if the interrupt is not enabled; and finally, a global interrupt enable. All three have to be activated in order to release an Interrupt Request.
In the case of AVR microcontrollers, the Global Interrupt Enable bit is called the I-bit, and is in the 7th position Status I/O Register (called SREG for short).
In order for the microcontroller to respond to an interrupt event the interrupt feature of the microcontroller must be enabled along with the specific interrupt. This is done by setting the Global Interrupt Enabled bit and the Interrupt Enable bit of the specific interrupt.
When interrupt triggered:
If all the conditions above are fullfiled, once the interrupt signal is triggered, the microcontroller follows the following sequence:
1- Finishes the execution of the current instruction, saves what it was up to on the stack and clear up the I bit (no interrupt allowed, although this is not entirely true)
2- The code that is associated with the interrupt is then loaded, executed until a RETI instruction is found (Return from Interrupt)
3- The address from the stack stored before is reloaded and the bit I is re-enabled
4- The execution of the program continues
Types of interrupts:
The Atmel AVR provides several different interrupt sources. These interrupts and the separate reset vector each have a separate program vector in the program memory space. All interrupts are assigned individual enable bits which must be written with a logical one together with the global interrupt enable bit in the status register in order to enable the interrupt.
There are basically two types of interrupts. The first type is triggered by an event that sets the interrupt flag. For these interrupts, the program counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, and hardware clears the corresponding interrupt flag. Interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared.
The second type of interrupt will trigger as long as the interrupt condition is present. These interrupts do not necessarily have interrupt flags. If the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered.
In my case I will program my board with the second type of interrupt.
The interrupts are handled in the AVR microcontroller by the so called interrupt vectors. These are ordered by priority in the following table:
These can be grouped into external and internal interrupts. External are the ones that come from a PIN change, and are reflected into the PCINT0 and PCINT1 vectors. Internal are the ones like a Watchdog, timer/counters or analog comparators and some of them are explained briefly below:
- Watchdog: it is a way to interrupt and reset program execution in order to prevent a code from getting caught on a loop. It will set a counter (that should be reset every loop iteration) and trigger an action once it reaches the defined timer. This counter is independent to any other clock source to the microcontroller and that makes it capable of taking care of the normal operation, without allowing the program to halt.
- Timer/counter: in the case of the ATtiny, it is an 8-bit timer/counter that allows usage of the CPU clock to basicly, count, and raise an interrupt whenever that is achieved. Since these numbers can be very high at normal execution speeds, the section counts on the so called prescaler, which is nothing else than a number that skips ticks, by factors of 8, 64, 256, 1024...
- Analog comparator interrupt: it is an interrupt triggered when the conditions in the analog comparator are attained. But what is an analog comparator? Well, put simply, it is a device that compares two analog signals and outputs the largest, or a flag. This can be set to compared to analog signals read in the input pins and to trigger an interrupt when it happens.
I will need to provide power to the ATTiny and connect it to my programmer. That is, connecting MISO, MOSI, SCK, RESET, VCC, and GND of the programmer to the corresponding pins on the ATtiny, just as seen on the picture below:
Programming with Arduino
I am using Arduino software to program my board as it is the most widely open option for further developments in differnt boards, formats, and easy to use. There are plenty of different libraries and programs that can be easily adapted with this software. I have also the old settings to program any board with my FabISP programming board.
Installing new boards
As I have my Arduino 1.8 installed, I just need to install in Arduino the ATTiny support using the built-in boards manager. In arduino's Preferences menu, I must set the following url:
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json on the Additional Boards Manager URLs field.
Then open the Tools menu and select the correct board, clock, port and programmer.
First I will open the Blink sketch from the examples menu in Arduino. So I need to upload the program to the ATtiny using the ISP.
Then I need tho change the pin numbers from 13 to 0. And finally, after verifying and uploading the program, I need to burn the Bootloader.
I got an error in the process...
Obviously, the error was due all the time to the wrong FTDI connection. It needed to flip it so that all connections matched, taking as a reference the GND pin (black wire).
Here there is the message saying the process was succesfully updated.
And finally the LED blinked.
Then I will modify the blinking timing on the code so that the LED will be switched on for 2 secs, then off for 1 sec and on for 0.5 secs.
Here you can see the new blinking process: