TM1115 - STM32F103ZE - Chapter 7-8 (MOTOR - SERVO - ADC)

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

TM1115

Embedded System Application


and Practice
TM1115 – Course Outline
Embedded System Application and Practice

 Chapter 7 Servo and DC motor control


7.1 How to control servo motor with stm32 by using PWM
7.2 DC motor with L298N dc motor driver

Demo - Servo motor control with LCD

Exercise 07a - Rotating servo linear


STM32 + Futaba S3003 servo motor + LCD
(display message on LCD and rotating the servo motor smoothly)

 Chapter 8 ADC
8.0 Exercise 08 ADC as voltmeter, and/or ADC as dimmer
7.1 Servo motors
Servo motors are used to control the position of rotate objects, arms or hands of robot
and R/C car etc.

Most of the servo motors have


three connections:

• Black/ Brown Ground Wire

• Red Power Wire

• White or Yellow PWM Wire


7.1 Servo Motor Control
To control the position of the servo motor’s shaft, you have to generate a PWM signal
frequency of 50Hz, and a variable Pulse width(0.5~1ms, 1.5ms, 2~2.5ms). For example:
With pulse width (2~2.5ms) that corresponds to the angular position 180 degree

PWM

Power Module V+ (NOT MCU 5V)

Common GND

SDASDASD

4
7.2 DC motors
• Used in DC powered cars and robots
• Wheels and movable joints
7.2 DC Motor Control
- A DC motor will rotate clockwise or anti-clockwise with respect to the
polarity of the motor power supply.

By using four switches, we can control the polarity of the motor power
supply. We call this as “H-Bridge”.

Anti-clockwise Clockwise

6
7.2 DC Motor Control
- Motor driver with DC motor

IN1(PE2), IN2(PE6)

PWM pulses(TIM1-CH1,PE9) L298N

Encoder Encoder
Feedback

DC motor

7
7.2 DC Motor Control
- Motor driver (L298N)

Direction Control of DC Motor

Input 1 Input 2 Direction

Ground (0) Ground (0) Motor Off

5 Volts (1) Ground (0) Forward

Ground (0) 5 Volts (1) Reverse

5 Volts (1) 5 Volts (1) Motor Off


7.2 DC Motor Control
- Speed control with PWM

DC motor
7.2 DC Motor Control
- Motor Encoder O/P signal

PA6 – TIM3(ch1)
Encoder Aout

Encoder Bout
PA7 – TIM3(ch2)

https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/
Demonstration – PWM Generation
STEP 1 – Set TIM1_CH2(PE11), PB6 & PB7 as I2C1_SCL & I2C1_SDA pins

Timer1 – Parameter settings:

1. Channel 2
2. Prescaler and period to get
50Hz @72Mhz
• Prescaler:144-1(/144)
• Period:10000-1(/10000)
Demonstration – 07-Servo_step

- Servo motor and DC motor schematic drawing


Demonstration – 07-Servo_step
- Press reset button to start the rotation
Here is the result of our application

PWM signal at test point (PE11 )

Hints
1. Find out capture compare reload
value(CCR) to control the rotation
angle of servo motor
Demonstration – 07-Servo_step
STEP 2 – build project in STM32CubeMX and add code in uVision

KEY IN

Double-click
GENERATE CODE
Demo only 07a-Servo_linear

- Set servo motor = 0 degree at the very beginning

- Rotate motor in a direction from right to left with slower speed


(rotating in anti-clockwise direction)

SDASDASD
- Back to the original position with faster speed
(clockwise direction)

15
Demo 07a-Servo_linear
Press reset button to start

Here is the result of our application


Demonstration – TIMER – Encoder mode
STEP 1 – Set TIMER3

Timer3 – Parameter settings:

1. Counter Period 65535


2. Encoder
• Encoder Mode
TI1 and TI2

IN1-PE2
IN2-PE6
ENA-
PE9(TIM1_CH1)

Encoder signal A
-PA6(TIM3_CH1)
Encoder signal B
-PA7(TIM3_CH2)
Exercise 08a-DC_motor-encoder
- Use Timer counter value to turn on LED

Here is the result of our application


Exercise 08a-DC_motor-encoder
Use Timer counter value to turn on LED

Encoder

Get encoder values


8. Analog to Digital conversion(ADC)
Analog data processing - Real world interfacing
8.1 Analog to Digital Conversion (ADC)

 Mostreal world data is analog. Whether it


be temperature, pressure, voltage, their
variation is always analog in nature.
 We must process the data received. It is
required to convert them to digital form
using an Analog to Digital Converter (ADC).
8.1 ADC resolution
 The ADC converts analog voltage to binary numbers. These
binary numbers can be in different length - 2, 4, 8, 10-bit. The
more bits the binary number has, the higher the resolution of
the - A/D.

Two Bits resolution Four Bits resolution

00 01 10 11 000 001 010 011 100 101 110 111


8.1 Analog to Digital Conversion (ADC)
8.1 Analog To Digital Converter with Direct Memory Address
– ADC DMA

Direct Memory Address (DMA)


While in your MCU project, you will need to read
a bunch of data to RAM and write it in the
registers of MCU. DMA can transfer data
between memory locations without the need of
CPU
SDASDASD
Therefore, you can further speed up the processing
speed.

24
8.1 Analog To Digital Converter with Direct Memory Address
– ADC DMA

Analog To Digital Converter

25
8.1 Analog To Digital Converter with Direct Memory Address – ADC DMA
SDASDASD
Analog To Digital Converter with DMA

Loop

26
8.1 Analog To Digital Converter with Direct Memory Address – ADC DMA

Analog To Digital Converter with DMA

ADC1 is a multi-channels 12 bits ADC


𝐴𝐷𝐶 𝑅𝑒𝑔𝑖𝑠𝑡𝑒𝑟
𝑉𝑖𝑛 = × 3.3
212

SDASDASD

27
Exercise 8.1 – ADC as voltage Meter
- Add code

28
Exercise 8.1 – ADC as voltage Meter
- Analog To Digital Converter - ADC channel10

𝐴𝐷𝐶 𝑅𝑒𝑔𝑖𝑠𝑡𝑒𝑟
𝑉𝑖𝑛 = × 3.3
212

SDASDASD

~END~ 29

You might also like