176
1 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich [email protected]

Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . [email protected]

Embed Size (px)

Citation preview

Page 1: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

1

Introduction to Microcontrollers

Michele Magno IIS Group - ETH Zurich [email protected]

Page 2: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

2

Outline MCU Architecture CPU Power Consumption MCU Peripherals ARM Architecture

ARM Coretx ARM Instruction Set STM32 ARM Coretex M3 Family

Data Sheet Examples

Page 3: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

3

What is a microcontroller ?

A Microcontroller is a small CPU with many support devices built into the chip

Self Contained (CPU, Memory, I/O)

Application or Task Specific (Not a general-purpose computer)

Appropriately scaled for the job

Small power consumption

Low costs ( $0.50 to $5.00.)

Page 4: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

4

Example of MCU Architecture I/O Port ADC - DAC

USARTx TIMERs DMA

Memory Clock

BUS

CPU

Page 5: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

5

How we compare and classify microcontrollers? Performance Metrics NOT easy to define and mostly application

depended.

Performance Metrics

Computation: Clock Speed MIPS (instructions per sec) Latency

Lateness of the response Lag between the begin and the end

of the computation Throughput

Tasks per second Byte per second

Goal: best tradeoff power consumptions Vs performances

Eletrical: Power Consumptions Voltage Supply Noise Immunity Sensitivity

Page 6: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

6

Power as a Design Constraint Why worry about power?

Battery life in portable and mobile platforms

Power consumption in desktops, server farms - Cooling costs, packaging costs, reliability, timing - Power density: 30 W/cm2 in Alpha 21364

(3x of typical hot plate)

Where does power go in CMOS?

leakshort2 VIfAVIfACVP ++= τ

Dynamic power consumption

Power due to short-circuit current during transition

Power due to leakage current

Page 7: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

7

Dynamic Power Consumption

fACV2

A - Activity of gates How often on average do wires switch?

f – clock frequency Trend: increasing ...

V – Supply voltage Trend: has been dropping with each successive fab

C – Total capacitance seen by the gate’s outputs Function of wire lengths, transistor sizes, ...

Reducing Dynamic Power 1) Reducing V has quadratic effect; Limits? 2) Lower C - shrink structures, shorten wires 3) Reduce switching activity - Turn off unused parts or

use design techniques to minimize number of transitions

Page 8: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

8

Short-circuit Power Consumption

Finite slope of the input signal causes a direct current path between VDD and GND for a short period of time during switching when both the NMOS and PMOS transistors are conducting

Vin Vout

CL

Ishort

fAVIshortτ

Reducing Short-circuit 1) Lower the supply voltage V 2) Slope engineering – match the rise/fall time of the input and output signals

Page 9: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

9

Leakage Power

leakVI

Sub-threshold current grows exponentially with increases in temperature and decreases in Vt

Sub-threshold current

Page 10: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

10

How can we reduce power consumption? Dynamic power consumption

Reduce the rate of charge/discharge of highly loaded nodes Reduce spurious switching (glitches) Reduce switching in idle states (clock gating) Decrease frequency Decrease voltage (and frequency)

Static power Consumption Smaller area (!) Reduce device leakage through power gating Reduce device leakage through body biasing Use higher-threshold transistors when possible

Power performance tradeoffs!

Page 11: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

11

Operating Modes

Page 12: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

12

Why Ultra-low Power Is so Important

Longer battery life Smaller products Simpler power supplies Less EMI simplifies PCB Permanent battery Reduced liability

Page 13: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

13

System Clock

Generator

ACLK

SMCLK

MCLK

CPU

Key Features:

• MCLK Main clock provided to the CPU

• SMCLK Sub-Main clock provided to the peripherals

• ACLK Auxiliary clock at low frequency provided to the

peripherals

• Peripherals can work at High and Low frequency

• Each Clock can be disabled (Clock Gating, reducing

dynamic power) by setting the status register SR.

• The CPU can be disabled (reducing Leakage power) by

setting the SR.

Clock Distribution

Page 14: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

14

Clock System Generator

Clock system Module provides the clocks for the MCU devices

Page 15: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

15

Memory

RAM (usually SRAM) Volatile memory for runtime execution Fastest access, low amount (<100Kb) Allocates variables

Flash ROM On-chip non-volatile memory used for code or data storage 8-512Kb, about 10k write cycles Bootloader: protected section to upload code in flash

(Ferroelectric Random Access Memory) FRAM Forefront of next generation non-volatile memory technology On-chip non-volatile memory faster (50ns) and lower power (250x less)

than Flash.

External memory Connected via serial (I2C, SPI) or dedicated (FSMC) interface

Page 16: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

16 16

Memory - Address Space On-Chip FLASH/ROM and RAM memory Everything is mapped into a single, contiguous address space:

All memory, including RAM, Flash/ROM, information memory, special function registers (SFRs), and peripheral registers.

Memory Address Description Access End: 0FFFFh

Start: 0FFE0h Interrupt Vector Table Word/Byte

End: 0FFDFh

Flash/ROM

0F800h Start *: 01100h

Word/Byte

010FFh End *: 0107Fh Information Memory

Start: 01000h (Flash devices only) Word/Byte

End: 0FFFh Start: 0C00h

Boot Memory (Flash devices only) Word/Byte

09FFh End *: 027Fh RAM

Start: 0200h Word/Byte

End: 01FFh Start: 0100h

16-bit Peripheral modules Word

End: 00FFh Start: 0010h

8-bit Peripheral modules Byte

End: 000Fh Start: 0000h

Special Function Registers Byte

Flash / ROM

RAM

Peripherals

Page 17: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

17

Interrupts A way to respond to an external event (i.e.,

flag being set) without polling

How it works: H/W senses flag being set Automatically transfers control to s/w that

“services” the interrupt When done, H/W returns control to

wherever it left off

Advantages: Transparent to user cleaner code μC doesn’t waste time polling

Main Prog ISR

: : : :

RETI

Page 18: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

18

Interrupts: details

3 types System reset (Non)-maskable NMI Maskable

Interrupt priorities could be fixed and defined by the arrangement of modules or set in the interrupt priority register

Page 19: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

19

(Non)-Maskable Interrupts

Sources An edge on the RESET pin when configured in NMI mode An oscillator fault occurs An access violation to the flash memory

Are not masked by GIE (General Interrupt Enable), but are enabled by individual interrupt enable bits

Page 20: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

20

NMI Interrupt Handler example

Page 21: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

21

Maskable Interrupts

Caused by peripherals with interrupt capability

Each interrupt can be disabled individually by an interrupt enable bit

All interrupts can be disabled by GIE bit in the status register

Page 22: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

