• Stefano Galbo
  • Beginner

Assignments


  • Read a microcontroller datasheet
  • Program your board to do something

Introduction


Doubtless this week was the most difficult in Fab Academy for me: I didn't know anything not only about programming, but also about basic concepts like binary code, boolean operations, and C language. Our classmate Gianfranco Caputo spent a lot of time to teach me (thanks for the patience!) but honestly this argument is too hard to learn in few days.

I didn't find a clear tutorial, so I made many exercises with NOT, OR, AND, SHIFT, IF, FOR, WHILE and their operations, but I was never sure of what I was doing.

Datasheet


I downloaded datasheet of our Attiny 44 from Fab Academy site: we read it all togheter and we talked about most important parts:


Pin configuration (pag.2): I don't remember all the acronyms but I know they are important to write the code. In my schematic, LED and BUTTON are on PA3 and PA7.






Microcontroller architecture (pag.4): ALU is the place of mathematical calculations; PROGRAM FLASH is the place where my code will finish; SRAM is a temporary memory; EEPROM is an hard memory to save data even after shutdown.

























Initial value (pag.66): these are default values in PORT and DDR registers, 0 in DDR register means that all the pins are as input, if I want to connect an output (LED for example) I will have to change in 1.





Electrical characteristics (pag.173): here I see electrical limits of my microcontroller and its pin, in order to use the right voltage.

Programming


In order to test my (little) knowledge, I tried to write a code that makes the LED blinking (here, C file)


First I compiled the #include part, pasting and coping from hello.ftdi.44.echo.c: these are libraries from which the software has to take.




Then I went to #define part: these commands are helpful to simplify the main.

Instead of writing every time pin number of LED, or the formula to set high value on a pin, I used these definitions in order to make "parametric" my main: if I change LED pin in my schematic from PA3 to PA7, I don't have to change many times in the main, but just one time in #define (PA7 instead of PA3), and the code automatically changes every time I write LED_pinnum.




The main is where microcontroller starts to execute the code and find all the operations to do.

First you have to set initial situation of pins, in my case I set button as an input, and led as an output, a turned off output (I looked at #define to remember how to write this, very useful the //comments!).




Second step is the command.

In my case I use "while (1)": while introduces a cycle, condition (1) is always true, so it's the same of "Forever".

Set and clear mean turn on and turn off the led, but I need to insert a delay of 1 second to make visible the blinking, infact without it the microcontroller executes blinking command at a speed (depending of resonator) too fast for human eyes.


Makefile


Next step was to build my makefile, so I downloaded hello.ftdi.44.echo.c.make from Fab Academy site, and I changed it for my needs.

In particular, I set to use external resonator (instead of internal one) with a 20 Mhz frequency, and I deleted all the programmers except the Fabisp, that I will use. This is my make file (here,file):




With C file and makefile in the same folder, I launched windows prompt, I went in that folder, and I wrote this command, in order to upload my program to microcontroller:




Advice: if your AVRDUDE doesn't work, follow this topic.

Final step


I made few changes in order to obtain an interaction from led and button, and to use also the if/else command: it's very simple, if there is a condition microcontroller makes something, if there isn't this condition it makes something else.

My goal is to create a slow blinking of led if I don't push the botton, and faster when botton is pushed.

These are my definitive C file and its makefile. I'm sure my program is very simple and I made something wrong, but this is my first programming experience and I'm not able to do better at the moment.




Before uploading the program, I had to install usbtinyisp drivers from here; then I executed make program command..and my led started to blink!