Week 8 : Embedded Programming

March 14 - 21 2018

Things to do :

  • Read a microcontroller data sheet.
  • Program your board to do something, with as many different programming languages and programming environments as possible.

AVR Programming


Thanks to my studies I already known something about programming in C but I never tried to program an AVR.
My adventure started whit this video from Elliot Williams , the author of "Make: AVR Programming, Learning to Write Software for Hardware".The Video cover the bases of the world of AVR programming in a very simple way.
Once I understood how the AVR works I moved on looking back to the week 6 and checked where I hooked up the led and the button.

Then I write a little program in C that make the led blink 3 times when I click the button.

Tips: the line PORTA |= (1 << PB3) that I commented PA3 Default value is redundant for my Hello Board.The reason is that I provided an hardware pull-up resistor that link directly the pins to Vcc when the button is not pressed.
That line wouldn't be reduntant if I hadn't the external pull-up resistor.

I downloaded and edited the Neil's Makefile :

(Hold the left button on the image to zoom the images)

The next images show the rest of the toolchain :
  • 1: Set the fuses

  • 2: Compile the .c file and produce the .hex and .out files

  • 3: If the previous step went good this should be the situation of the folder

  • 4: Flash the program into the ATtiny44

  • The final result:


    I made this simple programm not considering the Datasheet.I did it only whit the skills I learned by watching the video linked above.When I moved on reading the Datasheet I didn't know how to read it but I did know that DDR is the Data Direction Register and every bit of this register control if the signal,in the referenced pins,is an Input(0) or an Output(1).I also knew that PORTA/B control the values (1 or 0 , High or low) of the pins A/B.
    So when I found this section :

    and I read what I already knew I figured out how to use the datasheet.
    It's all about the special function registers.So I decided to test a feature that the ATtiny44 provides: The temperature sensor.

    Temperature measurement

    The Datasheet has everything you need to know about every feature you want to use.I wanted to use the Temperature sensor so I jumped to the chapter 16 'Analog to digital converter'.I read almost everything focusing on the paragraph usefull for my purpose.
    I listed below the screenshots of the datasheet whit the informations I needed:


    Once I was ready to code I wrote a program that should lights a led up if the temperature reaches 22 Celsius. (See the comments in the code to understand the steps I followed)

    But it didn't work,so I needed to use the serial comunication in order to have a cleaner feedback:

    The measured temperatured never changed.The reason why it happened was that I was reading the two registers,ADCH and ADCL,at the same time,but the datasheet clearly explain that ADCL must be read first,then ADCH.

    So I fixed it and I gave the proper name to all the bits instead of using general numbers.

    And finally it worked !!!