WEEK 7

07. Embedded Programming

  • Read a microcontroller data sheet.

  • Program your board to do something.

  • This week's assignment has been the hardest one so far: opening a window to an incredibly huge and unknown world definitely overwhelms you at the beginning. So the first thing I did was to read anything I could to figure out what a Microcontroller is and how it works. This is the fundamentals to move on to how you make it works. After I documented about the microprocessor I read the datasheet and concentrated on the position of the pins and correspondence with my board.

    Reading microcontroller's pin on my schematic

    Finding microcontroller's pin correspondence

    DDR, PORT and PIN operating principle

    As soon as possible I wanted to check if my board was alright so I downloaded a simple C program from Massimo Menichinelli's page and tried to program the board via my FabISP. I only had to change the PIN number in the C program. The biggest and most annoying problem is the "check connections" error I keep getting when trying to make fuse. I always have to disconnect and connect the plugs until eventually I got it to work. This problem happens to my classmates as well, but we have no idea how to solve it.

    Programming via FabISP setup

    FabISP and FTDI board as seen by my computer

    This is the Terminal screen of the "make", "fuses" and "program" operations.

    MacBook-Pro-di-Save:steadyled zaverie$ make
    avr-objcopy -O ihex steadyled.out steadyled.c.hex;\
    	avr-size --mcu=attiny44 --format=avr steadyled.out
    AVR Memory Usage
    ----------------
    Device: attiny44
    
    Program:     140 bytes (3.4% Full)
    (.text + .data + .bootloader)
    
    Data:          0 bytes (0.0% Full)
    (.data + .bss + .noinit)
    
    
    MacBook-Pro-di-Save:steadyled zaverie$ make program-usbtiny-fuses
    avr-objcopy -O ihex steadyled.out steadyled.c.hex;\
    	avr-size --mcu=attiny44 --format=avr steadyled.out
    AVR Memory Usage
    ----------------
    Device: attiny44
    
    Program:     140 bytes (3.4% Full)
    (.text + .data + .bootloader)
    
    Data:          0 bytes (0.0% Full)
    (.data + .bss + .noinit)
    
    
    avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m
    
    avrdude: AVR device initialized and ready to accept instructions
    
    Reading | ################################################## | 100% 0.00s
    
    avrdude: Device signature = 0x1e9207
    avrdude: reading input file "0x5E"
    avrdude: writing lfuse (1 bytes):
    
    Writing | ################################################## | 100% 0.00s
    
    avrdude: 1 bytes of lfuse written
    avrdude: verifying lfuse memory against 0x5E:
    avrdude: load data lfuse data from input file 0x5E:
    avrdude: input file 0x5E contains 1 bytes
    avrdude: reading on-chip lfuse data:
    
    Reading | ################################################## | 100% 0.00s
    
    avrdude: verifying ...
    avrdude: 1 bytes of lfuse verified
    
    avrdude: safemode: Fuses OK (H:FF, E:DF, L:5E)
    
    avrdude done.  Thank you.
    
    MacBook-Pro-di-Save:steadyled zaverie$ make program-usbtiny
    avr-objcopy -O ihex steadyled.out steadyled.c.hex;\
    	avr-size --mcu=attiny44 --format=avr steadyled.out
    AVR Memory Usage
    ----------------
    Device: attiny44
    
    Program:     140 bytes (3.4% Full)
    (.text + .data + .bootloader)
    
    Data:          0 bytes (0.0% Full)
    (.data + .bss + .noinit)
    
    
    avrdude -p t44 -P usb -c usbtiny -U flash:w:steadyled.c.hex
    
    avrdude: AVR device initialized and ready to accept instructions
    
    Reading | ################################################## | 100% 0.00s
    
    avrdude: Device signature = 0x1e9207
    avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
             To disable this feature, specify the -D option.
    avrdude: erasing chip
    avrdude: reading input file "steadyled.c.hex"
    avrdude: input file steadyled.c.hex auto detected as Intel Hex
    avrdude: writing flash (140 bytes):
    
    Writing | ################################################## | 100% 0.20s
    
    avrdude: 140 bytes of flash written
    avrdude: verifying flash memory against steadyled.c.hex:
    avrdude: load data flash data from input file steadyled.c.hex:
    avrdude: input file steadyled.c.hex auto detected as Intel Hex
    avrdude: input file steadyled.c.hex contains 140 bytes
    avrdude: reading on-chip flash data:
    
    Reading | ################################################## | 100% 0.34s
    
    avrdude: verifying ...
    avrdude: 140 bytes of flash verified
    
    avrdude: safemode: Fuses OK (H:FF, E:DF, L:5E)
    
    avrdude done.  Thank you.

    And the LED lights up!

    LED is steady, button turn it off and on at every click.


    Write a program and load it on the board

    After that I tried to modify the C program, but it was really too difficult to do it in a week without knowing anything about C. I tried instead working with Arduino IDE. To do so, I first installed an AVR set of information for the Arduino IDE to make it recognize my ATTiny, then tried different setup with LED blinking at different speed and button doing different operations.

    /*modified 8 May 2014
      by Scott Fitzgerald
      
      modified 17 March 2015
      by Saverio Silli for Fab Academy 2015 week 7 excercise
     */
    
    
    // the setup function runs once when you press reset or power the board
    void setup() {
      // initialize digital pin 7 as an output.
      pinMode(7, OUTPUT);
    }
    
    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(100);              // wait for a second
      digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
      delay(100);              // wait for a second
    }

    Video: LED blinks, button has no use.


    /*
      Button
    
     Turns off a light emitting diode(LED) connected to digital
     pin 7, when pressing a pushbutton attached to pin 3.
    
    
     
     created 2005
     by DojoDave 
     modified 30 Aug 2011
     by Tom Igoe
     
     modified 17 March 2015
     by Saverio Silli for Fab Academy 2015 week 7 excercise
     
     */
    
    // constants won't change. They're used here to
    // set pin numbers:
    const int buttonPin = 3;     // the number of the pushbutton pin
    const int ledPin =  7;      // the number of the LED pin
    
    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status
    
    void setup() {
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);
    }
    
    void loop() {
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
    
      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) {
        // turn LED on:
        digitalWrite(ledPin, HIGH);
      }
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
      }
    }

    Video: LED is steady, button turn it off when kept pressed.


     /*This sketch modify the frequency of an LED when the button is pressed.
     
    Created 17 March 2015
    by Saverio Silli for Fab Academy 2015 week 7 excercise
     */
    
    int delay_value = 100;
    int led_pin = 7;
    int button_pin = 3;
    void setup() { 
      pinMode(led_pin, OUTPUT); pinMode(button_pin, INPUT); 
    }
    void loop() { 
      digitalWrite(led_pin, HIGH);
      delay(delay_value);
      digitalWrite(led_pin, LOW);
      delay(delay_value);
      int button_state = digitalRead(button_pin);
      if (button_state == HIGH) { 
        delay_value = 10; 
      } else { 
        delay_value = 100; 
      } 
    }

    Video: LED blinks, button alternates blinking (faster/slower).


    Program files are available here:

  • Program Files