EMBEDDED PROGRAMMING


Home About me

ASSIGNMENT


Read a microcontroller data sheet.

Program your board to do something, with as many different programming languages and programming environments as possible.

THE ATTINY44 DATASHEET


The Datasheet provides a great deal of information to operate to the attiny44.

Pin configuration is clearly specified in section 1.

datasheet

Section 10.1.1, has information about the port pin register bits.

There are three hardware register memory locations: DDRxn, PORTxn and PINxn

Lower case x is the number letter for the port and n represents the number.

DDRx pins set the direction of the pin. If logic one is written on DDRxn, Pxn is configured as an output pin. Conversely, if DDRxn is written logic 0, Pxn is configured as input.

One particularity, is that if the data direction register is configured for input and PORTxn is written logic one, an internal pull-up resistor in PORTxn is activated.

When DDRx bits are set to logic one (as output), the PORTx register will control if that pin is set to low or high.

Alternative functions of PortA and PortB pins are also specified.

datasheet

datasheet

Another useful table, from the vast amount of information, is the clock prescaler select:

datasheet

THE AVR TOOLCHAIN


The chain of tools used to program an AVR microcontroller is known as the toolchain. The process is simple, you write the source code in an editor, compile it to a specified architecture and upload the software to the microcontroller, using a flash programmer. In my case:

VIM -> C CODE -> avr-gcc COMPILER -> avrdude and Arduino-as-ISP
  

First, install the toolchain in debian-based distributions as follows:

jc@jc-X220:~$ sudo apt-get install avrdude avrdude-doc binutils-avr avr-libc gcc-avr gdb-avr
[sudo] password for jc: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
avr-libc is already the newest version (1:1.8.0+Atmel3.5.0-1).
avrdude is already the newest version (6.2-5).
binutils-avr is already the newest version (2.25+Atmel3.5.0-2).
binutils-avr set to manually installed.
gcc-avr is already the newest version (1:4.9.2+Atmel3.5.0-1).
The following packages were automatically installed and are no longer required:
  libfftw3-single3 libgeonames0 libpulsedsp libtimezonemap-data
  libtimezonemap1 libwebrtc-audio-processing-0 linux-headers-4.4.0-31
  linux-headers-4.4.0-31-generic linux-headers-4.4.0-47
  linux-headers-4.4.0-47-generic linux-headers-4.4.0-53
  linux-headers-4.4.0-53-generic linux-headers-4.4.0-57
  linux-headers-4.4.0-57-generic linux-headers-4.4.0-59
  linux-headers-4.4.0-59-generic linux-headers-4.4.0-62
  linux-headers-4.4.0-62-generic linux-image-4.4.0-31-generic
  linux-image-4.4.0-47-generic linux-image-4.4.0-53-generic
  linux-image-4.4.0-57-generic linux-image-4.4.0-59-generic
  linux-image-4.4.0-62-generic linux-image-extra-4.4.0-31-generic
  linux-image-extra-4.4.0-47-generic linux-image-extra-4.4.0-53-generic
  linux-image-extra-4.4.0-57-generic linux-image-extra-4.4.0-59-generic
  linux-image-extra-4.4.0-62-generic linux-tools-4.4.0-47
  linux-tools-4.4.0-47-generic linux-tools-4.4.0-53
  linux-tools-4.4.0-53-generic linux-tools-4.4.0-57
  linux-tools-4.4.0-57-generic linux-tools-4.4.0-59
  linux-tools-4.4.0-59-generic linux-tools-4.4.0-62
  linux-tools-4.4.0-62-generic pyside-tools python-ply python-pyside
  python-pyside.phonon python-pyside.qtdeclarative python-pyside.qthelp
  python-pyside.qtnetwork python-pyside.qtopengl python-pyside.qtscript
  python-pyside.qtsql python-pyside.qttest python-pyside.qtwebkit
  signon-keyring-extension
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  gdb-doc
The following NEW packages will be installed:
  avrdude-doc gdb-avr
