(MCU) Lecture1 - Introduction
(MCU) Lecture1 - Introduction
LÊ TRỌNG NHÂN
[email protected]
[email protected]
Zalo Group [CLC]
▪ 15 weeks for Lectures
▪ 6 weeks for Labs (30%)
▪ MCQs for Mid-term (20%)
▪ MCQs for Final-term (50%)
▪ Final project: +2 maximum
3
15 Weeks for Lectures
▪ Understand microcontroller basics including all
common interfaces:
▫ General Input/Output GPIO (LEDs, Buttons)
▫ Interrupts: Timer Interrupt
▫ Serial communications: UART, I2C, SPI
▪ Final projects:
▫ Validate on hardware platforms
▫ Group of 3 students
▫ Presentations and Demos
5
6 Weeks for Labs
▪ Software requirements:
▫ STMCubeIDE
▫ Proteus simulations
▫ Github
6
24h Project for Midterm
▪ A project is given and students have 24 hour to
submit
▪ The format is similar to the LAB:
▫ Project description
▫ Project manual
▫ 5 – 10 steps to finalize the project
▪ Programing on STM32CubeIDE
▪ Simulate on Proteus
▪ Submit the Report for evaluation
7
Microcontroller and Microprocessor
Applications
▪ Networking & communications
▪ phones, modems, switches, routers, signal processors
▪ Many more.
8
Microprocessor vs Microcontroller
MPU MCU
Stand-alone CPU, RAM, CPU, RAM, ROM and IO
ROM and IO (and Timer) (and Timer) are all on a
are separated single chip
ROM, RAM and IO can be ROM, RAM and IO are fixed
customized
Expensive, versatility and Low cost, power and single-
general purpose purpose (control-oriented)
High resource of power, Low resource of power,
processing and memory (for processing and memory (for
AI inferences) device controller)
Normal Operating System Real Time Operating
(32 or 64 bits) System (8 or 16 bit)
9
Micro-Controller Platform
▪ Micro-Controller Unit (MCU) contains
RAM, ROM and IO
▪ Micro-Processor Unit (MPU) only
contains the CPU
▪ System on Chip (SoC) refers to MCUs
with a greater number of onboard
peripherals and functionality
http://www.ti.com http://www.microchip.com
1
Program on MPU vs MCU
1
Warm up Exercises
▪ LED turns on and off every second (LED Blinky)
1
C Language: Header and C++ Files
#include “led.h”
#ifndef __LED_H_
#define __LED_H_
int T_on;
int T_off;
#include <system_lib.h>
int counter;
#include “user_lib.h”
#include “UserFolder/lib.h”
void setOn(long duration){
//TODO: set LED on here
extern int T_on;
}
extern int T_off;
void setoff(long duration){
//TODO: set LED off here
void setOn(long duration);
}
void setoff(long duration);
void delay(long duration){
//TODO: set delay here
#endif
}
1
C Language: Main File
#include “led.h” ▪ Modules/ Libraries
#include “timer.h” are included
#include “gpio.h”
#include “button.h”
void main(){
initGPIO(); ▪ Modules/ Libraries
LED
initTimer(); are initiated
initButton();
initLED();
TIMER
….
….
while(1){};
GPIO }
void timer_isr(){ ▪ System operations
} are implemented in
BUTTON void ext_isr(){ interrupt functions
}
1
Finite State Machine (FSM)
▪ Finite-State Machine (FSM) or Deterministic Finite
Automata (DFA), finite automaton, or simply a state
machine, is a mathematical model of computation
Current Input Next State
State
Locked Coin Unlocked
Push Locked
Unlocked Coin Unlocked
A turnstile
Push Locked
1
Finite State Machine Programming
while(1){
switch(status){
case LOCKED:
lock_turnstile(); //operation in a state
if(Coin == true) //transition condition
status = UNLOCKED; //next state
break;
case UNLOCKED:
unlock_turnstile(); //operation in a state
if(Push == true) //transition condition
status = LOCKED; //next state
break;
default:
break;
}
}
1
Example
▪ Given an LED turns on for T_on and then turns off
for T_off.
▫ Design an DFA for this LED
▫ Implement the DFA in Arduino
1
Answer
timer_flag == 1
1
Answer (Arduino Code)
void loop(){
switch(status){ void timer_run(){
case INIT: if(timer_counter > 0)
pinMode(13, OUTPUT);
timer_counter--;
setTimer(T_on);
status = LED_ON; if(counter_timer == 0)
digitalWrite(13, HIGH); timer_flag =1;
break; }
case LED_ON:
void setTimer(long duration){
if(timer_flag == 1){
status = LED_OFF; timer_counter = duration;
setTimer(T_off); timer_flag = 0;
digitalWrite(13, LOW); }
}
break;
case LED_OFF:
if(timer_flag == 1){
status = LED_ON;
setTimer(T_on);
digitalWrite(13, HIGH);
}
break;
default:
break;
}
delay(10);
} 1