Skip to content

8. Embedded programming

Datasheet

Navigated through the Attiny44 Datasheet

Very packed honostly , learned that I could search for what I need to do , and which register to write in .

for example to program a certain pin as input , I need to write 0 in the specified register in the bit corresponding to pin number .

Also through datasheet , I found the pins’ names to be used in Arduino IDE .

Programming

In this week , I used the board made on week 6

I changed the code to get the led to light when I press the button .

C language

As we learned in the Group Assignment in this week

Programming in C has highest response

My code in C language :

#include <avr/io.h>

#define output(directions,pin) (direction |= 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

// The Button is connected to PA7 & Led to PB2

#define Led_port PORTB
#define Led_direction DDRB
#define Led_pin (1 << PB2)

#define Button_port PORTA
#define Button_direction DDRA
#define Button_pins PINA
#define Button_pin (1 << PA7)

void setup() {
  // set Led as output
  output(Led_direction,Led_pin);
  clear(Led_port,Led_pin);
  // No need to set Button as input , it's already set be default
}

void loop(){
  //Read Button and toggle Led based on Button input
  if (pin_test(Button_pins,Button_pin))
  set(Led_port,Led_pin);
  else
clear(Led_port,Led_pin);
}

After Uploading using Arduino IDE , The board performed it’s function of turning Led on when Button is pressed .

Yet , I had a weird error , As the led would have a fading light when the botton is not pressed .

After asking my instructor , it turned out that I didn’t program a pull up resistance for the button ,

So when it’s not pressed it’s still getting a signal , weak one , so the led lights weakly .

All I had to do is set zero for the Button pin , by adding this line to the setup

clear(Button_port,Button_pin);

Thankfully , it worked correctly

Arduino C

a bit slower than programming in C

That was my code for the same function on the same board

#define Button 3 // connected to PA7
#define Led 2 // connected to PB2

void setup() {
pinMode(Led,OUTPUT);
pinMode(Button,INPUT);
}

void loop{
if(digitalRead(Button))
digitalWrite(Led,HIGH);
elsedigitalWrite(Led,LOW);
}

Noticable that it’a much shorter and easier , yet that indicates many hidden libraries and functions .

That’s why it’s response time is larger .


Last update: May 29, 2021