Embedded Programming

Lesson 8

This week's assignment is twofold: first, we need to read a microcontroller datasheet and secondly we need to program the board we made in the electronics design lesson with as many different programmaing lanquages and programming environments.


Reading the Attiny44a datasheet

Lesson 8

The datasheet of the attiny44a was a tough read for someone unfamiliar with microcontrollers and after 100 pages, i decided to browse a little bit more instead of reading.


Blinking LED - Programming with WinAVR

Lesson 8

I downloaded the WinAVR software at this site and installed it on my computer. After downloading the hello.arduino.168.blink.c and hello.arduino.168.c.blink.make files, I changed them to hello.44.blink.c and hello.44.blink.c.make, started avrdude and checked on the datasheet on which I pin I had connected the LED light. This was in my case the PB3 pin, which I changed in the hello.44.blink.c.make file.

hello.44.blink.c.make

PROJECT=hello.44.blink
SOURCES=$(PROJECT).c
MMCU=attiny44
F_CPU = 20000000

CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)

$(PROJECT).hex: $(PROJECT).out
	avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\
   avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out

$(PROJECT).out: $(SOURCES)
	avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)

program-bsd: $(PROJECT).hex
	avrdude -p t44 -c bsd -U flash:w:$(PROJECT).c.hex

program-dasa: $(PROJECT).hex
	avrdude -p t44 -P /dev/ttyUSB0 -c dasa -U flash:w:$(PROJECT).c.hex

program-avrisp2: $(PROJECT).hex
	avrdude -p t44 -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex

program-avrisp2-fuses: $(PROJECT).hex
	avrdude -p t44 -P usb -c avrisp2 -U lfuse:w:0x7E:m

program-usbtiny: $(PROJECT).hex
	avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex

program-usbtiny-fuses: $(PROJECT).hex
	avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x7E:m

program-dragon: $(PROJECT).hex
	avrdude -p t44 -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex 

hello.44.blink.c

// test blinking LED
// Neil Gershenfeld
// 3/14/11
// (c) Massachusetts Institute of Technology 2011
// Permission granted for experimental and personal use;
// license for commercial sale available from MIT.

#include 
#include 

#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 led_delay() _delay_ms(100) // LED delay

#define led_port PORTB
#define led_direction DDRB
#define led_pin (1 << PB2)

int main(void) {
   //
   // main
   //
   // set clock divider to /1
   //
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   //
   // initialize LED pin
   //
   clear(led_port, led_pin);
   output(led_direction, led_pin);
   //
   // main loop
   //
   while (1) {
      set(led_port, led_pin);
      led_delay();
      clear(led_port, led_pin);
      led_delay();
      }
   }

For reference, i am using an usbtiny programmer connected to an usb, with a 20MHZ crystal and a attiny44 microcontroller. In the directory of these files i type in the command prompt, the following instructions in order to program my board.

make clean
make -f hello.44.blink.c.make
make -f hello.44.blink.c.make program-usbtiny-fuses
make -f hello.44.blink.c.make program-usbtiny

After i had finished executing these instructions, the light on my board started to blink:


LED and Button - Programming with WinAVR

Lesson 8

The second step was to add some complexity by adding a button to the programming. The Led now reacts by pressing on a button. I made two files led.c and led.c.make and in the dos prompt i typed in the directory of these files the following commands:

make clean
make -f led.c.make
make -f led.c.make program-usbtiny-fuses
make -f led.c.make program-usbtiny

This was the result:

Blinking SOS - Arduino

Lesson 8

In order to make let Arduino software talk with the attiny44, we need to download the attiny44 library and copy it to the arduino directory. We have to specify the board we are going to use, in the menu tools and set the programmer to USBtinyISP (in my case). Next, we have to set the serial port to the COMX which the computer is listening to. When you have written the program and you have the correct the settings,you can upload the data to the PCB board and watch the magic happen. Below, you find the code i used and after that the end result.

code

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int led = 8;
const int but = 2;
int buttonState = 0;
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  
  pinMode(led, OUTPUT); 
 pinMode(but, INPUT);  
}

// the loop routine runs over and over again forever:
void loop() {
  buttonState = digitalRead(but);
 
  if (buttonState == HIGH) {     
    // turn LED on:    
     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25);   // wait for a second
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25); 
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25); 
  // first sequence
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  //pause
  
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(75); 
     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(75); 
     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(75); 
// second sequence
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  //pause

 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25);   // wait for a second
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25); 
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25);
  //third sequence
  //pause
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(250);

  } 
  else {
    // turn LED off:
    digitalWrite(led, LOW); 
  }
 
  


}


Blinking SOS with Button - Arduino

Lesson 8

Now I followed that same steps as before, but i added the button function to the program.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int led = 8;
const int but = 7;
int buttonState = 0;
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  
  pinMode(led, OUTPUT); 
 pinMode(but, INPUT);  
 digitalWrite(but,HIGH);
}
// the loop routine runs over and over again forever:
void loop() {
  buttonState = digitalRead(but);
 
  if (buttonState == HIGH) {     
        
 // turn LED off:
    digitalWrite(led, LOW); 

  } 
  else {
    
    
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25);   // wait for a second
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25); 
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25); 
  // first sequence
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  //pause
  
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(75); 
     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(75); 
     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(75); 
// second sequence
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  //pause

 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25);   // wait for a second
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25); 
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(25);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(25);
  //third sequence
  //pause
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(250);
    
    
  }
 
  


}