22

Interrupt acceptance

1) Any currently executing instruction is completed. 2) The ProgramCounter PC, which points to the next instruction, is pushed onto the

stack. 3) The StatusRegister SR is pushed onto the stack. 4) The interrupt with the highest priority is selected if multiple interrupts occurred

during the last instruction and are pending for service. 5) The interrupt request flag resets automatically on single-source flags. Multiple source

flags remain set for servicing by software. 6) The SR is cleared. This terminates any low-power mode. Because the GIE bit is

cleared, further interrupts are disabled. 7) The content of the interrupt vector is loaded into the PC: the program continues with

the interrupt service routine at that address.

Page 23: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

23

Return from Interrupt

RETI - Return from Interrupt Service Routine 1. The SR with all previous settings pops from the stack. All

previous settings of GIE, CPUOFF, etc. are now in effect, regardless of the settings used during the interrupt service routine.

2. The PC pops from the stack and begins execution at the point where it was interrupted.

Page 24: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

24 24

Timers

Correct system timing is a fundamental requirement for the proper operation of a real-time application; If the timing is incorrect, the input data may be processed after the output

was updated The timers may be driven from an internal or external clock; Usually timers include multiple independent capture and compare

blocks, with interrupt capabilities; Main applications:

Generate events of fixed-time period; Allow periodic wake-up from sleep; Count external signals/events; Signal generation (Pulse Width Modulation – PWM); Replacing delay loops with timer calls allows the CPU to sleep between operations,

thus consuming less power.

Page 25: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

25

• The general-purpose timers consist of a 16-bit auto-reload counter driven by a programmable prescaler.

• They may be used for a variety of purposes, including measuring the pulse lengths of input signals (input capture) or generating output waveforms (output compare and PWM).

• Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds using the timer prescaler and the RCC clock controller prescalers.

• General-purpose TIMx timer features include:

- 16-bit up, down, up/down auto-reload counter.

- 16-bit programmable prescaler used to divide (also “on the fly”) the counter clock frequency by any

factor between 1 and 65535.

- Up to 4 independent channels for:

‣ Input capture

‣ Output compare

‣ PWM generation (Edge- and Center-aligned modes)

‣ One-pulse mode output

Timers

Page 26: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

26

Timers

General-purpose timer block diagram

Page 27: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

27

Timers

PATH: TimerX - Channel 1 - output compare

Page 28: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

28

Timers

PATH: TimerX - Channel 2 - output compare

Page 29: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

29

Timers

PATH: TimerX - Channel 3 - output compare

Page 30: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

30

Timers

PATH: TimerX - Channel 4 - output compare

Page 31: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

31

Timers

24MHz

Page 32: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

32

Timers

Page 33: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

33

Timers

The counter clock can be divided by a prescaler.

Page 34: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

34

Timers

The main block of the programmable timer is a 16-bit counter with its related auto-reload register. The counter can count up, down or both up and down.

CK_CNT

CNT

Page 35: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

35

Timers

In upcounting mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register), then restarts from 0 and generates a counter overflow event.

CK_CNT

CNT

Autoreload Register

Timer Interrupt

Period

Page 36: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

36

Timers

Output compare mode: This function is used to control an output waveform or indicating when a period of time has elapsed.

CK_CNT

CNT

Autoreload Register

Timer Interrupt

OC1

CH1 Interrupt

Compare Register

Page 37: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

37

Timers

Output compare mode: This function is used to control an output waveform or indicating when a period of time has elapsed.

CK_CNT

CNT

Autoreload Register

Timer Interrupt

OC1

CH1 Interrupt

Compare Register

Used to schedule periodic events

Page 38: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

38

Watchdog Timer (WDT)

WDT module performs a controlled system restart after a software problem occurs Can serve as an interval timer

(generates interrupts) WDT Control register is

password protected

Page 39: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

39

Watchdog timer (WDT )

The 16-bit WDT module can be used in:

Supervision mode: - Ensure the correct working of the software application; - Perform a PUC; - Generate an interrupt request after the counter overflows.

Interval timer:

- Independent interval timer to perform a “standard” interrupt upon counter overflow periodically;

- Upper counter (WDTCNT) is not directly accessible by software; - Control and the interval time selecting WDTCTL register;

Page 40: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

40

Digital I/O

Independently programmable individual I/Os

Several ports

Each has 8 I/O pins

Each pin can be configured as input or output

Some pins can be configured to assert an interrupt request

0 1 2 3 4 5 6 7

P1.

P6.

P2.

Input Register PxIN

Output Register PxOUT

Direction Register PxDIR

Interrupt Flag Register PxIFG

Interrupt Enable Register PxIE

Interrupt Edge Select Register PxIES

Function Select Register PxSEL

P3.

P5.

Port1 Port2

Port3

Port6

yes yes

yes no

yes no

yes no

yes yes

yes yes

yes yes

P4.

Page 41: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

41

GPIO – General Purpose I/O

Avoid floating inputs!!!

Use a pull-up/down resistor, GND, or internal programmable logic

Button produces either Vcc or Floating input. Adding a pull-down resistor fixes it.

Port Pin

To Input Logic

5.6KΩ

VCC Button

Some ports have internal programmable resistors

5.6KΩ

VCC Button

5.6KΩ

Page 42: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

42

GPIO - Inside Inputs/Outputs

Input section

Output section

Each pin is independent Ports (out) and Pins (in) are

different!!!

Page 43: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

43

Interfaces

Several protocols for inter-chip communication UART, I2C, SPI, USB,…

Serial communication protocols Meant for short distances “inside the box” Low complexity Low cost Low speed ( a few Mbps at the fastest ) Serial communication is employed where it is not practical,

either in physical or cost terms, to move data in parallel between systems.

Page 44: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

44

I2C Shorthand for an “Inter-integrated circuit” bus

I2C devices include EEPROMs, thermal sensors, and real-time clocks

Used as a control interface to signal processing devices that have separate data interfaces, e.g. RF tuners, video decoders and encoders, and audio processors.

I2C bus has three speeds: Slow (under 100 Kbps) Fast (400 Kbps) High-speed (3.4 Mbps) – I2C v.2.0

Limited to about 3 meters for moderate speeds

Page 45: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

45

I2C (Inter-Integrated Circuit) protocol Communications is always initiated and completed by

the master, which is responsible for generating the clock signal;

In more complex applications, I2C can operate in multi-master mode;

The slave selection by the master is made using the seven-bit address of the target slave;

The master (in transmit mode) sends: Start bit; 7-bit address of the slave it wishes to communicate with; A single bit representing whether it wishes to write (0) to or

read (1) from the slave; The target slave will acknowledge its address.

