9. The Programming of Embedded Microcontrollers¶
Goal(s):
- Individual assignment:
- Read a microcontroller data sheet
-
Program your board to do something (with as many different programming languages and programming environments as possible)
-
Group assignment:
- To compare the performance and development workflows for other architectures
Introduction¶
In this week we have to read and understand a microcontroller data sheet and then program the echo hellow world, made in week7 to control the LED using a switch. Refreshed the lessons from week7 and gone through the datasheet of Attiny85 and through the tutorials of embedded C and basics of AVR architectures. Again as with the other electronics exercises, this was also very new and the learning curve was very high here.
tinyAVR Microcontrollers¶
Atmel ATTiny category of microcontrollers are optimized for applications that require performance, power efficiency and easy of use. All tinyAVR devices are based on the same architecture and compatible with other AVR devices.
To know more about the features of the tiny85 category click here
Attiny85¶
The high-performance, low-power Microchip 8-bit AVR RISC-based microcontroller combines 8KB ISP flash memory, 512B EEPROM, 512-Byte SRAM, 6 general purpose I/O lines, 32 general purpose working registers, one 8-bit timer/counter with compare modes, one 8-bit high speed timer/counter, USI, internal and external Interrupts, 4-channel 10-bit A/D converter, programmable watchdog timer with internal oscillator, three software selectable power saving modes, and debugWIRE for on-chip debugging. The device achieves a throughput of 20 MIPS at 20 MHz and operates between 2.7-5.5 volts.
By executing powerful instructions in a single clock cycle, the device achieves throughputs approaching 1 MIPS per MHz, balancing power consumption and processing speed.
Data Sheet¶
To really understand the microcontroller, one you would need to read the full datasheet, but this was clearly impossible given that did not have electronics training to start with.
The datasheet gives useful information regarding the Microcontroller. For example, 3 different Microcontrollers - the ATtiny25/ATtiny45/ATtiny85 all are with the same architecture and features. The difference between them are the 2/4/8K Bytes In-System Programmable Flash, 128/256/512 Bytes of In-System Programmable EEPROM and 128/256/512 Bytes of Internal SRAM respectively. The same is also apply to ATtiny24A/ATtiny44A/ATtiny84A. The datasheet also gives an insight of how many programmable I/O lines is within ATtiny 45/85 as compared to that of Attiny44A.
Characteristics of the ATtiny45/85¶
- There are 8 pins on this chip
- VCC for supplying voltage
-
GND is ground
-
Port B (PB5:PB0) is a 6-bit bi-directional I/O port with internal pull-up resistors. Port B has alternate functions as analog inputs for the ADC, analog comparator, timer/counter, SPI and pin change interrupt, crystal oscillator Input/Output and many more RESET - a low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running and provided the reset pin has not been disabled.
Three different types of memory
-
Flash memory: A type of memory that works like both RAM and ROM. You can write information to flash memory just like in RAM, but that piece of information wont be erased when the power is off just like a ROM.
-
EEPROM: A read-only memory whose contents can be erased and reprogrammed using a pulsed voltage.
-
SRAM: A type of RAM that stores data using a static method, in which the data remains constant as long as electric power is supplied to the memory chip.
-
CPU (Central Processing Unit) is the electronic circuitry that carries out instructions by performing the basic arithmetic, logical, control and input/output (I/O) operations specified by the instructions.
The acronyms are described by the following image
Board Programming¶
Within the datasheet, there is a lot of information to be understood and absorbed, but the datasheet itself does not teach you how to use the ATtiny chip. It is application notes or tutorials that is required if one really wants to use the ATtiny. For now, we will be focusing on how to program the ATtiny45/85 MCU in the Hello World board to do something using either Arduino or the FabISP.
ISP (In-system programming) also called In-Circuit Serial Programming (ICSP), is the ability of some programmable logic devices, microcontrollers, and other embedded devices to be programmed while installed in a complete system. With reference to How Does ISP work?. It is performed using only four lines, data is transferred through two lines only and is shifted bit by bit. MOSI (data input) line, with a clock cycle between each bit and the next (on the SCK (clock input) line). MISO (data output) line is used for reading and for code verification, it is only used to output the code from the FLASH memory of the microcontroller. The RST (used to activate the serial Programming) pin, which is normally used to reset the device, is also used to enable the three pins (MOSI, MISO and SCK) to be used for ISP simply by setting RST to HIGH (5V).
Arduino Uno as ISP¶
I will be using Arduino Uno and Arduino IDE to program my FabISP board. As the support file for the Attiny does not come with Arduino IDE, I made reference to this site to add and install the additional support for it.
In Arduino IDE, go to File > Preference > Under Additional Board Manager URLs: > Add the following line
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
Next go to tool > Board > Board Manager > Search for Attiny and Install.
With the Arduino IDE support for Attiny setup, I follow this guide to setup the Arduino UNO and the FabISP board.
Connect the Arduino Uno to FabISP using one 2x3 connector with cable in the following order:
Uno –> FabISP 10 –> Reset 11 –> MOSI 12 –> MISO 13 –> SCK 5V –> VCC GND –> GND
After doing this in my previous assignment, I was able to use the Arduino as ISP directly with the IDe to program the boards. Below are the programs that I used and applied in the AVR programming.
Arduino IDE as Programming Environment¶
As I covered in the Design Assignment, I was able to use the Arduino IDE to program my board and explore all the example Sketches within the IDE itself.
Atmel Studio as Programming Environment¶
As documented by other students in the Fab Academy Archive, I decided to give Atmel Studio 7 a try for Programming because…
Studio 7 is the integrated development platform (IDP) for developing and debugging all AVR® and SAM microcontroller applications. The Atmel Studio 7 IDP gives you a seamless and easy-to-use environment to write, build and debug your applications written in C/C++ or assembly code. It also connects seamlessly to the debuggers, programmers and development kits that support AVR® and SAM devices.
Additionally, Studio includes Atmel Gallery, an online app store that allows you to extend your development environment with plug-ins developed by Microchip as well as third-party tool and embedded software vendors. Studio 7 can also seamlessly import your Arduino sketches as C++ projects, providing a simple transition path from Makerspace to Marketplace.
Since I was starting out with AVR programming, I was not sure how effective Atmel Studio would be over the Arduino IDE, but decided to try anyway
For programming first install and open ATMEL STUDIO. Click “New Project” and select “GCC C Executable project”. Click ok to confirm the selection
Now you can see the following window. Select device famiy as ATTiny and select ATTiny85. Click OK to confirm the operation.
You will see a list of supported tools for the ATTiny85 on the right side.
Now you will get a window like below
Now we need to setup some parameters for usbtiny debugging. For that Go to “Tools” and Select “External Tools…”
Add following details to the box:
Debug Version Title:- USBTiny ISP Debug Command :- avrdude.exe Arguments :- -c usbtiny -p attiny85 -U flash:w:$(ProjectDir)Debug\$(TargetName).hex:i