9. Embedded programming

Group Assignment page

Individual Assigment

Reading a Datasheet

I choose to read the Atmega 328p datasheet Arduino microcontroller because I was told it was going to be the microcontroller that I was going to use for my final project.

“The ATmega328P achieves throughputs approaching 1MIPS per MHz allowing the system designer to optimize power consumption versus processing speed.”p6

About Pins

I learned the diference between a Analog port and a Digital one. An analog pin reads all the values between 0V and 5V. A digital pin reads only 0V or 5V, it reads only the binary language.

  • VCC: Digital supply voltage.
  • GND: ground
  • Port B: is an 8-bit bi-directional I/O
  • Port C: is a 7-bit bi-directional I/O port
  • PC6/RESET: If the RSTDISBL Fuse is programmed, PC6 is used as an I/O pin.
  • PortD: is an 8-bit bi-directional I/O
  • AV: is the supply voltage pin for the A/D Converter
  • AREF: AREF is the analog reference pin for the A/D Converter.
  • ADC: serve as analog inputs to the A/D converter. These pins are powered from the analog supply and serve as - - 10-bit ADC channels.
  • Analog to digital converter: Is a pin for Inputs, it can be use for both analog and digital signals.
  • Instruction set summary: specify the number of clocks
  • Electrical characteristics: Shows the minimum and maximum voltages and temperatures that the microcontroller use and support.

About architecture

“In order to maximize performance and parallelism, the AVR uses a harvard architecture – with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program memory. This concept enables instructions to be executed in every clock cycle. The program memory is in-system reprogrammable flash memory.” p9

I was told by my tutor that the architecture is the way in which different microcontrollers’ functions are located into. Now, reading the datasheet, I undrestand also that that architecture define the way in which information is processed between the program memory and buses.

In harvard architecture for example; the program, who is executed by the program memory, and data are processed separately. This means that the program and data are stored in separate memory spaces which are accessible simultaneously. Therefore, while one instruction is being executed, the next one can be fetched. This is partly how one execution per clock cycle can be achieved. With other microcontroller architectures, there is only 1 way to access memory, so executions and program instruction access must be done alternately. That means a lower information processing.

About program instructions

“Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program memory. This concept enables instructions to be executed in every clock cycle. “p9

About data processing

“The fast-access register file contains 32  8-bit general purpose working registers with a single clock cycle access time. This allows single-cycle arithmetic logic unit (ALU) operation.”p9

Characteristics

● Temperature range: Automotive temperature range: –40°C to +125°C

This information is important to me because I need to expose the micro controller to hot temperatures.

Installing Attiny support on Arduino

I started by setting Arduino to make possible the comunication between my board and PC. I follow a tutorial to install Attiny support on Arduino.

Once Attiny support was installed I verified the correct comunication between the board and the computer tiping the following code in the Git bash terminal:

averdude -c usbtiny -p t44

Writing a code on Arduino

First of all is important to know the pins activating numbers. For that I used the microcontroller pinout.

LED’s exercise

I started writing a simple code to make my board’s led turn on and turn off.

void setup() 
{
  pinMode(2,OUTPUT); //CONFIGURACION DEL PIN
  digitalWrite(2,LOW); //INICIA EL PIN EN ESTADO BAJO 0 LOGICO 0 VOLTIOS
}
void loop() 
{
  digitalWrite(2,HIGH);
  delay(1000); //esto son 1000 milisegundos = 1 segundo
  digitalWrite(2,LOW);
  delay(1000);
}

The first thing to do after writing a code is to verify it with the icon circled in red. If the code is correctely compilated we can charge into the microcontroller’s board with the arrow icon behind.

After that, if all is right, the board should be runing the code and reacting to it. In this case, The led is successfully turning on and off with a second delay.

Arduino have some coding examples according to diferent inputs.

Download the code here

Button exercise

I opened a button example code and I make the exercise following it and replacing the pins’ numbers.

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(8, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(2, INPUT);
}

void loop() 
  {
    buttonState = digitalRead(8);   
    if (buttonState = HIGH)

    // turn LED on:     
    digitalWrite(2, HIGH);

      else

        // turn LED off

        digitalWrite(2,LOW);

    }

There were some errors in this code so I corrected it with one of my tutor’s help. This time I used an other pinout image to set my output and input pins.

After changing the pin’s number the simulation keeped not working so we identified an other error on the void loop‘s part of the code. Instead writing “if (buttonState == HIGH)” I was missing one “=”.

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(A2, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(8, INPUT);
}

void loop() 
  {
    buttonState = digitalRead(8);   //asignamos un valor
    if (buttonState == HIGH) //comparamos el estado del boton con el valor HIGH

    // turn LED on:     
    digitalWrite(A2, HIGH);

      else

        // turn LED off

        digitalWrite(A2,LOW);
    }

