BACK

Week 9 - Embedded Programming

We had already flashed our ISP programmer in week 5 (electronics production). We modified the design of the echo-hello-world by adding an LED and a switch; modified the fuses and tested it in week 7 (electronics design). This week we had to learn different ways to embed a program in the chip. We used the LED and switch on the echo-hello-world board for different programs using C language directly and with Arduino environment.

I have added some fragmented documentation of codes in assembly language and in python that I learnt from others but didn't complete trying it myself. I hope to try these in the future.

1. Microcontroller - Basics
2. C programming language to control the LED using the switch
3. Arduino to control the LED using the switch
4. Assembly language to control the LED using the switch
5. Python programming in Raspberry pi to control the LED using the switch


Some video clips from the week..

1. Microcontroller - Basics


As per Wikipedia: AVR is a family of microcontrollers developed by Atmel beginning in 1996. These are modified Harvard architecture 8-bit RISC single-chip microcontrollers. RISC is the short-form for Reduced Instruction Set Computer. AVRs are classified into the ATtiny series, ATmega series, ATxmega series and so on. From the datasheet,'By executing powerful instructions in a single clock cycle, the ATtiny24A/44A/84A achieves throughputs approaching 1 MIPS per MHz allowing the system designer to optimize power consumption versus processing speed'.



Free HTML5 Bootstrap template
The pin-out configuration of ATtiny 22A/44A/84A as given in the datasheet.
Free HTML5 Bootstrap template
Refer to http://www.avr-tutorials.com/general/microcontrollers-basics for description of each component of a microcontroller.
Free HTML5 Bootstrap template
Consider x to be A/B/C/D depending on the available ports in the AVR. The DDRx registers are used for configuring the data direction(input/output) of the port pins. PORTx register is used to write the values to the port pins in output mode. PINx register is used to read data from port pins in input mode.
Free HTML5 Bootstrap template
These are the truth tables of AND, OR and NOT gates, which are used in programming to refer or assign value to one pin in a port, without affecting the other pins in the same port.
Free HTML5 Bootstrap template
The hexadecimal-binary conversion table. In binary, a bit that is at a positive voltage is referred to as 1 and when it is at zero voltage, it is referred to as 0. The hexadecimal system is used because it can represent every byte (8 bits) as two hexadecimal digits.

2. C programming language to control the LED using the switch


To program an AVR microcontroller, we need to write a code in C, compile it with avr-gcc and transfer it into the microcontroller using avrdude. The AVR toolchain (avr-gcc, avrdude and avr-libc) is freely downloadable. Read this Fab academy tutorial to know about the make command and the Makefile.

Latch switch for LED (.C file)
Press-and-hold switch for LED (.C file)



Free HTML5 Bootstrap template
The circuit design of my board to be programmed. The LED is at PA7 and the switch is connected to PB2 of the Attiny45 chip.
Free HTML5 Bootstrap template
The filename is to be edited in the makefile before compiling.
Free HTML5 Bootstrap template
The C program written in Xcode to burn the LED when the switch is pressed and held.
Free HTML5 Bootstrap template
When compiled using in the terminal, errors about missing parantheses popped up.
Free HTML5 Bootstrap template
Post debugging, the program is successfully compiled and transferred into the chip.
Free HTML5 Bootstrap template
The C program to make the latch switch. Tap once to turn on the LED and tap it again to turn it off. Initially, the program did not work correctly every time due to the additional sparks that must have been set off due to bounce effect when the switch is pressed. Then I added a delay of 50ms after the LED burns and this countered the bounce effect.

3. Using Arduino to program the chip


To program an attiny chip using Arduino, we need to add the board library using the option to add in Preferences. The steps are explained below. The code written to make the LED blink when the board is powered is given below.
Blinking LED (.ino file)



Free HTML5 Bootstrap template
To use Arduino for programming, we have to first add attiny library to the board manager. To do that, go to Preferences and paste this link https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json in the Additional Board Manager URL box.
Free HTML5 Bootstrap template
Now search for attiny in the Board manager and select it.
Free HTML5 Bootstrap template
Next, in Tools, choose ATtiny44 as the Board and Processor. Since we have an external 20 MHz oscillator, choose that as the clock.
Free HTML5 Bootstrap template
After writing the program, choose Sketch>Compile and then debug, if any. Follow up with 'Upload using Programmer'.
Free HTML5 Bootstrap template
The code in Arduino to make the LED blink when the board is powered. The following are different functions used in the code. pinMode() configures the specified pin to behave either as an input or an output. Use digitalWrite() to write a HIGH or a LOW value to a digital pin. digitalRead() is used to read the value from a specified digital pin, either HIGH or LOW.

4. Assembly language to control the LED using the switch


This is Yadu's program. I wanted to try this. However, I did not succeed (because I didn't spend too much time in searching) in getting the right compiler. So I have just added the file and screenshot, in the hope of learning and trying this some other time.
Blinking LED (.s file)



Free HTML5 Bootstrap template
The program in assembly language to make a blinking LED.

5. Python programming in Raspberry pi to control the LED using the switch

Amith brought a 'Raspberry Pi' board to the lab and explained it to us. I didn't try programming it myself but hope to, some time in the future.




Free HTML5 Bootstrap template
Amit wrote this Python program to control the LED using the Raspberry Pi board.
Free HTML5 Bootstrap template
Use the command 'python filename.py' to run the file.

BACK