0 upgraded, 2 newly installed, 0 to remove and 36 not upgraded.
Need to get 2,328 kB of archives.
After this operation, 6,185 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 avrdude-doc all 6.2-5 [623 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 gdb-avr amd64 7.7-2build1 [1,705 kB]
Fetched 2,328 kB in 1s (1,294 kB/s)  
Selecting previously unselected package avrdude-doc.
(Reading database ... 539617 files and directories currently installed.)
Preparing to unpack .../avrdude-doc_6.2-5_all.deb ...
Unpacking avrdude-doc (6.2-5) ...
Selecting previously unselected package gdb-avr.
Preparing to unpack .../gdb-avr_7.7-2build1_amd64.deb ...
Unpacking gdb-avr (7.7-2build1) ...
Processing triggers for doc-base (0.10.7) ...
Processing 1 added doc-base file...
Registering documents with scrollkeeper...
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up avrdude-doc (6.2-5) ...
Setting up gdb-avr (7.7-2build1) ...
jc@jc-X220:~$ 
  

The Arduino can be used as a hardware programmer without the Arduino IDE.

AVRDUDE is a tool to modify the ROM and EEPROM contents of an AVR microcontroller using the ISP (In-system programming). Arguments of AVRDUDE are:

Usage: avrdude [options]
Options:
  -p                 Required. Specify AVR device.
  -b               Override RS-232 baud rate.
  -B               Specify JTAG/STK500v2 bit clock period (us).
  -C            Specify location of configuration file.
  -c             Specify programmer type.
  -D                         Disable auto erase for flash memory
  -i                  ISP Clock Delay [in microseconds]
  -P                   Specify connection port.
  -F                         Override invalid signature check.
  -e                         Perform a chip erase.
  -O                         Perform RC oscillator calibration (see AVR053). 
  -U :r|w|v:[:format]
                             Memory operation specification.
                             Multiple -U options are allowed, each request
                             is performed in the order specified.
  -n                         Do not write anything to the device.
  -V                         Do not verify.
  -u                         Disable safemode, default when running from a script.
  -s                         Silent safemode operation, will not ask you if
                             fuses should be changed back.
  -t                         Enter terminal mode.
  -E [,] List programmer exit specifications.
  -x         Pass  to programmer.
  -y                         Count # erase cycles in EEPROM.
  -Y                 Initialize erase cycle # in EEPROM.
  -v                         Verbose output. -v -v for more.
  -q                         Quell progress output. -q -q for less.
  -l logfile                 Use logfile rather than stderr for diagnostics.
  -?                         Display this usage.

avrdude version 6.2, URL: 
  

Argument -p needs the user to specify which microcontroller is used, in my case the Attiny44. To get a list of supported microcontrollers by avrdude type:

avrdude -c avrisp 
  

It will generate an output of supported parts. NOTE:"..." is indicated to show the list was cut short..

Valid parts are:
  uc3a0512 = AT32UC3A0512
  c128     = AT90CAN128
  c32      = AT90CAN32
  c64      = AT90CAN64
  pwm2     = AT90PWM2
  pwm216   = AT90PWM216
  pwm2b    = AT90PWM2B
  pwm3     = AT90PWM3
  ...
  t24      = ATtiny24
  t25      = ATtiny25
  t26      = ATtiny26
  t261     = ATtiny261
  t4       = ATtiny4
  t40      = ATtiny40
  t4313    = ATtiny4313
  t43u     = ATtiny43u
  t44      = ATtiny44
  ...
  ucr2     = deprecated, use 'uc3a0512'
  

-b is the baud-rate, which is the rate at which information is transferred in a communication channel such as serial.

-c is used to specify the type of programmer to use. In order to use the arduino as a programmer in avrdude, use programmer type as avrisp with the following argument:

-c avrisp 
  

Similarly, to see a list of compatible programmers, type:

avrdude -c help 
  

Other important arguments of avrdude are:

-P is the port to talk to the programmer. This value differs from windows to linux.

-U <memtype>:r|w|v:<filename>[:format]: is how avrdude puts data onto the chip.

TESTING AVRDUDE


First, upload the ArduinoISP code from the Arduino IDE to the Arduino-UNO. Then, connect the Arduino to the hello board as follows:

pins

Test avrdude with the following command:

avrdude -p t44 -c avrisp -P /dev/ttyACM3 -b 19200 
  

The output should look similar to this:

jc@jc-X220:~$ avrdude -p t44 -c avrisp -P /dev/ttyACM3 -b 19200

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9207 (probably t44)

avrdude: safemode: Fuses OK (E:FF, H:DF, L:FE)

avrdude done.  Thank you.
  

BLINK PROGRAM IN C: PROGRAMMING


C program for the attiny44 is written using bitwise logic.

To blink one of my hello-board LEDs, I check the circuit schematic:

Green and yellow LEDs are connected to pins PA7 and PA2.

In the C code I must:

Set DDR as output
Write to PORT hardware register to assign logic 1 or 0 accordingly. 
  

Bitwise operators available are the following:

bit setting:    X |= (1 << i);
bit clearing:   X ~= (1 << i);
bit toggling:   X ^= (1 << i);
  

I write a simple LED blinking code in C:

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

int main (void) {   
    DDRA = 0xff;

    while(1) {
        PORTA |= (1<<2);
        _delay_ms(1000);
        PORTA ^= (1<<2);
        _delay_ms(1000);

    }
   return 0; 
}
  

First, Data direction register A is set for output. 0xff is hexadecimal for 0b11111111

Next, pin PA2 is set to high.

The OR operation between default value of PORTA 0b00000000 and the bit-mask of (1 << 2) (i.e. 0b00000010) will set the bit high where the green LED is located.

Next, there is a delay of 1000 miliseconds, followed by a bit toggle of PA2 to low, which will turn off the LED.

BLINK PROGRAM IN C: UPLOADING CODE TO HELLO-BOARD


AVR-GCC example projects and library references here are really useful.

After several trials and errors, the following commands worked to upload the program to my board:

avr-gcc -Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections 
-fdata-sections  -DF_CPU=20000000  -DBAUD=19200  -I. -I. -mmcu=attiny44 -c -o hello_attiny44.o hello_attiny44.c

avr-gcc -Wl,-Map,hello_attiny44.map  -Wl,--gc-sections  -mmcu=attiny44  hello_attiny44.o  -o hello_attiny44.elf 
avr-objcopy -j .text -j .data -O ihex hello_attiny44.elf hello_attiny44.hex

avrdude -c avrisp -p attiny44  -b 19200 -P /dev/ttyACM3 -U flash:w:hello_attiny44.hex
  

The code output was the following:

jc@jc-X220:~/Repositories/jcmgitlab.fab_academy2017/week08$ avr-gcc -Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  -ffunction-sections -fdata-sections  -DF_CPU=20000000  -DBAUD=19200  -I. -I./ -mmcu=attiny44  -c -o hello_attiny44.o hello_attiny44.c
jc@jc-X220:~/Repositories/jcmgitlab.fab_academy2017/week08$ avr-gcc -Wl,-Map,hello_attiny44.map  -Wl,--gc-sections  -mmcu=attiny44  hello_attiny44.o  -o hello_attiny44.elf
jc@jc-X220:~/Repositories/jcmgitlab.fab_academy2017/week08$ avr-objcopy -j .text -j .data -O ihex hello_attiny44.elf hello_attiny44.hex
jc@jc-X220:~/Repositories/jcmgitlab.fab_academy2017/week08$ avrdude -c avrisp -p attiny44  -b 19200 -P /dev/ttyACM3 -U flash:w:hello_attiny44.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9207 (probably t44)
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 "hello_attiny44.hex"
avrdude: input file hello_attiny44.hex auto detected as Intel Hex
avrdude: writing flash (108 bytes):

Writing | ################################################## | 100% 0.18s

avrdude: 108 bytes of flash written
avrdude: verifying flash memory against hello_attiny44.hex:
avrdude: load data flash data from input file hello_attiny44.hex:
avrdude: input file hello_attiny44.hex auto detected as Intel Hex
avrdude: input file hello_attiny44.hex contains 108 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.09s

avrdude: verifying ...
avrdude: 108 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:FE)

