Skip to content

9. Embedded programming

This week I worked on browsing the data sheet for your microcontroller and use my programmer to program my board to do something.

Group assignment

This week of group assignment, We try to compare the performance and development workflows for other architectures. Click HERE to see more detail of the group assignment.

Individual assignment

For the individual, I’ve used the board I did on week 7, Blink4Terry

Blink4Terry

Specification
Microcontroller Atmel SAMD11C14A
Clock Speed 48MHz
Flash Memory 16KB
SRAM 4KB
Operating Voltage 1.62V – 3.63V
Input Voltage 3.3 - 5V
Dimensions 250 x 320 x 100 mm
Source Schematic

Atmel SAMD11C14A Datasheet

In the datasheet, I’ve read start from the ‘Table of Contents’. And browse the overview.

It will bring you to that page via clicking on the page number in the Table of Contents

Some contents are important, like the microchip pinout.

By reading the datasheet of my microcontroller, I have learned that: - Some microcontroller does not have USB interface, like the main chip on micro:bit, it need to add another chip for doing the USB interface. - There are diffent kind of pin type and amount, need to think carefully while choosing the chip to use - Memory size limited the size of the program, choose a bigger flash size if your program size is large.


Use my Programmer to Program

I connected my own programmer with the blink board and try to program it.

connect_5.jpg

Downloaded the edbg.exe and sam_ba_Generic_D11C14A_SAMD11C14A.bin file, then I’ve install the .bin file into the chip by using this command in terminal.

edbg-windows-r29.exe -t samd11 -bpv -f sam_ba_Generic_D11C14A_SAMD11C14A.bin

program_6.jpg


Arduino IDE

Pros Cons
A lot of sample codes and easy to modify C language only
Support board that is not Arudion board The code highlighting sometimes go wrongly

After then, I’ve went into the preferences and add a URL to letting you to install the board data and program with it.

https://www.mattairtech.com/software/arduino/package_MattairTech_index.json

Program_preferences.jpg

Then, in the ‘Boards Manager’, I’ve installed the package which included SAM D11C core board for Arduino.

Program_boards_manager.jpg

Then, select the D11C14A and started to program it :)

Program_select_board.jpg

I made a simple light on and off code:

const int button = 2;
const int led =  4;

int pressed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}
void loop() {
  pressed = digitalRead(button);
  if (pressed == HIGH) {
    digitalWrite(led, LOW);
  } else {
    digitalWrite(led, HIGH);
  }
  delay(100);
}

Result:

Advance (for loop)

But the light on and off does not satisfy me, so I made a breathing and SOS light by using the ‘For’ statement according to For statement - zh-tw (Microsoft Docs)

const int button = 2;
const int led =  4;

int pressed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}
void loop() {
  pressed = digitalRead(button);
  if (pressed == LOW) { 
    // if pressed = true
    // SOS ···---··· 
    // Rico recommended to use Dot() Dash() to replace analogWrite()
    analogWrite(led, 255);
    delay(100);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(100);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(100);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(500);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(500);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(500);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(100);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(100);
    analogWrite(led, 0);
    delay(100);
    analogWrite(led, 255);
    delay(100);
    analogWrite(led, 0);
    delay(1000);
  } else { 
    // if pressed = false
    for (int i = 0; i < 255; i++) {
      analogWrite(led, i);
      delay(1);
    }
    for (int i = 255; i > 0; i--) {
      analogWrite(led, i);
      delay(1);
    }
  }
}

Result:


Improved version

Thanks to Rico and Zack. They recommended and give an advice of letting my code shorter.

Rico said, something like this to shorten my code.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  //Morse Code for SOS
  dot(); dot(); dot();
  delay(500);
  dash(); dash(); dash();
  delay(500);
  dot(); dot(); dot();
  delay(2000);
}

void dash(){
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000); 
}

void dot(){
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
  delay(200); 
}

And Zack gave me a link to his board’s code, Click Here to visit.

Then I modify it to this ⬇️

// Pin config
const int button = 2;
const int led =  4;

// values for morse code, https://morsecode.world/international/timing.html
const int unit = 100;   // base time unit for morse code, smaller is faster
const int dot = unit; // same as dit (.)
const int dash = unit * 3;  // same as dah (-)
const int intra_char_space = unit;  // the gap between dits and dahs within a character
const int inter_char_space = unit * 3;  // the gap between the characters of a word
const int word_space = unit * 7;  // the gap between two words

// status values of button
int pressed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

// functions of breathing light

void breathing() {
  for (int i = 0; i < 255; i++) {
    analogWrite(led, i);
    delay(1);
  }
  for (int i = 255; i > 0; i--) {
    analogWrite(led, i);
    delay(1);
  }
}

// functions of morse code

// "_" is used for spaces in between words. Every char already ends with a inter_char_space of 3 time units.
void _() {
  delay(word_space - inter_char_space);
}

void s() {
  // ...
  analogWrite(led, 255);
  delay(dot);
  analogWrite(led, 0);
  delay(intra_char_space);
  analogWrite(led, 255);
  delay(dot);
  analogWrite(led, 0);
  delay(intra_char_space);
  analogWrite(led, 255);
  delay(dot);
  analogWrite(led, 0);
  delay(inter_char_space);
}

void o() {
  // ---
  analogWrite(led, 255);
  delay(dash);
  analogWrite(led, 0);
  delay(intra_char_space);
  analogWrite(led, 255);
  delay(dash);
  analogWrite(led, 0);
  delay(intra_char_space);
  analogWrite(led, 255);
  delay(dash);
  analogWrite(led, 0);
  delay(inter_char_space);
}

void loop() {
  pressed = digitalRead(button);
  if (pressed == LOW) {
    // if pressed = true
    // SOS ···---···
    s();
    o();
    s();
    _();
  } else {
    // if pressed = false
    breathing();
    }
  }

Result:


Downloads

Latest Blink4Terry code - .ino

Blink4Terry (New) f3z design file - Google Drive

Blink4Terry (New) trace & outline png file - .zip


Last update: June 28, 2022