Week09. Embedded programming

This week do the following
・ Try group assignment on how to write programs to various circuits
・ I write a program to the circuit I created by myself

Group Assignment

The content of our group assignment is here
Week09 Group Assignment

Impressions

Writing compiled code to a chip is the same, but the flow to get there is quite different.
I felt it was better to find something that suits me.
For Avr microcontrollers, I think it is better to use Atmel Studio or Arduino IDE.

Embedded programming

Read the data sheet

DataSheet

Information Needed to Use ATtiny Pins.

Registers

  • PORT
    • Port Data Register
    • There are PORTA and PORTB
    • In ATtiny44, PORTA manages 8 pins, but PORTB manages only 4 pins
  • DDR
    • Data Direction Register
    • DDRA and DDRB, corresponding to PORTA and PORTB respectively
    • Define if each pin is an input or an output.
      • Input when 0 is set,Output when 1 is set.
  • PIN
    • Port Input Pins
    • PINA and PINB, corresponding to PORTA and PORTB respectively
    • When set the input in DDR, the information input from the sensor etc. is taken into the corresponding PIN

1 Coding

Create a program to write to FabISP.

The first program created below is a program that blinks at 1-second intervals.

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

int main(){
    DDRA = 0b10000000; //port settings
    while(1){
        PORTA = 0b10000000;
        _delay_ms(1000);
        PORTA = 0b00000000;
        _delay_ms(1000);
    }    
}

However, the blink speed is obviously slow.
I checked the switching time with an oscilloscope.
It turns out that it has been switched every 8 seconds

Oscilloscope

When I read the data sheet, I have the following description in the item of Fuse Byte.

DataSheet LowFuseByte

The contents set in make fuse used in Week 07 were as follows.

avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m

Low Fuse Byte is 0x5E
It will be as follows when converted to bit

Fuse Low Byte Bit No Set Value
CKDIV8 7 0
CKOUT 6 1
SUT1 5 0
SUT0 4 1
CKSEL3 3 1
CKSEL2 2 1
CKSEL1 1 1
CKSEL0 0 0

Of these, the 6th and 7th bits are related to the clock frequency.
The seventh bit is set to divide the frequency by 8, and it is ON at 0.

Changed the setting of Fuse and changed it to the following value.

avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0xÐE:m

It will be as follows when converted to bit

Fuse Low Byte Bit No Set Value
CKDIV8 7 1
CKOUT 6 1
SUT1 5 0
SUT0 4 1
CKSEL3 3 1
CKSEL2 2 1
CKSEL1 1 1
CKSEL0 0 0

Rewrite and check the interval with an oscilloscope.
It is switched at one second intervals.

Oscilloscope

1-2 Switch on the LED

Second, I created a program that when the light is on, press the switch to turn off the light.
The switch of the circuit created in the 7th week has a pull-up resistor, so when the switch is not pressed, it becomes HIGH.

I created the following code.

#include <avr/io.h>

int main(){
    DDRA = 0b10000000; // portA setting
    DDRB = 0b00001011; // protB setting

    while(1){
        if(bit_is_clear(PINB,PINB2)){
            PORTA = 0b10000000;
        }else{
            PORTA = 0b00000000;
        }
    }    
}

SwitchOff
SwitchOn

It was a code that disappears when the switch is pressed.

Coding (Arduino)

Create a program using Arduino.
The code below blinks the LED every second.

#define LEDPIN 10

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LEDPIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LEDPIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LEDPIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Can use functions defined by Arduino
So it’s easy to see what it trying to process

Files