I couldn’t had the button and the termistor for my board so I maked this exercise on Proteus, a PCB simulator.

Proteus works close like Eagle, so to select components I just followed those steps.

To load my code into the simulator’s board I copied the .hex URL generated after the Arduino’s compilation in the micro controller’s settings.

It’s important to change those settings just as there are in the image below.

After that and after had corrected the code, the simulation worked good enough.

Download this exercice’s code here

Termistor and output exercise

The next exercice we did was to write a code to make my termistor interact with an output.

TERMO SENSOR LM35 CODE

int valor=0;
double temp=0.0;

void setup() 
{
  pinMode(2,OUTPUT); //Configuracion del pin
  digitalWrite(2,LOW);//Inicia el pin en un estado bajo 0 logico o 0 voltios 
}

void loop() 
{
  valor=analogRead(A3);
  temp = valor * 5.0 * 100.0 / 1024.0;  //convierte de 0 a 1023 en grados centígrados 

  if(temp < 10.0) 
  {
    digitalWrite(2,HIGH);    []
  }
  else
  {
    digitalWrite(2,LOW);
  }
}

I learned that the factors after “if” have to be between crochets, like 2 individual operations contained into the void loop‘s one. I learned also the diference between analog information and digital information and the importance of de Analog Digital Converter in a microcontroller. If I am right, analog pins read the electric stimulation from an input in a precise amount of energy, this information is transformed into the unity we want to work with (temperature in this case so Celsius degrees) by an equation inserted into the code at the same time that it passes into the ADC to be turned into a binari or digital information (0 or 1; high or low)

int valor=0;
double temp=0.0;

void setup() 
{
  pinMode(2,OUTPUT); //Configuracion del pin
  digitalWrite(2,LOW);//Inicia el pin en un estado bajo 0 logico o 0 voltios 
}

void loop() 
{
  valor=analogRead(A3);
  temp = valor * 5.0 * 100.0 / 1024.0;  //convierte de 0 a 1023 en grdaos centigrados 

  if (temp > 50.0) 
  {
    digitalWrite(2,HIGH);  
  {
    }
  }
  else
  {
    digitalWrite(2,LOW);
  }
  }

The code works but I have to try it on matter.

Download this exercice’s code here

HERO SHOT

  • In the way, I learned to use Tinkercad and I adored that software. It allows to create simulations with virtual components and the code is automatically written in Arduino code:
    Downlowad here the code of my Tinkercad’s simulation.

Download Tinkercad’s code here

Serial Comunication exercise

In short there are four indispensables pins that have to be used to enable a serial comunication: two transmisors TD and two receptors RX.
So the microcontroller’s transmisor have to be connected with the virtual terminal’s receptor and vice versa.

I started by writing the code. I need two Comunicators and there are COM1 and COM2 that have to be labeled on FTDI’s boards.

I added an FTDI and a visual terminal to the circuit simulator and I match the necessary connections. I set the FTDI with 9600 on Virtual baud rate and with COM 1 on physical port.

“In telecommunication and electronics, baud (/bɔːd/; symbol: Bd) is a common measure of symbol rate, which is one of the components that determine the speed of communication over a data channel.

It is the unit for symbol rate or modulation rate in symbols per second or pulses per second. It is the number of distinct symbol changes (signaling events) made to the transmission medium per second in a digitally modulated signal or a bd rate line code”. From Wikipedia

Then I had to Download a Virtual Serial Port Driver and create a comunication pair: COM1 and COM2.

#include <SoftwareSerial.h>

SoftwareSerial ftdi(1, 0); // RX, TX
int valor; //entre 0 y 1014
double temp;
char dato;

void setup() 
{
 ftdi.begin(9600);
}

void loop() 
{
  valor=analogRead(A3); //
  temp = valor * 5.0 * 100.0 / 1024.0;  //convierte de 0 a 1023 en grdaos centigrados 
  dato=ftdi.read();//leo un dato de comunicación
  ftdi.println(temp); //imprimir

  delay(10);
}

I copied the .hex URL into the microcontrollers settings into de simulator to upload the code.

The next step was to identify Arduino’s interface as COM 2

Once that done, I run the programm on the simulator and I opened the serial plotter on Arduino.

My sensor was being readed by the virtual terminal and then the information was being sended by serial comunication on the Arduinos’s serial plotter and here is my Hero shot!

Download this exercice’s code here

Learning outcomes

  • [x] Identify relevant information in a microcontroller datasheet.
  • [x] Implement programming protocols.

Have you?

  • [x] Linked to the group assignment page
  • [x] Documented what you learned from reading a microcontroller datasheet.
  • [x] Programmed your board
  • [x] Described the programming processes you used
  • [x] Included your source code:
  • [x] Included a short ‘hero video’ of your board