Page 46: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

46

I2C Bus Configuration

2-wire serial bus – Serial data (SDA) and Serial clock (SCL)

Half-duplex, synchronous, multi-master bus

No chip select or arbitration logic required

Lines pulled high via resistors, pulled down via open-drain drivers

(wired-AND, avoid short

circuit among the bus)

Page 47: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

47

I2C Features “Clock stretching” – when the slave (receiver) needs more time to

process a bit, it can pull SCL low. The master waits until the slave has released SCL before sending the next bit.

“General call” broadcast – addresses every device on the bus

10-bit extended addressing for new designs. 7-bit addresses all exhausted

Start

Address bits Receiver Ack

Data bits

Stop

Direction

Page 48: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

48

Example I2C bridge

Page 49: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

49

Sensors data acquisition example Realization with digital sensor: Data acquisition procedure:

Page 50: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

50

SPI Shorthand for “Serial Peripheral Interface” Defined by Motorola on the MC68HCxx line of

microcontrollers Generally faster than I2C, capable of several Mbps

Applications: Like I2C, used in EEPROM, Flash, and real time clocks Better suited for “data streams”, i.e. ADC converters Full duplex capability, i.e. communication between a codec and

digital signal processor

Page 51: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

51

Serial Peripheral Interface (SPI) protocol Supports only one master;

Can support more than a slave;

Short distance between devices, e.g. on a printed circuit boards (PCBs);

Special attention needs to be observed to the polarity and phase of the clock signal;

The master sends data on one edge of clock and reads data on the other edge. Therefore, it can send/receive at the same time.

Page 52: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

52

SPI Bus Configuration

Synchronous serial data link operating at full duplex Master/slave relationship 2 data signals:

MOSI – master data output, slave data input MISO – master data input, slave data output

2 control signals: SCLK – clock /SS – slave select (no addressing)

Page 53: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

53

SPI structure

As the register transmits the byte to the slave on the MOSI signal line, the slave transfers the contents of its shift register back to the master on the MISO signal line, exchanging the contents of the two shift registers.

Page 54: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

54

SPI vs. I2C

For point-to-point, SPI is simple and efficient Less overhead than I2C due to lack

of addressing, plus SPI is full duplex.

For multiple slaves, each slave needs separate slave select signal SPI requires more effort and more

hardware than I2C

SPI

I2C

Page 55: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

55

UART Shorthand for “Universal Asynchronous Receiver-Transmitter “

A UART’s transmitter is essentially just a parallel-to-serial converter with extra features.

The UART bus is a full-duplex bus. The essence of the UART transmitter is a shift register that is loaded

in parallel, and then each bit is sequentially shifted out of the device on each pulse of the serial clock.

Application: Communication between microprocessors, pc Used to interface the microcontroller with others transmission bus as: RS232,

RS485, USB, CAN BUS, KNX, LonWorks ecc. Used to connect microntroller with modem and transceiver as: telephone

modem, Bluetooth, WIFi, GSM/GPRS/HDPSA

Page 56: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

56

UART Asynchronous serial devices, such as UARTs, do not share a common

clock Each device has its own, local clock. The devices must operate at exactly the same frequency. Logic (within the UART) is required to detect the phase of the

transmitted data and phase lock the receiver’s clock to this. Bitrate: 2400, 19200, 57600,115200, 921600… One of the problems associated with serial transmission is

reconstructing the data at the receiving end, because the clock is not transmitted.

Difficulties arise in detecting boundaries between bits.

Page 57: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

57

UART The transmission format uses: 1 start bit at the beginning Settable 5,6,7,8 data bits string length Settable 1 or 0 even/odd parity bit control settable 1, 1.5, 2 stop bits end of each frame.

Parity control The parity bit control is accordingly set to 0 or 1 to have and odd

number of frame 1 bits in odd parity either an even number of frame 1 bits in the even parity

The control can detect 1 bit error in the frame

Page 58: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

58

UART transmission UART can transmit either with 2 or 4 wires

2 wires mode has transmit and receive lines

4 wires mode has transmit and receive lines plus 2 handshake signals, RTS request to send, CTS clear to send

UART

Tx FIFO

Rx FIFO

UART

Rx FIFO

Tx FIFO

Page 59: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

59

Analog to Digital Converters Most engineering applications require some form of data processing:

measurement, control, calculation, communication or data recording;

These operations, either grouped or isolated, are built into the measuring instruments;

The measuring equipment must maintain: - Compatibility and communication between measuring devices; - Acceptable error margin; - Noise and interference immunity; - Predictable measurement uncertainty; - Suitable type of control (analogue/digital); - Mathematical processing capacity; - …

Page 60: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

60

Direct Memory Access

Direct Memory Access (DMA) allows memory-to-memory or peripheral-to-memory communication without the intervention of the main CPU. The CPU initiates the data transfer and then can do other tasks or go in

stand-by

The DMA controller handles the actual data stream and sends an interrupt when done

Page 61: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

61

Direct Memory Access Concept of DMA: move functionality to peripherals

Peripherals use less current than the CPU; Delegating control to peripherals allows the CPU to shut down (saves

power) or perform other tasks (increase processing capabilities); “Intelligent” peripherals are more capable, providing a better opportunity for

CPU shutoff; DMA can be enabled for repetitive data handling, increasing the throughput

of peripheral modules; Minimal software requirements and CPU cycles.

Page 62: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

62

STM32 ARM® CortexTM-M Family

Page 63: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

63

STM32 ARM® CortexTM-M Family

Page 64: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

64

ARM Cortex-M3 Processor Cortex-M3 architecture

Harvard bus architecture 3-stage pipeline with branch speculation

Configurable nested vectored interrupt controller (NVIC)

Wake-up Interrupt Controller (WIC) Enables ultra low-power standby operation

Extended configurability of debug and trace capabilities More flexibility for meeting specific market requirements

Optional components for specific market reqs. Memory Protection Unit (MPU) Embedded Trace Macrocell™(ETM™)

Support for fault robust implementations via configurable observation interface EC61508 standard SIL3 certification

Physical IP support Power Management Kit™(PMK) + low-power standard cell libraries

and memories enable0.18μm Ultra-Low Leakage (ULL) process

Page 65: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

65

STM32F4 – CORTEX M4 STM32F401 – 84 MHz, the smallest, cost-effective solution with outstanding power efficiency STM32F405/415 – 168 MHz up to 1 Mbyte of Flash with advanced connectivity and encryption STM32F407/417 – 168 MHz, up to 1 Mbyte of Flash adding Ethernet MAC and camera interface STM32F427/437 – 180 MHz, up to 2 Mbytes of dual-bank Flash with SDRAM interface, Chrom-ART Accelerator™, serial audio interface, more performance and lower static power consumption STM32F429/439 – 180 MHz CPU/225 DMIPS, up to 2 Mbytes of dual-bank flash adding an LCD-TFT controller

