Home About Projects Final Project
Martins Krebs - Fab Academy 2018

- MĀRTIŅŠ KRĒBS -

Fab Academy - 2018

Week 9: Embedded Programming

Group assignment

  • Compare the performance and development workflows for other architectures.
  • Individual assignment

  • Read a microcontroller data sheet
  • Program your board to do something, with as many different programming languages and programming environments as possible.
  • Optionally, experiment with other architectures.
  • Learning outcomes:

  • Identify relevant information in a microcontroller data sheet.
  • Implement programming protocols.
  • Have you:

  • Documented what you learned from reading a microcontroller datasheet.
  • What questions do you have? What would you like to learn more about?.
  • Programmed your board.
  • Described the programming process/es you used.
  • Included your code.



  • Getting Started

    Before starting to program the board you need some knownledge of the components and microprocessor (ATtiny44) you're using and you should read the datasheets to start to understand them. One of the most important things to know abut the microprocessors are what pins do what, and where you connect the components that you are working with.

  • Microprocessors are general purpose central processing units of computers and embedded systems. They do not have any RAM, ROM, Input/output ports, serial communication ports and timers in their chips.
  • Microcontroller: Unlike microprocessors, microcontrollers have on chip CPU, fixed amount of RAM, ROM, I/O ports, analog to digital converters, timers and counters. This fixed amount of on chips RAM, ROM and input/output ports makes microcontrollers suitable for low cost and low space applications.

  • VCC = voltage supply to the rest of the controller.

  • GND = the ground

  • Port A (0-7): Port A is a 8-bit bi-directional I/O port with internal pull-up resistors. The same as port B, they will source current if the internal resistors are acitivated.

  • MOSI: Master Data output. Slave Data input for SPI channel.
    MISO: Master Data input, the data comes into our microcontroller through this pin.


  • Port B [0-2]: B Ports are 4-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port B pins that are externally pulled low will source current if the pull-up resistors are activated.

  • RESET: A low level on pin for longer than the minimum pulse length will generate a reset.

    Pin 1: VCC. This is the power applied to the pin. Typically 5 volts DC. This pin has only one function. Red wire from power supply paths to here.

    Pin 2: PB0 - PORTB-0 can be connected to an external clock, crystal oscillator input, pin change interrupt, external clock input

    Pin 3: PB1 - PORTB-1 can be used for pin change interrupt, crystal oscillator output

    Pin 4: PB3 - PORTB-3 can be used for RESET

    Pin 5: PB2 - PORTB-2 can be used for an interrupt, clock out, pin change interrupt.

    Pin 6: PA2 - PORTA-2 can be used for analog digital converter input

    Pin 7: PA6 - PORTA-6 can be used for 3-wire or 2-wire communication input

    Pin 8: PA5 - PORTA-5 can be used for three-wire communication output

    Pin 9: PA4 - PORTA-4 can be used for analog digital converter input

    Pin 10: PA3 - PORTA-3 can be used for analog digital converter input, or timer/counter, or interrupt

    Pin 11: PA2 - PORTA-2 can be used for interrupt, analog digital converter input

    Pin 12: PA1 - PORTA-1 can be used for interrupt, analog digital converter input

    Pin 13: PA0 - PORTA-0 can be used for interrupt, analog digital converter input, and external analog reference

    Pin 14: GND - GROUND - 0 volts. Black wire from power supply goes here.


    The Rest of the Family

  • ATtiny24
  • 2 KB of flash 128 B of SRAM 128 B of EEPROM
    2 timers 4 touch channels 1 SPI 1 I2C 10-bit ADC with 8 channels 1 analog comparator temperature sensor 4 PWM channels watchdog

  • ATtiny44
  • Identical to the ATtiny24 except:

    4 KB of flash 256 B of SRAM 256 B of EEPROM
    6 touch channels

  • ATtiny84
  • Identical to the ATtiny24 except:

    8 KB of flash 512 B of SRAM 256 B of EEPROM

    Types of Memory

  • Registers: fastest memory. It stores data that needs to be accessed. 32 x 8-bit general purpose working registers with a single clock cycle access time. Every bit represents a state.
  • SRAM: bigger and faster than the registers. Is the place where the data in the programme is stored.
  • DRAM: bigger but smaller. Volatile memory.
  • EEPROM: this memory is not volatile, which means that the data will remain there even if we unplug it.
  • FLASH: also no volatile, bigger and slower than the EEPROM, where the programming is stored.
  • Fuses: memory where the configuration of the microcontroller is stored.


  • What's a Bit?

    ATtiny family is a series of 8-bit microcontrollers. This means that all the information has to be divided into 8 possible blocks called bits.



    The numeration of the bits in a byte starts from the left. Is important to know that for when you want to modify the registers or send the information.


  • SHIFT: moves the position of a factor to the right or left, the number of positions that the second number indicates.
    << 2 indicates that you are moving the '1' two positions to the left and insetring 0's (like making 1.0 into 100.0)
  • AND: you use AND when you want to set a bit to 0.
  • OR: in the opposite way you use OR to put a 1 in a bit.
  • XOR: invert a value. For example you can use it to change the state of a let from on to off.
  • You need to know what port you are going to modify and to create the mask that will be the 'function' that will change your value as desired.

    Example:
    You want to put a 0 in the 4th bit slot. To do this, you create a Mask that has the shift operator, indicating the 4 positions you want to move. Then, you invert the mask and you apply the and operator. The resulting value will be the one you were looking for, same bits as the initial one but with a 0 in the 4th position.

    * * * * *

    I used this as a great reference to figuring out different aspects of this week's assignment

    * * * * *




    Configuring the Pins

    Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in “Register Description” on page 66, the DDxn bits are accessed at the DDRx I/O address, the_PORTxn bits at the PORTx I/O address, and the PINxn bits at the PINx I/O address.

    The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin. If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin.

    The port pins are tri-stated when reset condition becomes active, even if no clocks are running. If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).


    Toggling the Pin

    Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port.

    What are the fuses?

    There are 3 bytes of permanent storage in the chip called 'fuse low byte', 'fuse high byte' and 'fuse extended byte'. These bytes are called fuses and can be reprogrammed as many times as you want and determines the behaviour of the chip. To do that, their value is not erased when the chip is powered off or reprogrammed.

    Each microchip has its own definition for the values that must have the fuses. In this tutorial, we are going to work with the fuses of the Atmel Attiny44A.



    ATtiny44a Fuses

    NameDescriptionDefault ValueByteBit rangeDatasheet page
    CKDIV8The prescaler feature can be used to change the power consumption when not high performance is required. This feature can be used with any clock source. The prescaler divides the clock frecuency by a defined factor that can be configured using the CLKPS(3...0) register (Datasheet - Table 6-11), being its default value "0011" (divistion factor of 8).0LOW(7)30
    CKOUTThis fuse allows us to use the chip clock as a clock source to drive other circuits. Once this bit is programmed, the PIN B2 will output the clock.1LOW(6)30
    SUTThis fuses determine the Start-up Times for the external clock selection. This time must be long enough to ensure that the MCU is kept in reset mode while the clock is changing (is not stable). Its default value defines the longest Start-up time.10LOW(5,4)26
    CKSELThe AMTEL Attiny44A can use several clock sources. By default, the clock used is the internal 8MHz oscillator. The available configurations can be found in the datasheet, in table 6-1.0010LOW(3...0)25
    RSTDISBLWhen programmed (0), the PB3, instead of being used as a reset pin, it can be used as an I/O pin.1HIGH(7)3
    DWENThis fuse allows the activation of the 'debug' mode. Once it is programmed, the RESET port is configured as a bidirectional I/O pin with pull-up enabled.1HIGH(6)150
    SPIENFuse that enables serial program and data downloading. Once unprogrammed this fuse, in order to reset its value, you will need a 12V programmer.0HIGH(5)159
    WDTONA watchdog timer is a mechanism to prevent closed loops and errors in the microchip. It consists on a decreasing counter. If it reaches 0, it generates a timeout signal that triggers an action to restore the system. To avoid this, the program must restart the timer before it times out.1HIGHbit range41
    EESAVEIf programmed (0) the EEPROM memory will be preserved through chip erase.1HIGH(4)159
    BODLEVELThis fuse controles the Brown-out detector. A Broun-out detector is a circuit that monitors the supply voltage while operating. It compares the supplied voltage to a fixed one. If the supplied voltage decreases blow a fixed one (VBOT-), the Brown-out reset is enabled until the voltage raises above a VBOT+. Then a timer starts; if it times out, the reset is disabled. By default, the Brown-out detector is disabled. In table 20-6 of the Datasheet all the modes are specified.111HIGH(2...0)159 & 176
    SELDPRGENThis fuse, if programmed, allows to the device the ability to upload a program to the MCU by itself.1EXTENDED(0)152

    [As seen, there is just one bit in the Extended byte of the fuses whose value will change the microchip behaviour. The bytes 7...1 should be set with a value of 1.]



    CKDIV8

    CKDIV8 is a prescaler feature to lower the microcontroller clock.

    The prescaler feature can be used to change the power consumption when not high performance is required, or to ensure the performance (and programability) at any legal VCC. This feature can be used with any clock source. The prescaler divides the clock frecuency by a defined division factor that can be configured using the CLKPS(3:0) register (Datasheet - Table 6-11), being its default value “0011” (divistion factor of 8).

    When CKDIV8 is unprogrammed, CLKPS bits will be reset to “0000”. If CKDIV8 is programmed, CLKPS bits are reset to “0011”, giving a division factor of eight at start up.

    ****The device is shipped with the CKDIV8 Fuse programmed****




    Programming

    Working in the Arduino Program, the first things we need to do are setting the parameters we are writing in to match the components that we have on our board.

  • First, under Tools, select the board [ATtiny24/44/84]




  • Second, select the processor that is on the board [ATtiny 44]


  • Third, select the clock that is on your board. Internal cloacks are the clocks inside of the ATtiny, external are seperate clocks that you have soldered and connected on the board. [20MHz Ext.]




  • Lastly, we need to pick the programmer we will use to communicate the program to the board [USBtinyISP]




  • Making the board say hello via Morse Code seems like the obvious thing to make the "Hello World" board do. So lets check out some Morse Code to see what we need to program:

    According to the chart, we need to make sure the delay's of the longer "blinks" are x3 as long as the shorter "blinks". I am also going to have a nice long pause in between each letter for clairty's sake.


    Here is the Final Code