8. Embedded programming

What I’ve done this week

  • read the datasheet for microcontroller
  • write the program on the board which I made on week06

Weekly Assignment Requirement

Group assignment

  • Compare the performance and development workflows for different microcontroller families
  • Document your work (in a group or individually)

Individual assignments

  • Read the datasheet for the microcontroller you are programming
  • Program the board you have made to do something, with as many different programming languages and programming environments as possible

Description of Assignment Work

This week, I tried embedded programming using the MCU called Attiny44. read the datasheet of it at first. then, programmed on it actually.

Reading the Data sheet

Datasheet describes all the features of the electronic parts. need to read and understand it when we design the electronic circuit boards.
I tried to read through Attiny44’s datasheet firstly but the amount of its pages was too much (238 pages). So I picked up some notable sections which seemed to help to design the PCB board this time.

P.1 Feature

I could know the fundamental characteristic of the MCU here. Especially, I checked the section about the voltage carefully.

P.2 PIn configuration

Pin config. would be quite important when I design the board. Some of them had the unique function like for networking communication, controlling clocks, and so on.

P.4 block diagram

The functions of the MCU were shown intelligibly in the diagram. It also indicated how the sections related each other. this gave me a comprehensive understanding of the system of MCU.

P.54 I/O Ports

The diagram was about the architecture of a single port in detail. It helped understand how the MCU worked microscopically.

P.55 Configuring the Pin

This section explained pin setting, Each pin could be set by writing logic one and logic zero.

P.67 Register Description


Each resister was described about their address and default value of them in order to use them for programming.

Trying embedded programming

After reading the datasheet, I tried to program on Attiny44 using Arduino IDE.

preparation of Arduino IDE

Firstly, set Arduino IDE for programming on Attiny44. (referred to this website}



select preferences from Arduino menu. press the window icon at “Additional Boards Manager URLs:”



put the URL below into the input field.
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json


Attiny44 by David A. Mellis was shown up on board manager and installed it.


could find Attiny44 on the board list.

programming from Arduino IDE

loaded a sample program of Arduino IDE (Button program) and changed it slightly for my test.

the points I changed in the program were…

1. PIN numbers
Pin number of Attiny44 to program could be checked from the image.

credit

const int buttonPin = 2;

const int buttonPin = 3;


const int ledPin = 13;

const int ledPin = 7;


2. correct the architecture of resistor for the button from Pull down to Pull up
if (buttonState == HIGH)

if (buttonState == LOW)


3. behavior of the LED
digitalWrite(ledPin, HIGH);

digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);


here’s the changed program.


Worked!
While pressing the button, LED was blinking every 0.1 second.

C language programming from terminal

Secondly, tried to write program via terminal using the program written by c language.

I just started to use the 2 codes created by Kae Nagano last year. and added my learning point as comments to the code.

[code 1] The code which made LED blink

The structure of the program by C language included setup part and loop part in the main function though I would divide them if I write it in Arduino.

I did pin setting by bit shifts basically. For example, DDRA |= (1 << PA7) means setting PIN “PA7” as 1, DDRA &= ~(1 << PA7) means setting PIN “PA7” as 0.

//----------------------------------------------------------------------------------------------------
//  LED1.c       Led blink test
//----------------------------------------------------------------------------------------------------
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#define led_port PORTA
#define led_pins PINA
#define led_pin_out (1 << PA7)
#define led_direction DDRA


//----------------------------------------------------------------------------------------------------
//      main
//----------------------------------------------------------------------------------------------------
void main(void) {

   //--------  set clock divider to /1 ---------------------------

   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);

   //-------- initialize output pins -----------------------------

    led_port |= led_pin_out;    // set led port. use 1 as high and 0 as low
    led_direction |= led_pin_out;   // set PA7 as output

   //-------- Loop  -----------------------------------------------

    while(1){
        led_port |= led_pin_out;    // led on
        _delay_ms(1000);    //wait for 1000 ms
        led_port &= (~led_pin_out); // led off
      _delay_ms(1000);  //wait for 1000 ms
    }
}

worked.

[code 2] The program using tactile switch which included two functions.

The one made the LED turned ON while pressing the tactile switch.
The other used the tactile switch as a toggle controlling LED ON/OFF by a clicking.
Pick the one of them when executing the program in advance.

When I used the input device, I didn’t need to set data direction register because it had already been set as an output by default.
Before trying this code, I checked how to use toggle feature on the datasheet.


If I set a logic 1 to PINA (or PINB), it would work as a toggle.

//----------------------------------------------------------------------------------------------------
//  LED2.c       Led blink test
//----------------------------------------------------------------------------------------------------
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#define led_port PORTA
#define led_pin_out (1 << PA7)
#define led_direction DDRA
#define led_togglePins PINA //setting pin for the toggle function