Page 66: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

66

STM32F401 Product Lines

Page 67: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

67

STM32F10x Product Lines (2)

1x12-bit ADC

(1µs) Temp sensor

36MHz CPU

Up to 1MB Flash / 80KB

SRAM

Access Line: STM32F101

Performance Line: STM32F103

All lines include: Multiple communication peripherals

Up to 5 x USART, 3xSPI, 2xI²C

Main Osc 4-16MHz (25MHz on 105/107)

Up to 12 DMA channels

2 x Watchdogs

Multiple 16-bit Timers

Internal 8 MHz RC and 40 kHz RC

Reset circuitry and Brown Out Warning

Real Time Clock with Battery domain & 32KHz ext osc

Dual 12-bit DAC***

Up to 1MB Flash /

96KB SRAM

CAN

USB–FS Device

PWM

timer

2/3x12-bit ADC (1µs)

TempSensor

72MHz CPU

SDIO*

I2S*

FSMC**

ETM*

USB Access Line: STM32F102

1x12-bit ADC

(1µs) Temp sensor

48MHz CPU

Up to 128KB Flash / 16KB

SRAM

USB–FS Device

Value Line: STM32F100

1x12-bit ADC

(1.2µs) Temp sensor

24MHz CPU

Up to 512KB Flash / 32KB

SRAM

HDMI-CEC

PWM

timer

Connectivity Line: STM32F107

Up to 256 KB Flash /

64KB SRAM

2 x CAN

USB 2.0 OTG (FS)

PWM

timer

2x12-bit ADC (1µs)

TempSensor

72MHz CPU

2 x Audio Class I2S

Ethernet IEEE158

8

Connectivity Line: STM32F105

Up to 256 KB Flash /

64KB SRAM

2 x CAN

USB 2.0 OTG (FS)

PWM

timer

2x12-bit ADC (1µs)

TempSensor

72MHz CPU

2 x Audio Class I2S

* Performance/Access Lines 256KB Flash or more, Value Line with 100+pins and ALL Connectivity devices ** Performance and Access and Value devices with 256KB Flash or more. *** ALL Value line devices and Performance/Access devices with 256KB Flash or more

Page 68: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

68

Datasheet example : system architecture

Page 69: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

69

Datasheet example

Page 70: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

70

Datasheet example: Timers

Page 71: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

71

Datasheet example

Page 72: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

72

Datasheet example: GPIO

Page 73: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

73

Questions?

Page 74: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

74

EXERCISE – LAB ACTIVITIES STM32F401DISCOVERY

STM32F401VC MCU

ST-LINK/V2 embedded debugger

ST MEMS digital accelerometer,

ST MEMS digital microphone,

audio DAC + class D speaker

LEDs/ pushbuttons

USB OTG micro-AB connector

DATASHEET STM32F401; User Manual UM1669; Reference Manual RM0368

Page 75: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

75

C files (.c)

Toolchain

Target

object files (.o)

Development board Target HW simulator

Debugger

Application Cross-Development

Page 76: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

76

Embedded Systems Debugging

It is inconvenient or even impossible to run a software debugger together with the debugee Embedded systems are usually debugged using

remote debugging The debugger is running on a host computer and

controls the target either through hardware or a small software running on the target

Page 77: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

77

toolchains officially supported by STM, (STM provide simple tutorials and file of set up): ATOLLIC CooCox HITEX IAR KEIL RAISONANCE STM not support toolchain for Linux

but ”un”fortunately, multiple toolchain exist also for Linux users.

TOOLCHAIN FOR STM32

Page 78: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

78

JTAG

Integrated Debug Circuitry / On-Chip Debug: every chip shipped contains the debug functionality. A serial communication channel is used to connect the debug circuitry to a host debugger Besides debugging, another application of JTAG is

allowing device programmer to transfer data into iternal memory

Page 79: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

79

JTAG for programming

To program a device we have two alternatives: Using a USB / UART / … connection in bootloader mode Using JTAG and programmer to write flash memory

Page 80: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

80

JTAG for debugging

App.exe IDE

Build

Debug

App.hex

Build

HostOS

x86 Driver

Target board JTAG

CPU

Page 81: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

81

JTAG for debugging The HW debug support block is within the cortex-M3 core. The debug interface has access to the register bank, ALU, memory, etc …

Page 82: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

82

Keil MDK-ARM

The MDK-ARM is a complete software development environment for Cortex™-M, Cortex-R4, ARM7™ and ARM9™ processor-based devices.

MDK-ARM is specifically designed for microcontroller applications, it is easy to learn and use, yet powerful enough for the most demanding embedded applications.

Page 83: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

83

Your First Porject Open the Template.uvproj Keil uVision4 will open

Page 84: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

84

uVision IDE

Integrated Development Environment with MDK-ARM software environment and compiler

Manual at: http://www.keil.com/

Page 85: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

85

uVision IDE

File Browser

Editor

Output Window

Page 86: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

86

uVision IDE – Project Files

Project files can be organized in groups for an easy menagment. This organization is independent of the actual files organization on the disk. The code is usually organized in subgroups following the abstraction layers and libraries. In this example: Core library CMSIS and MDK-ARM (startup and system) ST StdPeriph_Driver library (on-chip devices init and use) Discovery Library (on-board devices init and use) User code (main and all the application code)

Page 87: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

87

Project Options

Choose the correct target

Page 88: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

88

Project Options

Choose the compilation output

Where compiled files are saved

Other information that might be useful

Page 89: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

89

Project Options

Global Defines

Page 90: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

90

Project Options

Debug Settings

Page 91: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

91

Project Options

ST-LINK configuration

Page 92: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

92

Project Options

ST-LINK configuration

Page 93: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

93

Project Options

Target programming options

Page 94: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

94

Write Your Code!

In main.c write a simple function no printf, no getch

Page 95: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

95

Code Compilation

Compile and build solution

Compilation Result

Page 96: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

96

Code Debugging

Donwload code to device and start debugging

Start execution

Reset device

Stop execution

Control execution

Set / remove breakpoint

Page 97: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

97

Code Debugging

Code window

Page 98: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

98

Code Debugging

Disassembly window: shows program execution in assembly code

Page 99: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

99

Code Debugging

ARM Internal register status

Set the breakpoint. A breakpoint is set with a double click on the lateral bar.

Current instruction location

Page 100: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

100

Code Debugging

Step: executes next instruction

