Fab Academy 2015
How to make almost anything

Luis Peña Mendoza

    Assignment 7 - week 7 - Mar 11

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

    Embedded Programming

    This week assignment was to read the datasheet of the Attiny44A and program your board to do something, with as many different programming languages and programming environments as possible. For this I'll be using Arduino IDE with C code and Arduino code. The first step for programming the board is to recognize the ATTiny44A pins that corresponds to our button and LED. In the schematic below I connected the Buttons to pin 12-13 and the LEDs to pin 10-11, this correspond in the Tiny datasheet to PA0-PA1 and PA2-PA3 respectively

    A. The Datasheet of the Attiny44A

    The Attiny44A is a small, cheap microcontrollers that are convenient for running simple programs. We can say that this microcontroller has 8-pin for the port A and 4-pin to the port B. Alimentation of 5v, grounded and pin to reset. The pins of the ports A and B are of digital input and output, in the port A we have converters analog/digital and also they can be used as analog pins.

    Ports A and B each consist of an 8-bit peripheral data register (PR) and an 8-bit data direction register (DDR). If a bit in the DDR is set to a 1, the corresponding bit position in the PR becomes an output. If a DDR bit is set to a 0, the corresponding PR bit is defined as an input. When you READ a PR register, you read the actual current state of the I/O pins (PA0-PA7, PB0-PB7, regardless of whether you have set them to be inputs or outputs. Ports A and B have passive pull-up devices as well as active pull-ups, providing both CMOS and TTL compatibility. Both ports have two TTL load drive capability. In addition to their normal I/O operations, ports PB6 and PB7 also provide timer output functions.

    Datasheet Attiny44A.

    B. Ensambling the ISP connector

    First, we cut the secure border from the wire, then select just 6 colors, to the connectors from ISP (MISO, SCK, RST, VCC, MOSI, GND). Pass the wire with the header, and then secure whit the other header, and repeat with the other side. We tested the ISP connector with the Hello Eccho and the FABISP. It worked perfect!

    C. Programming with Arduino

    Install Arduino IDE
    Download and install the Tiny libraries, download from here
    Set board
    >


    Set Serial Port


    Set Programmer


    Start Coding!


    Uploading using Arduino IDE

    We connect the micro usb to the FABISP, and the FABISP with the ISP connector to the Hello Board, and the Hello Board, with the FTDI wire to the computer.Then we download the file in C, and check its functionality.



    The objective of my program was: to use push two buttons with digital inputs to turn two LEDs on & off .
    C code: AVR GCC
    //Include Files 
    #include<avr/io.h> 
    #define F_CPU 128000UL 
    #include<util/delay.h>
    int main(void) {
      DDRA |= 1<<2|1<<3;//Declare as outputs 
      PORTA |= 1<<2|1<<3;
      //Switch off the LEDs
      DDRA &= ~(1<<0|1<<1);//Input declared 
      PORTA |= (1<<0|1<<1);//Pull up Enabled 
      while(1){
        //switch1
        if(!(PINA&(1<<0))) //If pressed 
        {
           _delay_ms(10);//debounce
           while(!(PINA&(1<<0)));
              //wait for release 
              _delay_ms(10);//debounce  
              PORTA^= (1<<3);//Toggle
        }
        //switch2 
        if(!(PINA&(1<<1)))//If pressed 
        {
          _delay_ms(10);//debounce
         while(!(PINA&(1<<1)));
        //wait for release 
        _delay_ms(10);//debounce 
        PORTA^= (1<<2);//Toggle
        }
      }
    }
    

    Arduino code: Wiring
    const int  buttonPin = 0;    
    const int ledPin = 2;       
    const int  buttonPin1 = 1;    
    const int ledPin1 = 3;       
    
    int buttonPushCounter = 0;   
    int buttonState = 0;        
    int lastButtonState = 0;     
    int buttonPushCounter1 = 0;   
    int buttonState1 = 0;        
    int lastButtonState1 = 0;     
    
    void setup(){
      pinMode(buttonPin, INPUT);
      pinMode(ledPin, OUTPUT);
      pinMode(buttonPin1, INPUT);
      pinMode(ledPin1, OUTPUT);
    }
    
    void loop(){
      buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState){
        if (buttonState == HIGH){
          buttonPushCounter++;
        } 
        else{
        }
      }
      lastButtonState = buttonState;
      
      if (buttonPushCounter % 2 == 0){
        digitalWrite(ledPin, LOW);
      } else{
       digitalWrite(ledPin, HIGH);
      }
      
      
      buttonState1 = digitalRead(buttonPin1);
      if (buttonState1 != lastButtonState1){
        if (buttonState1 == HIGH){
          buttonPushCounter1++;
        } 
        else{
        }
      }
      lastButtonState1 = buttonState1;
      
      if (buttonPushCounter1 % 2 == 0){
        digitalWrite(ledPin1, LOW);
      } else{
       digitalWrite(ledPin1, HIGH);
      }
    }
    

    Programming FabHello
    We opened up the IDE of Arduino and we enter a Tools/Boards to see that effectively it recognized the files added.

    In Linux
    We open the terminal and start the program Arduino, writing "arduino" in the terminal.
    We enter in file/preferences and localize the location of the sketchbook folder.
    Within the folder "sketchbook" we created the folder "hardware".
    We copy the folder tiny within the folder hardware.
    Within the folder "tiny" we created a document "boards.txt"
    We open the file "Prospective Boards.txt" that is within the folder "tiny" and we copy all the contents of the file to "boards.txt"
    We save the changes in the file "boards.txt"
    We open the IDE of Arduino and enter to Tools/Boards to see that actually recognized the files added.
    To programming is necessary to revise our schematic and use the next table below to work with pins of the microcontroller:



    In my case I am using the pin 10-11 to the leds and the pin 12-13 for the buttons. for the button in the Arduino IDE.
    Once that we already have the program, we are going to Tools/Boards to select the plate "Attiny 44 external 20 MHz clock" and we select the programmer "USBtinyISP"
    We connect the FabISP and the FTDI cable with our FabHello. In the event that our computer does not recognize the FabISP, we download the drivers from the following link:
    FabISP
    In the event that our computer does not recognize the FTDI cable,we download the drivers from the following link:
    FTDI
    For users of Windows 8, just use the option of automatic search for the drivers of the FTDI.
    We load the code
    Enjoy it!


    Once we have the Hello Board ready, we connect it with the sensor board, checking the appropriate pins from the ATTiny44, and the pins from the sensor card. We test the leds and buttons and they worked really well.

    You can Download the Source code here