avrdude done.  Thank you.
  

The green LED was blinking, as expected.

PUSHBUTTON IN C: PROGRAMMING


Two of the pushbuttons in my hello-board are hooked with pull-up resistors of 10kΩ.

Button S2, hooked to pin PB2, will be programmed to light an LED when pressed.

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

int main(void) {
        //-- Initialise Data Direction Register --//
        DDRB &= ~(1 << DDB2);
        DDRA |= ((1 << DDA2) | (1 << DDA7));
        PORTB |= (1 << PB2); /* enable pull-up resistor of pin PB2 */
        PINB |= (1 << PB2); /*set pin PB2 for input mode */  
        PORTA |= ( (1 << PA2) | (1 << PA7) );

        while(1){
                if ( (PINB & (1 << PB2)) == 0 ) { /*check button in PB2 is pressed*/
                        PORTA |= (1<<PA2); /* Turn Green LED on*/       
                }
                else {
                        PORTA &= ~(1<<PA2);
                }               
        }
        return(0);
        
} 
  

First, data direction register for PB2 is set for input by clearing the bit or setting it to zero. As specified in the datasheet, PORTB PB2 bit can be set to allow the internal pull-up resistor, this step is optional as I already included a pull-up resistor in my design.

PUSHBUTTON IN C: MAKEFILE


