9. Embedded programming¶
Objectives:¶
Group assignment
For this week’s group assignment, we have to compare the performance and development workflows for different microcontroller families and document our work.
Individual assignment
As part of the individual assignment I have to read the datasheet for the microcontroller that I would be programming. I have to program the board I made that does something, with as many different programming languages and programming environments as possible.
Files¶
Arduino File: LED blinking [Arduino File:LED switch]
Tools¶
Research¶
An Overview of Microcontroller Unit:
EnTcians
SpenceKonde
Electronics Hub
- C in Atmel tutorial by Nicerland.com
- Assembler introductiob by csee.com
- Assembler in Atmel tutorial by Nicerland.com
Link with final project¶
This week’s assignment has no direct link with my final project. However, this week is more like learning the skill set required to do the programming of the final project board and all others as well. So I will keep in mind the requirements of my final project while reading the datasheet to find some answers for creating my project.
Group Assignment¶
A microcontroller is like a basic computer, It is much simpler and has much less power than even the most basic personal computer. However it consists of the same components- a processor, a RAM, a flash storage and hardware peripherals such as GPIO pins connecting sensors and circuitry. They can be called “special purpose computers.” that are capable of only doing one thing well.
Microcontroller classification:
Family:
The microcontrollers can also be classified according to their family. Family is usually a company or a manufacturer in which the controller is fabricated. Each family has its own architecture and instruction set. Some like 8051 are famous but PIC and ARM are going more popular than 8051 now.
8051.
The first microprocessor 4004 was invented by Intel Corporation. 8085 and 8086 microprocessors were also invented by Intel. In 1981, Intel introduced an 8-bit microcontroller called the 8051. It was referred to as a system on a chip because it had 128 bytes of RAM, 4K byte of on-chip ROM, two timers, one serial port, and 4 ports (8-bit wide), all on a single chip. When it became widely popular, Intel allowed other manufacturers to make and market different flavors of 8051 with its code compatible with 8051. It means that if you write your program for one flavor of 8051, it will run on other flavors too, regardless of the manufacturer. This has led to several versions with different speeds and amounts of on-chip RAM.
8051 Flavors / Members
8052 microcontroller
8031 microcontroller
8051 Microcontroller Features
8 – Bit ALU: ALU or Arithmetic Logic Unit is the heart of a microcontroller. It performs arithmetic and bitwise operation on binary numbers. The ALU in 8051 is an 8 – Bit ALU i.e. it can perform operations on 8 – bit data.
8 – Bit Accumulator:The Accumulator is an important register associated with the ALU. The accumulator in 8051 is an 8 – bit register.
RAM: 8051 Microcontroller has 128 Bytes of RAM which includes SFRs and Input / Output Port Registers.
ROM: 8051 has 4 KB of on-chip ROM (Program Memory).
I/O Ports: 8051 has four 8 – bit Input / Output Ports which are bit addressable and bidirectional.
Timers / Counters: 8051 has two 16 – bit Timers / Counters.
Serial Port: 8051 supports full duplex UART Communication.
External Memory: 8051Microcontroller can access two 16 – bit address line at once: one each for RAM and ROM. The total external memory that an 8051 Microcontroller can access for RAM and ROM is 64KB (216 for each type).
Additional Features: Interrupts, on-chip oscillator, Boolean Processor, Power Down Mode, etc.
2) AVR Microcontroller
AVR is a family of microcontrollers developed since 1996 by Atmel, acquired by Microchip Technology in 2016. These are modified Harvard architecture 8-bit RISC single-chip microcontrollers. AVR was one of the first microcontroller families to use on-chip flash memory for program storage. Flash, EEPROM, and SRAM are all integrated onto a single chip, removing the need for external memory in most applications. Some devices have a parallel external bus option to allow adding additional data memory or memory-mapped devices. Almost all devices (except the smallest TinyAVR chips) have serial interfaces, which can be used to connect larger serial EEPROMs or flash chips. Wiki
3) PIC Microcontroller
PIC is a family of microcontrollers made by Microchip Technology. The name PIC initially referred to Peripheral Interface Controller, and is currently expanded as Programmable Intelligent Computer. Early models of PIC had read-only memory (ROM) or field-programmable EPROM for program storage, some with provision for erasing memory. All current models use flash memory for program storage, and newer models allow the PIC to reprogram itself. Program memory and data memory are separated. Data memory is 8-bit, 16-bit, and, in latest models, 32-bit wide. Program instructions vary in bit-count by family of PIC, and may be 12, 14, 16, or 24 bits long. The instruction set also varies by model, with more powerful chips adding instructions for digital signal processing functions. The hardware capabilities of PIC devices range from 6-pin SMD, 8-pin DIP chips up to 144-pin SMD chips, with discrete I/O pins, ADC and DAC modules, and communications ports such as UART, I2C, CAN, and even USB. Low-power and high-speed variations exist for many types. Wiki
Individual Assignment¶
Reading the datasheet:
I found this data sheet of ATtiny24/44/84 as I would be using the board from electronic design week which has a microcontroller ATtiny44. I found it difficult to read the datasheet. It was more like reading my engineering textbook. We had to learn the same without actually knowing what it means. I want it to make sense and get the relevant information from the datasheet. As a preliminary readout, I’m trying to assess whether this particular component is suitable for my project, I can refer to the datasheet. The ATtiny 44 datasheet has almost 200 pages so we have to dig in and find the relevant information. Thankfully the pdf document makes it easy for me to search.
1. Ordering Information: I checked out the ordering information given in the datasheet and searched on google to make sense of it. When looking at the open schematics, we see that the microcontroller has a bunch of numbers at the end of its name. These numbers are known as the ordering code. I checked my datasheet to find the ordering information given. Each of the letters given stands for a particular detail. For ATtiny 44 itself there are many different types of order numbers and all their last few digits and numbers are slightly different depending on the description.
This is when I realised I’m not reading the correct datasheet for my chip ATtiny 44-SSU but I couldn’t find the datasheet for this particular chip. So I asked my instructor Joel(Joel took charge of our lab when we got back to academics after the lockdown. He was Neil’s student last year but was around all the while as he was the electrical engineer of the whole building.) what to do and he told me to continue using the same datasheet as it is pretty much the same except minor changes when it comes to placement of the pin around the chip.
2. Package Information: Second I looked at the mechanical aspect. This is very important because after laying out the schematics electrically if the mechanics don’t work, it might be a problem. So we need to search for the package information on the pdf and I found the section with the mechanical data, there is also some dimensions and the types. I realised that the mechanical dimension is very important when I want to create a very specific footprint.
3. Pin Configuration: This tells us exactly what the component does. I searched for the Pin Descriptions and it is given on page 6 and 7. This segment labels all the pin numbers and pin out and what is its function.
4. Electrical Characteristics: On searching “characteristics” I found that it is on page 155. There are a bunch of tables talking about the characteristics and I checked the voltage characteristics first. This gives the minimum and maximum input supply voltage.
5. Clock settings: I wanted to read this section because the hello echo board has an external clock of 20 MHz.
Programming my Board¶
This week we need to programme the Hello-echo board that we designed in the
Electronics Design Week.
Hello echo board is based on the Microchip ATtiny44 . It’s a 8-bit RISC based microcontroller. The original design has a UART Port.
Programming using Arduino¶
Arduino: Arduino is an open-source platform used for building electronics projects. Arduino consists of both a physical programmable circuit board (often referred to as a microcontroller) and a piece of software, or IDE (Integrated Development Environment) that runs on our computer, used to write and upload computer code to the physical board.
source:
Before I’m able to program my board I have to set up the Arduino environment for the ATtiny44 and I have to bootload the chip. First I have to Download, install and open the Arduino software.
The original Arduino software does not support the ATtiny44, so I have to add some board directories for enabling my ATtiny44 board to my arduino environment.
Step 4: I clicked on preferences under file in the main menu
Step 5: In Additional Board Manager URLs, it is empty now.
Step 6: I found the additional url from the link
Step 3: I added the Following URL http://drazzy.com/package_drazzy.com_index.json
Step 8:After adding the URL open Board Manager by clicking
Tools >> Board >> Boards Manager…
Step 9: I searched for ATtiny44 in the window that popped out.(Make sure you have Internet connection)
Step 10:I iInstalled the latest version available in the list.
Step 11: Now ATtiny44 is available as an option under the ATtiny Microcontrollers in board Selection. I selected it.
Step 12: Next I selected the Clock as 20MHz.
Step 13: As I’m using Fab ISP to programme the Hello-Echo board, I selected the Programmer as USBtinyISP.
Step 14: Burn the Bootloader.
ATTiny44 is by default set to use the Internal clock .So if you are using Arduino to program it. It will not get a program initially . We have to upload the Bootloader first.
The bootloader contains a map of the chip, it sets all the transistors in the right state and it puts the IC in programming mode. The bootloader is the little program that runs when the Arduino is turned on, or the reset button is pressed. Its main function is to wait for the Arduino software on the computer to send it a new program for the board, which it then writes to the memory on the board. The next step is to connect the programmer USBtinyISP to the Hello Echo Board with the wire through the 2 by 3 pin header. The USBtinyISP is connected to the computer with the USB2.0 hub. Here I used the USBtinyISP as a programmer. I just had to click on the burn bootloader in the Arduino menu.
Refer to the Arduino Pin Mapping given above to find the Number corresponding to the Pin Number on ATtiny44.
First I wrote a simple programme that blinks the LED
#define LED 2 #define SW 3 void setup() { pinMode(LED, OUTPUT); pinMode(SW, INPUT); } void loop() { digitalWrite(LED, HIGH); delay(1000); digitalWrite(LED, LOW); delay(1000); }
Click upload to flash the Hello-Echo Board.
Next I wrote a program to ON/OFF the LED using Switch.
#define LED 2 #define SW 3 void setup() { pinMode(LED, OUTPUT); pinMode(SW, INPUT); } void loop() { if(digitalRead(SW)==0) { digitalWrite(LED, LOW); } else { digitalWrite(LED, HIGH); } }
Programming using C in Atmel studio¶
Step 1: First I downloaded Atmel Studio and set it up.
I opened the software and chose File>>new>>project from the main menu.
Step 2: Next I selected Gcc C Executable Project and I gave the project the name “Nan Test” and clicked Ok.
Step 3: I selected the microcontroller that I used in my target board (Hello blinky) from the next window and clicked on the okay button. Unfortunately I made a mistake here and selected ATTiny 45 as the microcontroller family however it was ATTiny44A that was used in my hello blinky.
Now the Atmel environment is created.
Step 4: Next step is to add the programmer to our enviournment. Choose External Tools under Tools in the main menu to fill up the spaces as shown below.
Title:-
|
USBTiny
|
Command :-
|
avrdude.exe
|
Argument :-
|
-c usbtiny -p attiny44 -U flash:w:$(ProjectDir)Release\$(TargetName).hex:i
|
click apply and ok to save the tool.
Avrdude was already installed in the computer I was using. It can be checked by simply typing Avrdude in cmd. If it is not installed it has to be downloaded and installed.
A small window appears and press ok and check if USBtiny is added as a tool.
Step 5: I created a simple led blinking program based on what Jogin told me to do.
DDRx DDR stands for Data direction Register, it is an 8-bit register which determines whether one pin will be an input or an output pin. As I want the pin connected to the LED on the Hello-Blinky board to be set as the output, I gave the value of it as 1 on DDRx. I connected my LED to PB2, so I made it as output in DDRA. Hence I give the code to set the pin PB2 as out put by writing. DDRA=0xff; This is HEX equivalent for DDRA=ob11111111 What this does is that it will set all the pins on the PB2 as output pins.
PORTx while DDRx denotes the direction of the Pin, PORT denotes the state of it. That means if I give a value 1 in the PORT register of that pin It would be set to HIGH, or 5V in case of the micro controller.
PINx register is a dynamic register whose value will be automatically updated based on the received values of the sate of all pins, so 5V to that pin will write 1 to that register.
Step 6: For Uploading code I had to first build/compile it by using Build Soution in Build under main menu.
The first time I got the output as Build failed.
This is the console result of a failed output.
#include <avr/io.h> // AVR input/output library #define F_CPU 20000000 // Since we connected a 20MHz crystal into the circuit #include <util/delay.h> // Delay library main() { DDRA = (1 << 2); //set pins 8 in the B port as output pins. while (1) // infinte loop { PORTA |= (1 << 2); //Turns pin8 ON. _delay_ms(1000); //wait for one second. PORTA &= (!(1 << 2)); //Turns pin OFF. _delay_ms(1000); } }
I tried to find what the error was very many times and I asked Jogin to help me. He also tried a lot and couldn’t find the error.
Only sometime later while Jogin was teasing me about my poor eye sight(basically how older I’m than all of them!) and asked Eldho to read my chip that it occured to me that I have entered the wrong microcontroller as the target initially. Once that was corrected, the program compiled fine.
This is the console result of a successful output.
avrdude.exe: Version 5.10, compiled on Jan 19 2010 at 10:45:23 Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/ Copyright (c) 2007-2009 Joerg Wunsch System wide configuration file is "C:\WinAVR-20100110\bin\avrdude.conf" Using Port : lpt1 Using Programmer : usbtiny AVR Part : ATtiny44 Chip Erase delay : 4500 us PAGEL : P00 BS2 : P00 RESET disposition : possible i/o RETRY pulse : SCK serial program mode : yes parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 ByteDelay : 0 PollIndex : 3 PollValue : 0x53 Memory Detail : Block Poll Page Polled Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- eeprom 65 6 4 0 no 256 4 0 4000 4500 0xff 0xff flash 65 6 32 0 yes 4096 64 64 4500 4500 0xff 0xff signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00 lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00 Programmer Type : USBtiny Description : USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/ avrdude.exe: programmer operation not supported avrdude.exe: Using SCK period of 10 usec avrdude.exe: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude.exe: Device signature = 0x1e9207 avrdude.exe: safemode: lfuse reads as FE avrdude.exe: safemode: hfuse reads as DF avrdude.exe: safemode: efuse reads as FF avrdude.exe: NOTE: FLASH memory has been specified, an erase cycle will be performed To disable this feature, specify the -D option. avrdude.exe: erasing chip avrdude.exe: Using SCK period of 10 usec avrdude.exe: reading input file ".hex" avrdude.exe: can't open input file .hex: No such file or directory avrdude.exe: write to file '.hex' failed avrdude.exe: safemode: lfuse reads as FE avrdude.exe: safemode: hfuse reads as DF avrdude.exe: safemode: efuse reads as FF avrdude.exe: safemode: Fuses OK avrdude.exe done. Thank you.
Step 7: As the build is success the program is ready to flash using programmer Tools=>USBTinys
Here is the working of the program and the output.
Programming using Assembler in Atmel studio¶
Mistakes and Learnings¶
- When I programmed the LED blink program using Arduino I got the output very easily But when I tried to do the ON/OFF using switch program it did not work, the LED was ON all the way. I tried to figure out in many ways . Then I connected the programmer using FTDI cable. Still it didn’t work. Then I asked Joel for help and he figured out that the syntax of digitalread was wrong. Once it was corrected the program worked fine.
- When I programmed using Atmel 7 studio with C, the Build solution failed the first time. I later realised that the microcontroller chip family I gave was wrong, Once that was corrected it worked well.
- When I programmed using Atmel 7 studio with Assembler, the blink didn’t work the first time. I later realised that I gave the pin of the switch wrong. Once I connected it, it worked perfectly.