int main(void) u16 count = 0; u32 b = 0; int c =0; c = SumValues(5, 18); /* main while */ while(1) count++; if (count==10000) b++; SumValues(int add1, int add2) volatile int sum_result = 0; sum_result = add1 + add2; return sum_result;

Page 101: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

101

Code Debugging

Step Over: executes next line

int main(void) u16 count = 0; u32 b = 0; int c =0; c = SumValues(5, 18); /* main while */ while(1) count++; if (count==10000) b++; SumValues(int add1, int add2) volatile int sum_result = 0; sum_result = add1 + add2; return sum_result;

Page 102: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

102

Code Debugging

Step Out: Exit from the current function

int main(void) u16 count = 0; u32 b = 0; int c =0; c = SumValues(5, 18); /* main while */ while(1) count++; if (count==10000) b++; SumValues(int add1, int add2) volatile int sum_result = 0; sum_result = add1 + add2; return sum_result;

Page 103: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

103

Code Debugging

The Call Stack + Locals window shows local variables and functions.

Page 104: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

104

Code Debugging

The Memory window displays the memory area content. Several separate windows can be used at a time.

Page 105: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

105

Code Debugging

The Watch window allows to evaluate symbols, registers, and expressions. The window displays the item name, value, and type. To add a variable just type the name (case sensitive)

Page 106: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

106

The Symbols window shows debug information about the application symbol name, location, and the symbol type. For functions, the window shows the return and parameter type.

Code Debugging

Page 107: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

107

#1 Turn On a LED

Page 108: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

108

General Purpose IO • The STM32 is well served with general purpose IO pins, having up to 80 bidirectional IO pins. The IO pins

are arranged as five ports each having 16 IO lines.

GPIO port A

GPIO port B

GPIO port D

GPIO port E

GPIO port C

PA [15:0]

PB [15:0]

PC [15:0]

PD [15:0]

PE [15:0]

AHB1

Page 109: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

109

DATASHEET CPU PIN OUT INFORMATION (DM00086815.pdf)

Page 110: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

110

Where are the CPU pins connected?

DATASHEET DISCOVERY BOARD INFORMATION (DM00093902.pdf)

Page 111: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

111

General Purpose IO (what) • I want to use a GPIO. What do I need to know?

Which bus GPIOs are connected to?

GPIO ports are always on the AHB1bus

Which port are we going to use?

All LED are connected to the I/O Port D of STM32F401

Which PINs the LEDs are connected to?

Orange LED is connected to the pin 13 of Port D

Green LED is connected to the pin 12 of Port D

Red LED is connected to the pin 14 of Pin D

Blue LED is connected to the pin 15 pin D

What do I need to do with this GPIO? (input, output, ...)

I need to write (output) to control the voltage on the LEDs

Page 112: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

112

General Purpose IO (where) • I want to use a GPIO. Where can I gather these information?

The datasheet contains all the information we need

Look at the UM1669 User Manual

we learn about the bus AHB1

all the information we need about our LEDs

Page 113: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

113

General Purpose IO (how) • I want to use a GPIO. How can I use this information to actually turn on a LED?

We need to enable the High Speed AHB (AHB1) peripheral.

void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, New);

Look at: stm32f4xx_rcc.c

We need to configure the GPIO Port

Fill up a GPIO_InitTypeDef structure

Look at: stm32f4xx_gpio.h

Init the GPIO Port with void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef*

GPIO_InitStruct);

Look at: stm32f4xx_gpio.c

Turn ON the LED

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

Look at: stm32f4xx_gpio.c

Page 114: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

114

General Purpose IO (code) main.c (green LED)

#include "stm32f4xx.h"

#include "stm32f4xx_conf.h"

int main(void)

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable the GPIO_RED Clock */

/* GPIOD Periph clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure the GPIO_LED pin */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Turn ON */

GPIO_SetBits(GPIOD, GPIO_Pin_14);

while(1);

“And light was made”

must include stm32f4xx_gpio.h

Enable AHB1 bus Port D

Configuration for Pin 14 Port D as output

Page 115: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

115

General Purpose IO (exercises)

1. Turn ON the BLUE LED Remember: the LED is connected to the Pin 15 of the Port D (on AHB1 bus)

2. Turn ON both the LEDs

3. Make the LEDs blink together

Tip: use an empty for cycle to create a delay routine 4. Make the LEDs alternately blink 5. Make ALL (Pin 12, 13, 14, 15) the LEDs alternately blink.

Page 116: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

116

General Purpose IO (questions)

1. Look at the Reference manual and stm32f4xx_gpio.h / stm32f4xx_gpio.c (and also using google)

Why do we set GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ?

Can you indicate the other modes a GPIO can be configured?

What do “Input pull-up” and “Input pull-down” mean for the GPIO configuration? In which case are these modes useful?

Page 117: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

117

#2 SysTick

Page 118: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

118

SysTick

• SysTick is used to schedule periodic events • When the SysTick expires an IRQ handler is called

Page 119: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

119

SysTick (how) • I want to schedule a periodic event. How can I use SysTick?

We need to setup the SysTick

static __INLINE uint32_t SysTick_Config(uint32_t ticks)

ticks is the number of ticks between two interrupts

SystemCoreClock is the number of ticks in 1 sec

Look at: core_cm3.h

We need to setup the callback (Interrupt Service Routine)

The ISR is always define in stm32f40x_it.c

The name of the ISR for SysTick is void SysTick_Handler(void)

Here is the code executed every ticks ticks

Page 120: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

120

SysTick (code)

main.c #include "stm32f4xx.h" #include "stm32f4xx_conf.h" int main(void) if (SysTick_Config(SystemCoreClock / 1000)) /* Capture error */ while (1); while (1);

ISR executed every 1 ms

stm32f4xx_it.c ... void SysTick_Handler(void) /* Here goes the code to periodically execute */ ...

Page 121: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

121

SysTick (exercises)

1. Make the LEDs blink using the SysTick

2. Make the LEDs alternately blink using the SysTick

3. Make the LEDs that blik at diferrent frequency

4. Make the LEDs alternately blink with a frequency lower than 1Hz. • The SysTick does not support a frequency lower than 1 Hz.

2. Look at the code (not just your code, everywhere): What SystemCoreClock is?

What is its value?

Why must the ISR be named SysTick_Handler ?

Page 122: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

122

#3 Mixed Exercises and Solutions

Page 123: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

123

1.1 Turn ON the BLUE LED

main.c (blue LED) #include "stm32f4xx.h"

#include "stm32f4xx_conf.h"

int main(void)

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable the GPIO_LED Clock */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure the GPIO_LED pin */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Turn ON */