Neil Gershenfeld provided us with a very useful example of a Makefile for an Echo program in C.

A makefile is a generic program that tells the compiler what program to use to compile and how to link the different files during compilation and uploadingto the AVR board.

I modify the makefile to make it easier to automate the toolchain process.

PROJECT = button_attiny44
MCU = attiny44
F_CPU = 20000000
BAUD = 19200
PROGRAMMER = avrisp
SOURCES = $(PROJECT).c
PORT = ttyACM3
ARGS1 =-Os -g -std=gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections
$(PROJECT).o: $(PROJECT).c
        avr-gcc $(ARGS1) -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I./ -mmcu=$(MCU)  -c -o $(PROJECT).o $(PROJECT).c

$(PROJECT).elf: $(PROJECT).o
        avr-gcc -Wl,-Map,$(PROJECT).map -Wl,--gc-sections -mmcu=$(MCU) $(PROJECT).o -o $(PROJECT).elf

$(PROJECT).hex: $(PROJECT).elf
        avr-objcopy -j .text -j .data -O ihex $(PROJECT).elf $(PROJECT).hex

all: $(PROJECT).hex
        avrdude -c $(PROGRAMMER) -p $(MCU) -b $(BAUD) -P /dev/$(PORT) -U lfuse:w:0x5E:m
        avrdude -c $(PROGRAMMER) -p $(MCU) -b $(BAUD) -P /dev/$(PORT) -U flash:w:$(PROJECT).hex
clean:
        rm $(PROJECT).o $(PROJECT).elf $(PROJECT).hex $(PROJECT).map
  

Variables PROJECT (name of the file to compile), MCU (microcontroller), F_CPU(frequency), BAUD, PROGRAMMER, PORT AND ARGS1(arguments to pass to avr-gcc for compilation) are passed through to the substitution function$(). The format of a makefile is:

TARGET: DEPENDENCIES
	RECIPE 
  

Further details can be found here.

PUSHBUTTON IN C: TESTING


Run the following command in the shell: make all to compile and upload the code to the microcontroller.

Ouput is the following:

jc@jc-X220:~/Repositories/jcmgitlab.fab_academy2017/week08/button_c$ make all
avrdude -c avrisp -p attiny44 -b 19200 -P /dev/ttyACM3  -U lfuse:w:0x5E:m

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9207 (probably t44)
avrdude: reading input file "0x5E"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.01s

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.01s

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:5E)

avrdude done.  Thank you.

avrdude -c avrisp -p attiny44 -b 19200 -P /dev/ttyACM3  -U flash:w:button_attiny44.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9207 (probably t44)
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 "button_attiny44.hex"
avrdude: input file button_attiny44.hex auto detected as Intel Hex
avrdude: writing flash (86 bytes):

Writing | ################################################## | 100% 0.18s

avrdude: 86 bytes of flash written
avrdude: verifying flash memory against button_attiny44.hex:
avrdude: load data flash data from input file button_attiny44.hex:
avrdude: input file button_attiny44.hex auto detected as Intel Hex
avrdude: input file button_attiny44.hex contains 86 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.09s

avrdude: verifying ...
avrdude: 86 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:5E)

avrdude done.  Thank you.

jc@jc-X220:~/Repositories/jcmgitlab.fab_academy2017/week08/button_c$  
  

Program worked as expected.

SERIAL IN C: PROGRAMMING


I decided to implement a serial communication program using Neil Gershenfeld's hello.ftdi.44.echo.c example.

The program sends a smiley ":P" to the computer by serial whenever the button S2 is pressed. The button is debounced and the green LED also lights up when pressed.

I included Neil Gershenfeld's file as a header.

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "hello.ftdi.44.echo.h"

#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 8.5 // bit delay for 115200 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define char_delay() _delay_ms(10) // char delay

