Download - Microcontrollers and embedded devices
@Th-01/1.00 TITLE #2/88
Th
● Introduction to microcontrollers and embedded devices.– History– Single-board computer– Single-chip computers– Embedded devices– Robots.
@Th-01/1.01 #3/88
Th Little History: Single board computers
● single circuit board● microprocessor(s)● memory● input/output (I/O)
REF: https://en.wikipedia.org/wiki/Single-board_computer
@Th-01/1.02 #4/88
Th History: System on a chip
REF: https://en.wikipedia.org/wiki/System_on_a_chip
● all components of a computer
● other electronic system into a single chip
@Th-01/1.03 #5/88
Th History: Embedded systems, microcontrollers
REF: https://en.wikipedia.org/wiki/Embedded_systemREF: https://en.wikipedia.org/wiki/Microcontroller
● Apollo Guidance Computer
● Microcontroller
@Th-01/1.04 #6/88
Th Little History: Robots
REF: https://en.wikipedia.org/wiki/Robot
Karel Čapek
ASIMO by Hondaadvanced humanoid robot
Industrial robots assembling cars
@Th-01/1.05 #7/88
Th Little History: Internet-of-things (IoT)
REF: https://en.wikipedia.org/wiki/Internet_of_things
@Th-01/2.00 TITLE #9/88
Th
● Numeral systems. Logical and bitwise operations.– Numeral systems; decimal, binary and other
numbers – conversion from and to; Logical and bitwise operations.
@Th-01/2.01 #10/88
Th Numeral system
REF: https://en.wikipedia.org/wiki/Numeral_system
● Binary:0 1
● Octal:0 1 2 3 4 5 6 7
● Decimal:0 1 2 3 4 5 6 7 8 9
● Hexadecimal:0 1 2 3 4 5 6 7 8 9A B C D E F
@Th-01/2.02 #11/88
Th Numeral system: Decimal
● Decimal numbers:
0 1 2 3 4 5 6 7 8 9
REF: https://en.wikipedia.org/wiki/Decimal
decimal
0
1
2
3
4
5
6
7
8
9
@Th-01/2.03 #12/88
Th Numeral system: Binary
● Binary:
0 or 1
true or false
yes or no
REF: https://en.wikipedia.org/wiki/Binary_number
binary decimal000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
@Th-01/2.04 #13/88
Th Numeral system: Hexadecimal
● Hexadecimal:
0 1 2 3 4 5 6 7 8 9
A B C D E F
binary decimal hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 FREF: https://en.wikipedia.org/wiki/Hexadecimal
@Th-01/2.05 #14/88
Th Numeral system: Octal
● Octal:
0 1 2 3 4 5 6 7
binary decimal octal
000 0 0
001 1 1
010 2 2
011 3 3
100 4 4
101 5 5
110 6 6
111 7 7REF: https://en.wikipedia.org/wiki/Octal
@Th-01/2.06 #15/88
Th Numeral system: Binary coded decimal
● Binary clock
REF: https://en.wikipedia.org/wiki/Binary-coded_decimal
binary decimal0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
@Th-01/2.07 #16/88
Th Boolean algebra
REF: https://en.wikipedia.org/wiki/Boolean_algebra
@Th-01/2.08 #17/88
Th Boolean operations, Truth tablec4 c3 c2 c1 Name Notes Abr
0 0 0 0 Contradiction Always FALSE
0 0 0 1 Logical NOR NOR
0 0 1 0 Converse nonimplication
0 0 1 1 Negation
0 1 0 0 Material nonimplication
0 1 0 1 Negation
0 1 1 0 Exclusive disjunction TRUE when different XOR
0 1 1 1 Logical NAND NAND
1 0 0 0 Logical conjunction AND
1 0 0 1 Logical biconditional TRUE when equal XNOR
1 0 1 0 Projection function
1 0 1 1 Material implication
1 1 0 0 Projection function
1 1 0 1 Converse implication OR
1 1 1 0 Logical disjunction
1 1 1 1 Tautology Always TRUE
REF: https://en.wikipedia.org/wiki/Truth_table
A B C
1 0 0 c1
2 0 1 c2
3 1 0 c3
4 1 1 c4
@Th-01/2.09 #18/88
Th Bitwise operations
REF: https://en.wikipedia.org/wiki/Bitwise_operation
● AND ● OR ● XOR
● XNOR● NOR● NANDA1 A2 B
0 0 1
0 1 0
1 0 0
1 1 0
A1 A2 B
0 0 0
0 1 1
1 0 1
1 1 1
A1 A2 B
0 0 0
0 1 0
1 0 0
1 1 1
A1 1 B
0 0 1
0 1 1
1 0 1
1 1 0
A1 1 B
0 0 0
0 1 1
1 0 1
1 1 0
A1 A2 B
0 0 1
0 1 0
1 0 0
1 1 1
@Th-01/2.10 #19/88
Th Bitshift operations
REF: https://en.wikipedia.org/wiki/Bitwise_operation
● Logical shift– Left – Right
@Th-01/2.11 #20/88
Th Bitshift operations
REF: https://en.wikipedia.org/wiki/Bitwise_operation
● Arithmetic shift
– Left – Right
@Th-01/2.12 #21/88
Th Bitshift operations
REF: https://en.wikipedia.org/wiki/Bitwise_operation
● Rotate no carry– Left – Right
@Th-01/2.13 #22/88
Th Bitshift operations
REF: https://en.wikipedia.org/wiki/Bitwise_operation
● Rotate through carry
– Left – Right
#Lab-01/1.00 TITLE @24
LAB
● Introduction to microcontrollers and embedded devices.– Demonstration of microcontroller and embedded devices.– Basic principles.– Simulation of a microcontroller system.
#Lab-01/1.01 @25
LAB Demo: Embedded devices and robots
● Digital alarm clock ● Rover
● EmbeddedComputer
REF: https://en.wikipedia.org/wiki/Embedded_system REF: https://en.wikipedia.org/wiki/Rover_(space_exploration)
#Lab-01/1.02 @26
LAB Demo: Quadcopter
REF: https://en.wikipedia.org/wiki/Quadcopter
#Lab-01/1.03 @27
LAB Simulation: Microcontrollers
Specialized desktop software Web based platforms
● Advantages– Very easy to use
● Disadvantages– May not be exact match of the
device– No connection to the real world
● Advantages– Very close match to the real device– Allows debugging– Could connect device to real world
● Disadvantages– Limited connectivity to the real
world
#Lab-01/1.04 @28
LAB Simulation: Microcontrollers – Atmel Studio Stimuli
● Atmel StudioStimuli
REF: Atmel Studio Stimuli
#Lab-01/1.05 @29
LAB Simulation: Microcontrollers – Web based platforms
● Autodesk 123D – Circuit.io
REF: https://circuits.io
#Lab-01/2.00 TITLE @31
LAB
● Introduction to microcontrollers and embedded devices.– Conversion between numeral systems.– Logical, bitwise and and arithmentic operations. Optimizations.
#Lab-01/2.01 @32
LAB Numerical conversion: binary to decimal
nth bit val 2n
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
4-bit● 0101
0+4+0+1=5● 1010
8+0+2+0=12
8-bit● 01011010
0+64+0+16+8+0+2+0=...● 10100101
128+0+32+0+0+4+0+1=...
#Lab-01/2.03 @34
LAB Numerical conversion: hexadecimal to/from binaryDEC HEX
0 0 0 0 0 0
0 0 0 1 1 1
0 0 1 0 2 2
0 0 1 1 3 3
0 1 0 0 4 4
0 1 0 1 5 5
0 1 1 0 6 6
0 1 1 1 7 7
1 0 0 0 8 8
1 0 0 1 9 9
1 0 1 0 10 A
1 0 1 1 11 B
1 1 0 0 12 C
1 1 0 1 13 D
1 1 1 0 14 E
1 1 1 1 15 F
4-bit
● 0001 = 1
● 0101 = 5
● 1010 = A
● 1111 = F
8-bit
● 0000 0001 = 01
● 0010 0011 = 23
● 0100 0101 = 45
● 0110 0111 = 67
● 1000 1001 = 89
● 1010 1011 = AB
● 1100 1101 = CD
● 1110 1111 = EF
#Lab-01/2.04 @35
LAB Bitwise operations: NOT, AND, OR, XOR in C and C++
REF: https://en.wikipedia.org/wiki/Bitwise_operations_in_C
● NOT
uint8_t a, b; a = 0b10110111; b = ~a; /* b = 01001000 */
● AND
uint8_t a, b; a = 0b10110111; b = a & 0b00001111; /* b = 00000111 */
● XOR
uint8_t a, b; a = 0b10110111; b = a ^ 0b00001111; /* b = 10111000 */
● OR
uint8_t a, b; a = 0b10110111; b = a | 0b00001111; /* b = 10111111 */
#Lab-01/2.05 @36
LAB Bitwise operations: shifting in C and C++
REF: https://en.wikipedia.org/wiki/Bitwise_operations_in_C
● Right shift uint8_t a, b;
a = 0b10110111;
b = a >> 1; /* b = 01011011 */
b = a >> 2; /* b = 00101101 */
● Left shift uint8_t a, b;
a = 0b10110111;
b = a << 1; /* b = 01101110 */
b = a << 2; /* b = 11011100 */
#Lab-01/2.06 @37
LAB Bitwise operations: shifting, arithmetic equivalents
● Right shiftDivide by power of 2
uint8_t a, b;
a = 0b01011010; /* 90 */
b = a >> 1; /* div 2 *//* b = 00101101 = 45 */
b = a >> 2; /* div 4 *//* b = 00010110 = 22 */
● Left shiftMultiply by power of 2
uint8_t a, b;
a = 0b01011010; /* 90 */
b = a << 1; /* mul 2 *//* b = 10110100 = 180 */
b = a << 2; /* mul 4 *//* b = 01101000 = 104 */
REF: https://en.wikipedia.org/wiki/Bitwise_operations_in_C
#Lab-01/2.08 @39
LAB Bitwise operations: Setting and clearing specific bit
● Setting specific bit uint8_t a, b; a = 0b10110111; b = a | 0b00001000; /* b = 10111111 */____________________________
● Multiple bits
uint8_t a, b; a = 0b10110111; b = a | 0b00001001; /* b = 10111111 */
● Clearing specific bit uint8_t a, b; a = 0b10110111; b = a & 0b11101111; /* b = 10100111 */____________________________
● Multiple bits
uint8_t a, b; a = 0b10110111; b = a & 0b10101111; /* b = 10100111 */
#Lab-01/2.09 @40
LAB Bitwise operations: Setting and clearing specific bit
● Setting specific bit
uint8_t a, b; a = 0b10110111; b = a | (1 << 3); /* 00000001 */ /* 00001000 */ /* b = 10111111 */
● Clearing specific bit
uint8_t a, b; a = 0b10110111; b = a & ~(1 << 4); /* 00000001 */ /* 00010000 */ /* 11101111 */ /* b = 10100111 */
#Lab-01/2.10 @41
LAB Bitwise operations: Flipping (set/clear) specific bit
● Flipping specific bit uint8_t a, b; a = 0b10110111; b = a ^ 0b00001000; /* b = 10111111 */ b = a ^ 0b00001000; /* b = 10110111 */____________________________
● Multiple bits
b = a ^ 0b00001001; /* b = 10111110 */
● Flipping specific bit uint8_t a, b; a = 0b10110111;
b = a ^ (1 << 4); /* 00000001 */ /* 00010000 */ /* b = 10100111 */
b = a ^ (1 << 4); /* 00000001 */ /* 00010000 */ /* b = 10110111 */
#Lab-01/2.11 @42
LAB Bitwise operations: Checking specific bit(s)
● Check if specific bit is set or clear uint8_t a; a = 0b10110111;
if (a & 0b00000100) { /* TRUE */ } else { }
if (a & 0b00001000) { } else { /* FALSE */ }
uint8_t a; a = 0b10110111;
if (a & (1 << 2)) { /* TRUE */ } else { }
if (a & (1 << 3)) { } else { /* FALSE */ }
#Lab-01/2.12 @43
LAB Bitwise operations: Check and conditionally set/clear bit
● Check if a bit is set/clear and depending on that set/clear another bit
@Th-02/1.00 TITLE #45/88
Th
● Designing of a microcontroller system– Basic principles in the design of a microcontroller
systems.– Defining of the requirements and
implementation.
@Th-02/1.01 #46/88
Th
Microcontroller
Designing of a microcontroller system: Basic principles
CPUCPURAMRAM
ProgramProgram
ROMROMUtility● Timers● Counters
Utility● Timers● Counters
I/O (GPIO)● Digital● ADC● DAC● Serial
I/O (GPIO)● Digital● ADC● DAC● Serial
System● ISP● Debug
System● ISP● Debug
@Th-02/1.02 #47/88
Th
Protection
Designing of a microcontroller system: Basic principles
PowerPower
ResetReset
Peripherals Peripherals
Microcontroller Microcontroller ProgrammingProgramming
@Th-02/2.00 TITLE #49/88
Th
● Minimal microcontroller configuration– The minimal allowed
(or acceptable, based on the requirements)microcontroller configuration.
@Th-02/2.01 #50/88
Th Minimal microcontroller configuration: Atmel ATtiny85
Microcontroller
AtmelATtiny85
● RAM 512 bytes● Flash 8 Kbytes● ROM 512 Bytes
● Clock 1/8/20 MHz
Microcontroller
AtmelATtiny85
● RAM 512 bytes● Flash 8 Kbytes● ROM 512 Bytes
● Clock 1/8/20 MHz
Vcc Vcc
PB2PB2
PB1 PB1
PB0 PB0
ResetReset
PB3PB3
PB4PB4
GND GND
(+)PowerSupply
(-)
(+)PowerSupply
(-)
ResetReset
@Th-02/2.02 #51/88
Th Tinusaur
● Digital input● Digital output● Analog to digital input● Pulse-width modulation
(analog output)● Timers● Serial input and output
PB0PB0
PB1PB1
PB2PB2
PB3PB3
PB3PB3
#Lab-02/1.00 TITLE @53
LAB
● Designing of a microcontroller system– Circuit diagram. Practical design considerations– PCB. Practical design considerations.– Assembling of a minimal microcontroller system.
#Lab-02/1.01 @54
LAB Circuit diagram
Microcontroller
AtmelATtiny85
● RAM 512 bytes● Flash 8 Kbytes● ROM 512 Bytes
● Clock 1/8/20 MHz
Microcontroller
AtmelATtiny85
● RAM 512 bytes● Flash 8 Kbytes● ROM 512 Bytes
● Clock 1/8/20 MHz
Vcc Vcc
PB2PB2
PB1 PB1
PB0 PB0
ResetReset
PB3PB3
PB4PB4
GND GND
(+)
Power3.3 V
(-)
(+)
Power3.3 V
(-)
Reset
10K
100uF
100nF
REF: https://en.wikipedia.org/wiki/Circuit_diagram
#Lab-02/2.00 TITLE @58
LAB
● Minimal microcontroller configuration– Programming and testing of a minimal microcontroller system.
(under Windows and Linux)
#Lab-02/2.01 @59
LAB Programming the microcontroller
#include <avr/io.h>#include <util/delay.h>
int main(void) { DDRB |= (1 << PB3);
while (1) { PORTB |= (1 << PB3); _delay_ms(200); PORTB &= ~(1 << PB3); _delay_ms(400); }
return (0);}
#Lab-02/2.02 @60
LAB Testing Programs
● Testing programs forShield LEDx2– Blinking LED– Blinking LEDs– Fading LED/LEDs
● Testing programs forShiled EDUx4IO– Blinking LED– Buzzer sound
@Th-03/1.00 TITLE #62/88
Th
● Development tools.● Compiling the source code to binary.● Cross-compilers for C/C++ and Assembly
language.● Specialized development environments.
@Slide 63 #63/88
Th Development Tools
● Compilers● Linkers and other tools.● Programmers.● Debugging tools.
@Slide 64 #64/88
Th Compiling to Binary
#include <avr/io.h>#include <util/delay.h>
int main(void) { DDRB |= (1 << PB3);
while (1) { PORTB |= (1 << PB3); _delay_ms(200); PORTB &= ~(1 << PB3); _delay_ms(400); }
return (0);}
B 8 9 A B 9 9 AC 0 9 A C 1 9 88 F E 4 9 3 E C0 1 9 7 F 1 F 70 0 C 0 0 0 0 0C 1 9 A C 0 9 89 F E 7 2 8 E 38 1 E 0 9 1 5 02 0 4 0 8 0 4 0E 1 F 7 0 0 C 00 0 0 0 E C C FF 8 9 4 F F C F
@Slide 65 #65/88
Th Cross-compilers for C/C++ and Assembly Language
#include <avr/io.h>#include <util/delay.h>
int main(void) { DDRB |= (1 << PB3);
while (1) { PORTB |= (1 << PB3); _delay_ms(200); PORTB &= ~(1 << PB3); _delay_ms(400); }
return (0);}
30: b8 9a sbi 0x17, 032: b9 9a sbi 0x17, 134: c0 9a sbi 0x18, 036: c1 98 cbi 0x18, 138: 8f e4 ldi r24, 0x4F3a: 93 ec ldi r25, 0xC33c: 01 97 sbiw r24, 0x013e: f1 f7 brne .-440: 00 c0 rjmp .+042: 00 00 nop44: c1 9a sbi 0x18, 146: c0 98 cbi 0x18, 048: 9f e7 ldi r25, 0x7F4a: 28 e3 ldi r18, 0x384c: 81 e0 ldi r24, 0x014e: 91 50 subi r25, 0x0150: 20 40 sbci r18, 0x0052: 80 40 sbci r24, 0x0054: e1 f7 brne .-856: 00 c0 rjmp .+058: 00 00 nop5a: ec cf rjmp .-405c: f8 94 cli5e: ff cf rjmp .-2
B 8 9 A B 9 9 AC 0 9 A C 1 9 88 F E 4 9 3 E C0 1 9 7 F 1 F 70 0 C 0 0 0 0 0C 1 9 A C 0 9 89 F E 7 2 8 E 38 1 E 0 9 1 5 02 0 4 0 8 0 4 0E 1 F 7 0 0 C 00 0 0 0 E C C FF 8 9 4 F F C F
@Th-03/2.00 TITLE #68/88
Th
● Compiling the source code to binary.● Linking the compiled binary code.● Deployment.● Debugging tools.
@Slide 69 #69/88
Th Compiling the Source Code to Binary
#include <avr/io.h>#include <util/delay.h>
int main(void) { DDRB |= (1 << PB3);
while (1) { PORTB |= (1 << PB3); _delay_ms(200); PORTB &= ~(1 << PB3); _delay_ms(400); }
return (0);}
B 8 9 A B 9 9 AC 0 9 A C 1 9 88 F E 4 9 3 E C0 1 9 7 F 1 F 70 0 C 0 . . .
● main.c– int main(void) {
...
... }
● mylib.c● myio.c
@Slide 70 #70/88
Th Linking the Compiled Binary Code
B 8 9 A B 9 9 AC 0 9 A C 1 9 88 F E 4 9 3 E C0 1 9 7 F 1 F 70 0 C 0 0 0 0 0C 1 9 A C 0 9 89 F E 7 2 8 E 38 1 E 0 9 1 5 02 0 4 0 8 0 4 0E 1 F 7 0 0 C 00 0 0 0 E C C FF 8 9 4 F F C F
B 8 9 A B 9 9 AC 0 9 A C 1 9 88 F E 4 9 3 E C0 1 9 7 F 1 F 70 0 C 0 . . .
@Slide 71 #71/88
Th Deployment
B 8 9 A B 9 9 AC 0 9 A C 1 9 88 F E 4 9 3 E C0 1 9 7 F 1 F 70 0 C 0 0 0 0 0C 1 9 A C 0 9 89 F E 7 2 8 E 38 1 E 0 9 1 5 02 0 4 0 8 0 4 0E 1 F 7 0 0 C 00 0 0 0 E C C FF 8 9 4 F F C F
● Enter programming mode● Upload binary code● Verify data● Start program
#Lab-03/1.00 TITLE @74
LAB
● Development tools.– GCC-AVR, make, AVRDUDE.
● Specialized development environments.– Atmel AVR Studio.– Arduino IDE. Installation and setup.
● Specialized debugging tools.
#Lab-03/2.00 TITLE @80
LAB
● “Hello, World!” for microcontrollers: the blinking LED.– Setup and algorithm.– 1 blinking LED.– 2 blinking LEDs.
● Practical implementation.– Loop and delay.– Compiling.– Uploading.
● Advanced– Using timers – the idea.
#Slide 81 @81
LAB Setup
Microcontroller
AtmelATtiny85
● RAM 512 bytes● Flash 8 Kbytes● ROM 512 Bytes
● Clock 1/8/20 MHz
Microcontroller
AtmelATtiny85
● RAM 512 bytes● Flash 8 Kbytes● ROM 512 Bytes
● Clock 1/8/20 MHz
Vcc Vcc
PB2PB2
PB1 PB1
PB0 PB0
ResetReset
PB3PB3
PB4PB4
GND GND
330Ω
330Ω
#Slide 82 @82
LAB “Hello, World!” for microcontrollers – the Blinking LED
Loop
LED: turn on
Delay: 200 mS
LED: turn off
Delay: 400 mS
LED port: set as output#include <avr/io.h>#include <util/delay.h>
int main(void) { DDRB |= (1 << PB0);
for (;;) { PORTB |= (1 << PB0); _delay_ms(200); PORTB &= ~(1 << PB0); _delay_ms(400); }
return (0);}
Loop: start, infinite
#Slide 83 @83
LAB 2 Blinking LEDs
#include <avr/io.h>#include <util/delay.h>
// -----------------------------
#define LED1_PORT PB0#define LED2_PORT PB1
// -----------------------------
int main(void) {
// Initialization DDRB |= (1 << LED1_PORT); DDRB |= (1 << LED2_PORT);
// Start the main loop for (;;) {
PORTB |= (1 << LED1_PORT); PORTB &= ~(1 << LED2_PORT); _delay_ms(200);
PORTB &= ~(1 << LED1_PORT); PORTB |= (1 << LED2_PORT); _delay_ms(400);
}
return (0);}
#Slide 84 @84
LAB Blinking LEDs Using Timers – the Idea
void init_timer(uint8_t max) { // Set timer in CTC mode TCCR0A |= (1 << WGM01); // Set timer in interrupt mode TIMSK |= (1 << OCIE0A); sei(); // Enable interrupts // set compare value OCR0A = max; // Prescale and start timer TCCR0B |= TIMER_TCCR0B_1024TH;}
// Define interrupt vectorISR (TIMER0_COMPA_vect) { // Flip the LED bit vector PORTB ^= (1 << LED1_PORT);}
OCR0A
1 1 0 0 1 0 1 0
TCNT0
x x x x x x x x
TCNT0 == OCR0ATCNT0 == OCR0A
for (;;) { for (TCNT0 = 0; TCNT0 < OCR0A; TCNT0++) { timer_delay(TCCR0B); } ISR (TIMER0_COMPA_vect);}
for (;;) { for (TCNT0 = 0; TCNT0 < OCR0A; TCNT0++) { timer_delay(TCCR0B); } ISR (TIMER0_COMPA_vect);}
#87/88
● Author: Neven Boyanov● Licensing: CC-BY-SA-3.0
Creative Commons Attribution–ShareAlike LicenseFull text: https://creativecommons.org/licenses/by-sa/3.0/legalcodeMore info: https://creativecommons.org/licenses/by-sa/3.0/