# Embedded programming

In this week we have to program the board we designed in the electronics design week, after reading the data sheet of micro-controller we're going to use (Attiny44)

I started reading the data sheet. The data sheet is filled with information that I don't understand. And it is really difficult for a beginner. The first I read in the cover is that flash memory has only 10,000 write/erase cycles. And then I remembered that our guru Francisco told us about this in `Pre-Fab Academy`. Before Fab Academy I thought these chips would last forever.

I saw the datasheet as a mine of information. The more you dig in the datasheet, the more information that you will find. But digging is actually a laborious task and certainly not an easy one. Moreover, the information that surfaces is not always ready for consumption. Okay, I need to use less metaphors and clearer sentences. The thing is, going through the first 50 and so pages of the datasheet made me realize two things.

First, is the datasheet is full of information thatâ€™s not always easy-to-follow and/or digest. My second realization hit me when I was looking at some of the example codes in the datasheet written in assembly and C. Assembly language is literally out of this world and I should be more thankful for the existence of the readable and human-friendly C language. But even to understand the C code examples you need learn about programming micro-controllers.

## Programming the board

To start programming the board, our guru Francisco Sanchez gave us a class about micro-controllers and how to write in registers.

We have two ways to write in registers:

1- Write in the whole register like `DDRA = 0b00000001`. This will set all bits as input and only one bit will be set as output.

2- The other way is modifying only one bit:
- `DDRA |= (1<<PA3)` to write 1 in PA3 - `DDRA &= ~(1<<PA3)` to write 0 in PA3 - `DDRA & (1<<PA3)` to test the bit in PA3 (Asking "is it 1?") - `DDRA ^= (1<<PA3)` to toggle the bit in PA3 (If it is 0 it will change it to 0, and if it is 0 it will change it to 1)

Firstly, let's take a look on the board I made in the electronics design week. The button is connected to `PA3` so this will be an input and the LED is connected to `PB2` so this will be an output.

Now, let's write the make file. The most important part about it is that the `PROJECT` name should match the name of the file you're going to write for the C code, and also to write the correct name of the microcontroller and the frequency of the resonator.

``````PROJECT=embeddedprog
SOURCES=\$(PROJECT).c
MMCU=attiny44
F_CPU = 20000000

CFLAGS=-mmcu=\$(MMCU) -Wall -Os -DF_CPU=\$(F_CPU)

\$(PROJECT).hex: \$(PROJECT).out
avr-objcopy -O ihex \$(PROJECT).out \$(PROJECT).c.hex;\
avr-size --mcu=\$(MMCU) --format=avr \$(PROJECT).out

\$(PROJECT).out: \$(SOURCES)
avr-gcc \$(CFLAGS) -I./ -o \$(PROJECT).out \$(SOURCES)

program: \$(PROJECT).hex
avrdude -p t44 -P usb -c usbtiny -U flash:w:\$(PROJECT).c.hex

fuses: \$(PROJECT).hex
avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m``````

After that, I wrote a program that will turn on the LED when I press the push button.

``````// Ahmed Abobaker - Fab Academy 2017
// 16 April 2017

#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>

int main (void) {
DDRA &= ~(1 << PA3); //Setting PA3 as input
DDRB |= (1 << PB2); //Setting PB2 as output

while(1) {
if ( PINA & (1<<PA3) ) {
PORTB |= (1 << PB2);
}
else{
PORTB &= ~(1 << PB2);
}
}
}``````

I uploaded the program using my programmer using this command in the terminal `sudo make program`. And here is the result:

Not so good, as you can see in the video that the button is sensitive, when the board moves the LED turns on. I asked Fransisco about the problem and he told me that the problem is that I don't have a pull down resistor.

I checked the data sheet of attiny44 and I found out that there is no built-in pull down resistor.

Here is a resourse that explains pull up and pull down resistors.

So, I decided to modify the design of the board by removing the copper between the button and VCC and then using a jumper wire to connect the button to GND and then use the built-in pull up resistor in the microcontroller. The modified design will be like this.

Before modifying the board I needed to desolder the `pin header` because it was broken and this is the result:

The traces was removed because I used a tweezer to remove the pin header by force which was a mistake obviously. So, I decided to use the board I made for the input devices week and and modify it the same way I was going to do for the previous board. Here is the design :

The board after removing the copper between the button and VCC and then using a jumper wire to connect the button to GND:

Then I wrote this program

``````// Ahmed Abobaker - Fab Academy 2017
// 16 April 2017

#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>

int main (void) {
DDRA &= ~(1 << PA3); //Setting PA3 as input
DDRB |= (1 << PB2); //Setting PB2 as output
PORTA |= (1 << PA3); // This is to activate the pull up resistor

while(1) {
if ( PINA & (1<<PA3) ) {
PORTB &= ~(1 << PB2);
}
else{
PORTB |= (1 << PB2);
}
}
}``````

I uploaded the program and here is the result: