Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

EEE026 - Projeto de Sistemas Embutidos

Programação de SE – Introdução

Diógenes C. da Silva Jr.


DEE/UFMG
diogenes@ufmg.br

EEE026 – PSE – UFMG 2023


Ciclo de Vida

© 2014 by M. Jiménez, R.
Palomera, & I. Couvertier
EEE026 – PSE – UFMG 2023 2
Time-to-market: a demanding design metric
 Time required to develop a product to the point it can be sold to
customers
 Market window
 Period during which the product would have highest sales
 Average time-to-market constraint is about 8 months
 Delays can be costly

Revenues ($)
Time (months)
Embedded Systems Design: A Unified
Hardware/Software Introduction, (c) 2000 Vahid/Givargis

EEE026 – PSE – UFMG 2023 3


Exemplos de programas de uP

EEE026 – PSE – UFMG 2023 4


Pisca LED

The program toggles on and off an LED


connected to pin 0 of port 1 (P1.0) of the
microcontroller, as illustrated in Fig. 4.1.
Toggling is achieved by changing the
voltage level at the pin.

EEE026 – PSE – UFMG 2023 5


Código C

EEE026 – PSE – UFMG 2023 6


Código Assembly

EEE026 – PSE – UFMG 2023 7


EEE026 – PSE – UFMG 2023 8
Chaves - bounce

Review of battery powered embedded systems design for mission-critical low-power applications
Matthew Malewski, David M. J. Cowell & Steven Freear http://orcid.org/0000-0001-7858-4155
https://doi.org/10.1080/00207217.2017.1409813

EEE026 – PSE – UFMG 2023 9


Chaves – debounce por HW

EEE026 – PSE – UFMG 2023 10


Chaves – debounce por SW

 Polling  Tempo de balanço de


 contador chaves: 10 – 15 ms
 Interrupção  Percepção humana: 75
 Detecção de borda ms

EEE026 – PSE – UFMG 2023 11


Polling

tBOUNCE n
⁄⁄

EEE026 – PSE – UFMG 2023 12


Chave

EEE026 – PSE – UFMG 2023 13


Lab. 1 – Pisca LED


GPIO Entrada 
GPIO Saída

EEE026 – PSE – UFMG 2023


Lab. 1 – Arduino Uno R3


Main Features
 Arduino UNO is a microcontroller
board based on the ATmega328P. It has
14 digital input/output pins (of which 6
can be used as PWM outputs), 6 analog
inputs, a 16 MHz ceramic resonator, a
USB connection, a power jack, an ICSP
header and a reset button. It contains
everything needed to support the
microcontroller; simply connect it to a
computer with a USB cable or power it
with a AC-to-DC adapter or battery to
get started. You can tinker with your
UNO without worrying too much about
doing something wrong, worst case
scenario you can replace the chip for a
few dollars and start over again.

https://docs.arduino.cc/hardware/uno-rev3

EEE026 – PSE – UFMG 2023


Lab. 1 – Bibliotecas Arduino


Digital I/O 
Time
 digitalRead()  delay()
 digitalWrite()  delayMicroseconds()
 pinMode()  micros()
 millis()

https://www.arduino.cc/reference/en/libraries/

EEE026 – PSE – UFMG 2023


Programação em Assembly

 Etapas

EEE026 – PSE – UFMG 2023


1. Label for Entry line: Names like “RESET”,
“INIT”, “START” are common, but any one
will do. This label serves to load the reset
vector.

2. Housekeeping and peripherals configuration:


This task includes:
 stack initialization,
 configuration of the watchdog timer,
 configuration of peripherals and I/O
ports as needed.

3. Main routine or algorithms: The instructions


for the intended program.

EEE026 – PSE – UFMG 2023 18


EEE026 – PSE – UFMG 2023 19
Subrotinas em Assembly
 The fastest and simplest way to
introduce local values or results is to
use registers.
 Assign a RAM segment for local
variables.
 Local constants not supposed to
change, may be defined in ROM
space.
 The stack is a valid segment for local
values if accompanied with
appropriate manipulation of the SP
register.

EEE026 – PSE – UFMG 2023 20


Programação em C

EEE026 – PSE – UFMG 2023 21


Programação em C

 Diretivas de pré-processamento
 #include <file>
 #define <symbolic name> expression
 #pragma (para recursos dependentes do compilador)

EEE026 – PSE – UFMG 2023 22


Variáveis em C

 attributes:
 name, type, scope, storage class, and storage duration.
 storage class:
 auto, extern, static and register (when the variable is created
and when it is destroyed).
 storage duration:
 automatic (local) or static (global).
 scope of visibility:
 file scope, function scope, or block scope.

EEE026 – PSE – UFMG 2023 23


 Static: visible only to the current module.
 Const: values that are never changed.
 Volatile: values that can change unexpectedly:
 Memory-mapped peripheral register;
 Global variables modified by an interrupt service routine;
 Global variables accessed by multiple tasks within a multi-
threaded application;
 Not subject to compiler optimizations.
 volatile (“ever-changing”) and const (“read-only”).

EEE026 – PSE – UFMG 2023 24


Estruturas de dados

EEE026 – PSE – UFMG 2023 25


Pisca LED em C

EEE026 – PSE – UFMG 2023 26


Inlining – C + Assembly
No IAR: Prefira usar “Intrinsic Functions”
asm("assembly_instruction");

Problemas:
1. The compiler is not aware of the effect of the assembly
code. For this reason, registers and memory locations that
are not restored within the sequence of inline assembler
instructions might cause the rest of the code to not work
properly.
2. As opposed to the Application Programming Interface used
with high level code, inline assembly code sequences do not
have a well-defined interface with the surrounding code.
This makes it fragile and introduces the possibility of
maintenance problems.
3. Optimizations will not take into account inline assembly
code sequences.
4. With the exception of the data definition directives the rest
of the assembler directives will cause errors.
5. There is no control over the alignment.
6. Variables with auto duration cannot be accessed.

EEE026 – PSE – UFMG 2023 27


EEE026 – PSE – UFMG 2023 28
Exemplo 5.9

 Pisca led
 LED1 is connected to pin
P1.0 in active high mode;

 LED2 to pin P1.3 in


active low mode;

 Starting with LED1 on


and LED2 off, let us
toggle both LEDs;

 The diodes are kept in


the state for a period of
time.

EEE026 – PSE – UFMG 2023 29


Exemplo 5.10
 An external parallel ADC
 connected to port P1, and
 handshaking is done with port P2, bits P2.0
and P2.1.
 The ADC starts data conversion when
P2.0 makes a low-to-high transition.
 New datum is ready when P2.1 is
high.
 The objective of the code is to read
and store 10 data in memory.
 Do something else.

EEE026 – PSE – UFMG 2023 30


1. Declare variables and functions
2. In main function:
1. Setup:
1. Stop watchdogtimer
2. P2.0, P2.2 and P2.3 as output
2. Mainloop (forever):
1. Clear outputs.
2. Call function for storing data.
3. Call function for something else
3. Function to Read and Store Data.
1. Request conversion
2. Wait for data
3. Store the data
4. Prepare for new
4. Something Else
1. Send pulse to P2.3

EEE026 – PSE – UFMG 2023 31


1. Declare variables and functions
2. In main function:
1. Setup:
1. Stop watchdogtimer
2. P2.0, P2.2 and P2.3 as output
2. Mainloop (forever):
1. Clear outputs.
2. Call function for storing data.
3. Call function for something else
3. Function to Read and Store Data.
1. Request conversion
2. Wait for data
3. Store the data
4. Prepare for new
4. Something Else
1. Send pulse to P2.3

EEE026 – PSE – UFMG 2023 32


Interrupção
1. It completes the instruction being currently executed, if any.
2. The PC and the SR registers are pushed, in that order, onto the stack. In this way, the state of the CPU is preserved,
including the operating power mode before the interrupt.
3. The SR is cleared, except for the OSCOFF flag, which is left in the current state. This means that the system is taken into
the active mode.
4. The highest priority interrupt vector is loaded into the PC. For single source interrupts, the interrupt flag is reset. Multiple
sources interrupt flags should be reset by software, and priority established within the ISR.
5. The Interrupt Service Routine (ISR) is executed. When the reti instruction is executed, it will pop the SR and PC, returning
to the place where the interrupt occurred, restoring also the previous controller state.

EEE026 – PSE – UFMG 2023 33


Exemplo 7.1
Using Interrupts in Assembly Language

Consider an MSP430F2231 with a red


LED connected to P1.2 and a hardware
debounced push-button connected to P1.3.

Write an interrupt-enabled assembly


program to toggle the LED every time the
push-button is depressed.

EEE026 – PSE – UFMG 2023 34


SDK

EEE026 – PSE – UFMG 2023 35


ICE – In Circuit Emulator

https://scienceprog.com/microcontroller-simulators-and-emulators/ https://microcontrollershop.com/An%20Embedded%20Tools%20Introduction.php

EEE026 – PSE – UFMG 2023 36


MSP430 IAR IDE

EEE026 – PSE – UFMG 2023 37


2-wire JTAG (spy-bi-wire)

TI SLAU278af
EEE026 – PSE – UFMG 2023 38
4-wire JTAG

TI SLAU278af
EEE026 – PSE – UFMG 2023 39
MSP430

TI SLAU647o

EEE026 – PSE – UFMG 2023 40


10 Debugging Tips for Beginners

#1. Print things a lot #8. If you’re not sure where the
#2. Start with code that already problem is, do a binary search
works #9. Take a break and walk away
#3. Run your code every time from the keyboard
you make a small change #10. How to ask for help
 Explain what you’re trying to do
#4. Read the error message
 Show the code that’s giving the
#5. Google the error message error
#6. Guess and Check  Show the entire stack trace
including the error message
#7. Comment-out code  Explain 2-3 things that you’ve
tried already and why they didn’t
work
https://blog.hartleybrody.com/debugging-code-beginner/

EEE026 – PSE – UFMG 2023 41


Ten Bug-Killing Rules for C

1. Braces 7. Bit-wise Operators


2. Keyword "const" 8. Signed and Unsigned
3. Keyword "static" Integers
4. Keyword "volatile" 9. Parameterized Macros
5. Comments vs. Inline Functions
6. Fixed-width Data 10. Comma Operator
Types

https://barrgroup.com/embedded-systems/how-to/bug-killing-standards-for-embedded-c

EEE026 – PSE – UFMG 2023 42


More bug-killing coding rules for C

11. Keywords "short" and


"long" shall not be used
12. Keyword "char"
13. Initialization of variables
14. Global variable names
15. Constants in comparisons

EEE026 – PSE – UFMG 2023 43

You might also like