GPIO_SetBits(GPIOD, GPIO_Pin_15);

while(1);

Page 124: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

124

1.2 Turn ON both the LEDs

main.c #include "stm32f4xx.h"

#include "stm32f4xx_conf.h"

int main(void)

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable the GPIO_LED Clock */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure the GPIO_LED pin */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14| GPIO_Pin_15; /* TRY TO PUT GPIO_Pin_All instead of GPIO_Pin_14| GPIO_Pin_15. Try to find out why of LED8 is ON!!

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Turn ON */

GPIO_SetBits(GPIOD, GPIO_Pin_14);

GPIO_SetBits(GPIOD, GPIO_Pin_15);

while(1);

Page 125: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

125

1.3 Make the LEDs Alternately Blink

main.c #include "stm32f4xx.h" #include "stm32f4xx_conf.h" int main(void) u32 i; GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); while(1)

/* Turn ON */ GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_15); for(i=0;i <720895;i++); /* Turn OFF */ GPIO_ResetBits(GPIOD, GPIO_Pin_14); GPIO_SetBits(GPIOD, GPIO_Pin_15); for(i=0;i <720895;i++);

;

Page 126: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

126

1.4 Make the LEDs blink together

main.c #include "stm32f4xx.h”

#include "stm32f4xx_conf.h" int main(void) u32 i; GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); while(1)

GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_SetBits(GPIOD, GPIO_Pin_15); for(i=0;i <720895;i++); GPIO_ResetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_15); for(i=0;i <720895;i++);

;

Page 127: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

127

2.1Make the LEDs blink using the SysTick

main.c #include "stm32f4xx.h" #include "stm32f4xx_conf.h" int main(void) GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); if (SysTick_Config(SystemCoreClock/10)) /* Capture error */ while (1); while (1);

stm32f4xx_it.c int i=0; void SysTick_Handler(void) i++; if(i%2) GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_SetBits(GPIOD, GPIO_Pin_15); else

GPIO_ResetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_15);

Page 128: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

128

3 Make the LEDs blink using the SysTick

main.c #include "stm32f4xx.h" #include "stm32f4xx_conf.h" int main(void) GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); if (SysTick_Config(SystemCoreClock/10)) /* Capture error */ while (1); while (1);

stm32f4xx_it.c int i=0; void SysTick_Handler(void) i++; if(i%2) GPIO_SetBits(GPIOD, GPIO_Pin_14); GPIO_SetBits(GPIOD, GPIO_Pin_15); else

GPIO_ResetBits(GPIOD, GPIO_Pin_14); GPIO_ResetBits(GPIOD, GPIO_Pin_15);

Page 129: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

129

1. USE ALL THE LED AVAILABLE

2. Increase the duty cycling of blink 3. Make ALL (Pin 12, 13, 14, 15) the LEDs alternately blink with different combination of times 4. Find on the board other LEDs to be controlled.

Page 130: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

130

#4 Button and Interrupts

Page 131: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

131

Interrupts The Nested Vector Interrupt Controller (NVIC):

• facilitates low-latency exception and interrupt handling

• controls power management

• implements System Control Registers.

NVIC supports up to 64 dynamically reprioritizable interrupts each with up to16levels of priority. There are an additional 16 interrupt sources within the Cortex core.

The NVIC maintains knowledge of the stacked (nested) interrupts to enable tail-chaining of interrupts.

Although the NVIC is a standard unit within the Cortex core, in order to keep the gate count to a minimum the number of interrupt lines going into the NVIC is configurable when the microcontroller is designed.

Page 132: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

132

EXTI • We want to configure an external interrupt line. • An EXTI line is configured to generate an interrupt on each falling edge. • In the interrupt routine a led connected to a specific GPIO pin is toggled.

Pressing the button

EXTI + Interrupt

Interrupt Service Routine

Page 133: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

133

EXTI (what) • I want to turn on a LED using the button. What do I need to know?

Which bus LEDs and the button are connected to?

LED We know

BUTTON????

Which port are we going to use for LEDS and the button?

LEDS PORT D

BUTTON?

Which PINs the LEDs and the button are connected to?

Red LED is connected to the pin 14 of Port D

Blue LED is connected to the pin 15 of Port D

The button is connected to the pin ?????

What do I need to do with this GPIO? (input, output, ...)

I need to write (output) for the LEDs

I need to read (input) for the button

Page 134: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

134

EXTI (how) • I want to turn on a LED using the button. How can I do that?

We need to setup LEDs as seen before in exercise #1

We need to setup the button. 3 steps needed.

1. Configuration of the GPIO for the button (button is connected to a GPIO)

2. Configuration of the EXTI line (because we want to use the button as a trigger for an external

interrupt)

3. Configuration of the NVIC (because we want to call an ISR when button is pressed)

- GPIO configuration Look at: stm32f4xx_gpio.c / stm32fxx_gpio.h

GPIO_InitTypeDef GPIO_InitStructure; /* Enable the BUTTON Clock */ RCC_APXXPeriphClockCmd(RCC_APXXPeriph_GPIOAXX, ENABLE); // INSERT THE RIGHT NAME IN XX /* Configure Button pin as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_INXXX; // INSERT THE RIGHT MODE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // INSERT THE RIGHT PIN GPIO_Init(GPIOA, &GPIO_InitStructure);

Page 135: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

135

EXTI (how) • The external interrupt/event controller consists of up to 23 edge detectors for generating

event/interrupt requests. Each input line can be independently configured to select the type (pulse or pending) and the corresponding trigger event (rising or falling or both). Each line can also masked independently. A pending register maintains the status line of the interrupt requests

Page 136: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

136

EXTI (how)

- EXTI line configuration

- // THIS IS JUST AN EXAMPLE LOOK IN THE Reference Manual change it properly

Look at: stm32f4xx_exti.c / stm32f4xx_exti.h

- NVIC configuration

- // THIS IS JUST AN EXAMPLE LOOK IN THE Reference Manual change it properly

Look at: misc.c / misc.h

We need to setup the ISR

The name of the EXTI0 IRQ is void EXTI0_IRQHandler(void)

EXTI_InitTypeDef EXTI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //CHECK FOR THE CLOCK /* Connect Button EXTI Line to Button GPIO Pin */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); /* Configure Button EXTI line */ EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure);

NVIC_InitTypeDef NVIC_InitStructure; /* Enable and set Button EXTI Interrupt to the lowest priority */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);

Page 137: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

137

EXTI (code)

main.c #include "stm32f4xx.h" #include "stm32f4xx_conf.h" void LEDs_Setup(void) INSERT THE SETUP CODE FOT THE LED void BUTTON_Setup(void) GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable the BUTTON Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AhB1Periph_AFIO, ENABLE); /* Configure Button pin as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Connect Button EXTI Line to Button GPIO Pin */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); ...