#define serial_port PORTA
#define serial_direction DDRA
#define serial_pins PINA
#define serial_pin_in (1 << PA0)
#define serial_pin_out (1 << PA1)

#define max_buffer 25

int main(void) {
   //
   // main
   //
   static char chr;
   //static char buffer[max_buffer] = {0};
   //static int index;
   //
   // set clock divider to /1
   //
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   //
   // initialize output pins
   //
   set(serial_port, serial_pin_out);
   output(serial_direction, serial_pin_out);
   //
   // main loop

   //button data direction registers
   DDRB &= ~(1 << DDB2);
   //LEDs data direction registers
   DDRA |= ((1 << DDA2) | (1 << DDA7));
   //button as input
   PINB |= (1 << PB2);
   //Switch off LEDs
   PORTA &= ~( (1 << PA2) | (1 << PA7) );
   //waiting for pressing of button
   put_string(&serial_port, serial_pin_out, "waiting for button");

	while (1)
	{
		if ( (PINB & (1 << PB2)) == 0 ) {
        	_delay_ms(150);
        	
				if( (PINB & (1 << PB2)) == 0) {
        			PORTA |= (1 << PA2); //turn on green LED
        			put_string(&serial_port, serial_pin_out, ":P");

				}
		} 
		    else {
            PORTA &= ~(1 << PA2);
          
		}
      
    }
   }
  

The program uploaded successfully to the microcontroller. This time I used the Arduino IDE to upload as a programmer.

avrdude: Version 6.3, compiled on Sep 12 2016 at 15:21:49
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/jc/Downloads/1/arduino-1.6.13/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/jc/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyACM3
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATtiny44
         Chip Erase delay              : 4500 us
         PAGEL                         : P00
         BS2                           : P00
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65     6     4    0 no        256    4      0  4000  4500 0xff 0xff
           flash         65     6    32    0 yes      4096   64     64  4500  4500 0xff 0xff
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9207 (probably t44)
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 "/tmp/arduino_build_226854/serial_c.ino.hex"
avrdude: writing flash (412 bytes):

Writing | ################################################## | 100% 0.63s

avrdude: 412 bytes of flash written
avrdude: verifying flash memory against /tmp/arduino_build_226854/serial_c.ino.hex:
avrdude: load data flash data from input file /tmp/arduino_build_226854/serial_c.ino.hex:
avrdude: input file /tmp/arduino_build_226854/serial_c.ino.hex contains 412 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.31s

avrdude: verifying ...
avrdude: 412 bytes of flash verified

avrdude done.  Thank you. 
  

SERIAL IN C: TESTING


I didnt have a FTDI cable. So I used the Arduino UNO as a FTDI connector.

This is how I did it:

I used Gtkterm to communicate with my board over serial.

It needs the port and baud rate to work.

Here is the program in action:

SERIAL IN PYTHON: CREATING A GIMP LAUNCHER


I wanted to create a launcher for a linux application, so I chose gimp.

I used python to create the script.

Install python's serial library with this command:

sudo apt-get install python-setuptools 
  

The code is simple, it uses two libraries: (1) the serial library to wait for the microcontroller to send information and (2) the subprocess library, which can call commands on bash from a python script.

The code is the following:

# Set baud and port
SERIAL_PORT = "/dev/ttyACM3"
BAUD_RATE = 115200

import serial
import subprocess

sp = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout = 5)
sp.flush()

print ("GIMP launcher")

while(1):
    response = sp.read(1)       # get one byte
    if response == "O":
        print "Waiting for a button press."
    elif response == ":":       # if received :, execute gimp command in bash
        print "Update!!"
        res = subprocess.check_output(["gimp"])
    else:
        print "Still waiting."
  

When the python script receives the first character of the button press, which is :, it will launch gimp by calling gimp from the shell in linux.

Execute the python script by typing:

python launcher.py
  

Program in action:

LESSONS LEARNED


FILES


Blink program in C

Makefile for blink program

Button pressing in C

Makefile for button pressing

serial_attiny44.c

hello.ftdi.44.echo.h

hello.ftdi.44.echo.c

Gimp launcher in Python

RESOURCES


Arduino pins

AVR Toolchain


The content of this page is licensed under Attribution-NonCommercial 4.0 International CC BY-NC 4.0. Any source code, if otherwise unspecified, is licensed under the MIT License