//#define sw_port PORTA
#define sw_pins PINA
#define sw_pin  (1 << PA3)  //setting pin for the tactile switch


//----------------------------------------------------------------------------------------------------
//      main
//----------------------------------------------------------------------------------------------------
void main(void) {

   //--------  set clock divider to /1 ---------------------------

   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);

   //-------- initialize output pins -----------------------------

    led_port |= led_pin_out;
    led_direction |= led_pin_out;

   //-------- Loop  -----------------------------------------------

    while(1){
//      chkSw(); //sw read 1
        chkSw2(); //sw read 2

    }
}

//----------------------------------------------------------------------------------------------------
//      sw read 1
//----------------------------------------------------------------------------------------------------
void chkSw(void){

    if(sw_pins & sw_pin){
                led_port &= ~led_pin_out;   // led off by pull-up resister
    }else{
                led_port |= led_pin_out;    // led on
    }
}

//----------------------------------------------------------------------------------------------------
//      sw read 2
//----------------------------------------------------------------------------------------------------
void chkSw2(void){

    if(sw_pins & sw_pin){
             //   led_port &= ~led_pin_out;     // keeping stable status while not pressing the switch
    }else{  //if the switch was pressed, following command would be executed.
         _delay_ms(10);     //wait for 10 ms to avoid chattering
        if(sw_pins & sw_pin){
                led_togglePins |= led_pin_out;      // switching toggle
        }
    }
}

function sw read 1 worked.


function sw read 2 worked.

Lastly, I adding the original loop part and function part to the code I tested by myself.

[code 3] Using switch statement to change LED status in 3 patterns as turning ON, blinking, and tuning OFF.

//----------------------------------------------------------------------------------------------------
//  LED4.c       Led blink test
//----------------------------------------------------------------------------------------------------
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#define led_port PORTA
#define led_pin_out (1 << PA7)
#define led_direction DDRA
#define led_togglePins PINA

//#define sw_port PORTA
#define sw_pins PINA
#define sw_pin  (1 << PA3)

int count = 0;


//----------------------------------------------------------------------------------------------------
//      main
//----------------------------------------------------------------------------------------------------
void main(void) {

   //--------  set clock divider to /1 ---------------------------

   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);

   //-------- initialize output pins -----------------------------

    led_port |= ~(led_pin_out);     // set led port
    led_direction |= led_pin_out;   // set led port direction

   //-------- Loop  -----------------------------------------------

    while(1){
        LEDSW();
        switch(count){
        case 1:
            while(sw_pins & sw_pin){
                    led_port |= led_pin_out; 
                    _delay_ms(2000);
                    led_port &= ~(led_pin_out); 
                    _delay_ms(2000);
                    }
            break;

        case 2:
            while(sw_pins & sw_pin){
                led_port &= ~(led_pin_out);
            }
            break;

        default:
            led_port |= led_pin_out;  

    }
    }
}


 //-------- Function  -----------------------------------------------
int LEDSW(){
    if(sw_pins & sw_pin){

    }
    else {
        _delay_ms(10); 
        if(sw_pins & sw_pin){
            if(count < 2){
                count++;
                return count;
                } 
            else {
                count = 0;
                return count;
                }

        }
    }
} 



Writing program on the board
Using USBtinyISP for loading the program and FTDI cable for power supply.



created a make file for my program.

run three codes for loading the program as usual.

make -f Led4.c.make
make -f Led4.c.make program-usbtiny-fuses
make -f Led4.c.make program-usbtiny



Final Result

worked! but not working smoothly when it was switching from blinking status to off status.

At the end, my mentor told me that it would be nicer to change the timing to check the status for switch statement like checking it every 0.1 sec while blinking LED.

Group Assignment

For the group assignment, I played around with ESP8266.
Firstly, I just programed on it from ArduinoIDE.
Secondly, I tried to use MicroPython which is compiler and also a runtime that enabled to run the python on the MCU. I used it to test program interactively (REPL prompt) on terminal. and also I tried to transfer a python file to the MCU and executed it.
Please see the link below.

Kamakura Group assignment week08

Description of Important Weekly Learning Outcome

I had never faced to datasheet before so I was really surprised that it consisted of more than 200 pages for describing a small electronic component which was less than 1cm. I learned that I just needed to read necessary part for my own work. Though it was quite hard to find where it was described, I would like to get used do it in order to work smoothly. And also I tried to write C language for MCU at first time. It was more difficult than doing same thing with Arduino as usual. I wanted to master it but I considered not to do so in the term of FabAcademy because I only had very limited time. I supposed that I should use Arduino more from the point of view of my ability.

Led1.c
Led1.c.make
Led2.c
Led2.c.make
Led4.c
Led4.c.make