Decomment: stm32f10x_gpio.h / stm32f10x_exti.h / misc.h

Page 138: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

138

EXTI (code)

main.c /// THIS IS AN EXAMPLE, PLEASE CHECK IN YOUR DATASHEET AND FIX THE CODE ... /* Configure Button EXTI line */ EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable and set Button EXTI Interrupt to the lowest priority */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); int main(void) LEDs_Setup(); BUTTON_Setup(); while (1);

stm32f10x_it.c // YOU NEED TO MODIFY THE Interrupt Routine as you like. ... void EXTI0_IRQHandler(void) if(EXTI_GetITStatus(EXTI_Line0) != RESET) /* Turn ON LED BLUE */ GPIO_SetBits(GPIOD, GPIO_Pin_15); /* Clear the User Button EXTI line pending bit */ EXTI_ClearITPendingBit(EXTI_Line0); ...

Page 139: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

139

EXTI (exercises)

1. Write a code to avoid bouncing (google “button debounce”)

• Write a code to toggle the status of the LED (press button -> LED ON, press button again -> LED OFF, and so on...)

• Write a program in which the button starts and stops the blinking of the led

• Write a program to modify the frequency of LED blinking by pressing the button (i.e. switch ON-> LED blinks at 1 Hz / Press the button -> LED blinks at 5 Hz / Press the button -> LED blinks at 10 Hz ...)

• Write a program to recognize double click on the button to turn ON the LED. If the frequency of the clicking is too low the program should not recognize the double click. The behavior is similar to that one of a mouse: double click is recognized only if the frequency of the two clicks is high enough.

Page 140: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

140

EXTI (questions)

1. Look at the Reference manual RM0368 and standard peripheral library code (and also using google) http://www.st.com/web/en/resource/technical/document/reference_manual/DM00096844.pdf

Why do we set GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING for the button?

Why EXTI_InitStructure.EXTI_Line = EXTI_Line0; ?

What happens if we set EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; ?

Page 141: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

141

#5 Flash Memory

Page 142: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

142

FLASH • Flash memory is a non-volatile computer storage chip that can be electrically erased and reprogrammed • Flash memory is non-volatile, meaning no power is needed to maintain the information stored in the chip • Flash memory offers fast read access times (although not as fast as volatile DRAM memory used for main

memory in PCs) and better kinetic shock resistance than hard disks • Limitations:

- although it can be read or programmed a byte or a word at a time in a random access fashion, it can only

be erased a "block" at a time (once a bit has been set to 0, only by erasing the entire block can it be

changed back to 1)

- another limitation is that flash memory has a finite number of program-erase cycles (typically written

as P/E cycles)

Page 143: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

143

FLASH (what) • I want to write data in flash memory. What do I need to know?

We need to know the starting address and the end address of the memory we are going to write to

IMPORTANT: in flash there is also your program. So be careful: the addresses you are going to

use MUST be different from the addresses where your program is.

Available Space

Your Program

Page 144: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

144

FLASH (where) • I want to write data in flash memory. . Where can I gather the information I need?

The datasheet and reference manual contain all the information we need

Look at the Reference manual

Datasheet we learn about the Flash module organization

(In the low pages there is our program, so we can safely choose addresses from 0x0808000 to

0x08003FFFF)

USEFULL DATA AND DEFINE: /* Base address of the Flash sectors */ #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes

*/ #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes

*/ #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes

*/ #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes

*/ #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes

*/ #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128

Kbytes */

Page 145: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

145

FLASH (how) • I want to store data in flash. How can I store data in flash memory?

Unlock the Flash Bank1

Using void FLASH_UnlockBank1(void);

Look at: stm32f4xx_flash.c

Clear All pending flags

Using void FLASH_ClearFlag(uint32_t FLASH_FLAG);

Look at: stm32f4xx_flash.c

Erase the FLASH pages

Using FLASH_Status FLASH_ErasePage(uint32_t Page_Address);

Look at: stm32f4xx_flash.c

Program Flash Bank1

Using FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);

Look at: stm32f4xx_flash.c

Page 146: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

146

FLASH (code)

main.c //THIS IS JUST AN EXAMPLE YOU NEED TO ADAPT IT . #include "stm32f4xx.h" #define FLASH_PAGE_SIZE ((uint16_t)0x400) #define BANK1_WRITE_START_ADDR ((uint32_t)0x08008000) #define BANK1_WRITE_END_ADDR ((uint32_t)0x0800C000) uint32_t EraseCounter = 0x00, Address = 0x00; uint32_t Data = 0x3210ABCD; __IO uint32_t NbrOfPage = 0x00; volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; int main(void) /* Unlock the Flash Bank1 Program Erase controller */ FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); /* Program Flash Bank1 */ Address = BANK1_WRITE_START_ADDR; while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE)) FLASHStatus = FLASH_ProgramWord(Address, Data); Address = Address + 4; FLASH_LockBank1(); while(1);

Page 147: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

147

FLASH (exercises)

1. Check the correctness of the written data using the debugger (memory window) • Check the correctness of the data using code

Tip: you can access the flash memory using the pointers (Memory Mapping I/O) ((*(__IO uint32_t *) Address) != Data)

• Write a program to store in flash memory the number of times a button is pressed in a unit of

time. After this time a LED is turned ON if this number is above a threshold (i.e. a LED is turned ON if the button is pressed more than 2 times in 5 seconds)

• Modify the code to write in FLASH the half-word 0xBEEF and check the correctness of the data Tip: FLASH_ProgramWord() cannot be used anymore.

Page 148: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

148

#6 Using the ST Examples

Page 149: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

149

Timers (what) • I want a LED blinking at 1Hz using a timer.

We need to setup the GPIO port and pin the LED is connected to

We already know how to do that

Since we are going to use interrupts generated by timers we need to setup NVIC

The IRQChannel for TIMER2 is TIM2_IRQn

The ISR is void TIM2_IRQHandler(void)

We need a generic timer because we want the LED blinking at a fixed frequency

We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

Page 150: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

150

Timers (how) • We need to setup the GPIO port and pin the LED is connected to

• Since we are going to use interrupts generated by timers we need to setup NVIC

void NVIC_Configuration(void) // EXAMPLE OF TIM2. PLEASE CHECK YOUR Reference Manual and make change is needed NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);

Page 151: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

151

Timers (how) • We need a generic timer because we want the LED blinking at a fixed frequency

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t PrescalerValue = 0; As usual a struct is used for the configuration

of the peripheral

