Embedded Programming

Its programming time !!, so here we are at week #8, and this week is about embedded programming, and for this weeks assignment we wre assigned to read a microcontroller data sheet, and program our board to do something, with as many different programming languages and programming environments as possible, and as an extra credit: experiment with other architectures.

Reading a microcontroller data sheet:

So as mentioned in the assignment we had to read a microcontroller data sheet, and that is what I wanted to do untill I saw this "data sheet", first thoughts: it looked like a magic spell that you are not spouse to read, I mean who can read 286 pages of I dont know what, but I realized that you dont have to read everything you only have to read the necessary information that you need.

After some long explanation from our Guru Francisco and some struggling I finally got to understand somethings from this data sheet. So the first picture in the data sheet was this picture below, which shows the pin configuration of the ATTiny44 which is the microcontroller that am using in my board for this week, and from this picture you get that the ATTiny44 has a VCC pin to supply voltage, ground pin, 4 pins for PORTB (PB0 to PB3) which is a 4-bit I/O port with internal pull-up resistors, and 8 pins for PORTA (PA0 to PA7) which a 8-bit I/O port with internal pull-up resistors.

So I took my board from week #6 (Electronics Design), and the first thing that came to mind is to try programming it to do the blink sketch, because before fabacademy I was familiar with the arduino and this is the first excercise you do as a beginner in arduino.

So I thought that this is going to be easy as with the arduino but it turned out to be not, the first question that came to my mind is what software I need to download to start writing my code in it, but it turned out the I need any text editor only!!

Then I discoverd that I need two things, my code ofcourse in one file and something called a makefile in another file. What I understood later that makefiles are a simple way to organize code compilation. Before starting to write any code, Francisco gave us a lecture about data direction register, and how it is programmed to select whether each individual input/output pin is configured as an input or an output. then we learned a series of commands. For example:


DDRA |= (1 << PA5);     set pin 5 in port A as output.

DDRB &= ~(1 << PB2);    set pin 2 in port B as input.

PINA &(1<< PA3)     test bit if it is High

PORTA &= ~(1 << PA5)     write  "Low" at  pin 5 of port A

PORTB |= (1 << PB2);     write  "High" at pin 2 of  port A

PINA &(1<< PA3)     test bit if it is High

Then I started writing my code, and it should be easy to blink an LED but this was my first time using C language so it was not that easy, but withsome guide from Francisco and some tutorials I was finally able to finish my code. After uploading the code I faced a strange problem, I mean the LED should blink for one second but instead it took 10 seconds to blink, actully I was stuck for a while with this not knowing what to do, but after asking Francisco it appeared that I had a problem with the fuses and I got a little help from my colleague Hashim because he faced similar problem, the problem was that the burn fuese command that I used was

avrdude -p t44 -P usb -c avrisp2 -U lfuse:w:0x5E:m

this command was to set the clock to be 20 MHz, then I changed it to this

avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0xEC:m

which will make the clock to be 8 MHz, because I was using an 8MHz crystal oscillator.To uploud the code using my programmer from week 4, I typed this command in the terminal sudo make program and to burn fuses I typed sudo make fuses . After that the code worked as shown in the video below.

// Mouaz Ali
// BLINK
// March 2017
// CC-SA-BY 4.0 License

#include <avr/io.h>
#include <util/delay.h>

int main (void) {

DDRA |= (1 << PA5); // set PA5 as out put in DDRA


while(1) {

PORTA |= (1<< PA5); // set PA5 HIGH

_delay_ms(1000); // delay 1s

PORTA&= ~(1<< PA5); // set PA5 LOW

_delay_ms(1000);


}

}

Explaining the code: - Defining the delay function #include<util/delay.h - Assigning the LED as output DDRA |= (1 << PA5); - Set PA5 to HIGH PORTA |= (1<< PA5); - Set PA5 to LOW PORTA &= ~(1<< PA5); - Put a delay of 1000ms = 1s _delay_ms(1000)

Future thoughts

I didnt mention above but Francisco also gave us a lot of things, like timers, prescaler and ADC which I think that am going to use in my final project, I understood some stuff about these things but I hope to learn more and more about these and much more during fabacademy and of course after as well.

Previous Week |-----------| Next Week

Home Page

Attachments:

blink code

makefile