Page 152: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

152

Timers (how) • We need a generic timer because we want the LED blinking at a fixed frequency

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t PrescalerValue = 0; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

Clock enable for the TIMER2

Page 153: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

153

Timers (how) • We need a generic timer because we want the LED blinking at a fixed frequency

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t PrescalerValue = 0; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

upcounting mode

Page 154: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

154

Timers (how) • We need a generic timer because we want the LED blinking at a fixed frequency

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t PrescalerValue = 0; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1;

To set the prescaler we use the formula: Prescaler = (SystemCoreClock / Fx) - 1 where Fx is the counter clock of the TIMER (CK_CNT) we want. In this case we are setting the TIM2 counter clock to 1KHz

Page 155: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

155

Timers (how) • We need a generic timer because we want the LED blinking at a fixed frequency

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t PrescalerValue = 0; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1; TIM_TimeBaseStructure.TIM_Period = 999;

The frequency of the timer (frequency at which the timer interrupt is generated) is set using the period parameter: TIM2 frequency = counter clock / (period + 1) In our case we are using period=999, then: TIM2 frequency = 1KHz / (999 + 1) = 1Hz

Page 156: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

156

Timers (how) • We need a generic timer because we want the LED blinking at a fixed frequency

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; uint16_t PrescalerValue = 0; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1; TIM_TimeBaseStructure.TIM_Period = 999; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

As usual the init routine

Page 157: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

157

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; As usual a struct is used for the configuration of the peripheral

Page 158: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

158

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

The comparison between the output compare register and the counter has no effect on the outputs. (this mode is used to generate a timing base). We are interested in interrupt not in output waveform.

Page 159: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

159

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

OC1 signal is active high on the corresponding output pin

Page 160: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

160

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0;

The compare register is set to 0. The blinking frequency is then 1 Hz

Page 161: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

161

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(TIM2, &TIM_OCInitStructure);

Init as usual

Page 162: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

162

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

The compare register can be written at anytime, the new value is taken in account immediately

Page 163: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

163

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);

We are interested in the interrupt of the CHANNEL 1 of the TIMER2

Page 164: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

164

Timers (how) • We use the TIM2_CH1 (Timer 2 channel 1) in output compare mode

TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); TIM_Cmd(TIM2, ENABLE);

TIMER2 enabled

Page 165: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

165

Timers (how) • Handle the interrupt

void TIM2_IRQHandler(void) if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); GPIO_WriteBit(GPIOD, GPIO_Pin_15, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_15)));

1. Check the flags to see what channel the interrupt is related to 2. Clear the flag 3. Turn on/off the LED

Page 166: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

166

Timers (code)

main.c #include "stm32F4xx.h“ void NVIC_Configuration(void) NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); void LEDs_Configuration(void) GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure);

Page 167: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

167

Timers (code) int main(void) TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; uint16_t PrescalerValue = 0; LEDs_Configuration(); NVIC_Configuration(); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Compute the prescaler value */ PrescalerValue = (uint16_t) (SystemCoreClock / 1000) - 1; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 999; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Output Compare Timing Mode configuration: Channel1 */ /* Frozen - The comparison between the output compare register TIMx_CCR1 and the counter TIMx_CNT has no effect on the outputs.(this mode is used to generate a timing base). */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; /* OC1 signal is output on the corrisponding output pin */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; /* OC1 active high */ TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); /* TIMx_CCR1 can be written at anytime, the new value is taken in account immediately */ TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); while(1); return(0);

Page 168: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

168

Timers (code)

stm32f4xx_it.c ... void TIM2_IRQHandler(void) if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); GPIO_WriteBit(GPIOD, GPIO_Pin_15, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_15))); ...

Page 169: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

169

Timers (exercises)

1. Modify the blinking frequency to 2Hz, 5Hz, 0.5Hz, 0.1Hz

2. Make a LED blinking at 2Hz, the other one at 3Hz

Tip: use TIM_GetCapture1() and TIM_SetCompare1() Tip: you need to use two different channels

• Use the button to modify the blinking frequency of the LEDs (using timers)

• (+) Generate a 500Hz square wave in output from TIM2_CH1 pin (check using the oscilloscope)

Tips: You should use Toggle of the Output Compare mode More information: RM0008, STM32F10x Standard Peripherals Library, AN2581

• (+) Generate a 50Hz square wave in output from TIM2_CH1 pin and a 1KHz square wave in

output from TIM2_CH2.

• (++) Generate a PWN signal @ 25KHz duty cycle 10%

Page 170: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

170

#7 Using the ST Examples

Page 171: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

171

Which Source Code for the examples?

STM32F401-Discovery_FW_V1.0.0 ( THE CPU MODEL IS CRITICAL) Download the right

version of your cpu.

3 Using ST Examples and Library

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00092826.pdf

Page 172: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

172

Opening MEMS L3GD 20 Project

Page 173: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

173

Main in Examples

c

BE ALWAYS AWARE OF CLOCKS!!

c

IF you don’t know some definition

Page 174: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

174

Main in Examples

c

BE ALWAYS AWARE OF CLOCKS!!

c

IF you don’t know some definition

Page 175: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

175

DEMO MEMS FUNCTION static void MEMS_Test(void) float Buffer[6]; uint8_t Xval, Yval = 0x00; /* Demo Gyroscope */ Demo_GyroConfig(); /* Read Gyro Angular data */ Demo_GyroReadAngRate(Buffer);

/* Update autoreload and capture compare registers value*/ Xval = ABS((int8_t)(Buffer[0])); Yval = ABS((int8_t)(Buffer[1]));

if ( Xval>Yval) if ((int8_t)Buffer[0] > 15.0f) STM_EVAL_LEDOn(LED4); STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED5); STM_EVAL_LEDOff(LED6); if ((int8_t)Buffer[0] < -15.0f) STM_EVAL_LEDOn(LED5); STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); STM_EVAL_LEDOff(LED6); else if ((int8_t)Buffer[1] < -15.0f) STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOff(LED4); STM_EVAL_LEDOff(LED5); STM_EVAL_LEDOff(LED6); if ((int8_t)Buffer[1] > 15.0f) STM_EVAL_LEDOn(LED6); STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); STM_EVAL_LEDOff(LED5);

c

c

Configuration

Data

Processing and Leds

Page 176: Introduction to Microcontrollers · PDF file · 2014-03-211 Introduction to Microcontrollers Michele Magno IIS Group - ETH Zurich . michele.magno@iis.ee.ethz.ch

176

CONTINUE TO TRY AND TEST ALL THE EXAMPLES AND YOU WILL KNOW ALL ABOUT THE STM32 FEATURES!!!