Lab 12
Lab 12
MICROPROCESSOR in PRACTICE
LAB 12
DC Motor Control
HCMC 8/2024
1. Introduction
The L298 IC can control 2 DC motors, but the practice kit only has 1 motor, so the
two motor control circuits are connected in parallel to increase current capacity. In the
circuit diagram, the two input pairs input1, input4 and input2, input3 are connected. These
two pairs of input signals are directly connected to the PWM1 and PWM2 pins of the
microcontroller. Similarly, the outputs are also connected, leaving only 2 outputs
connected to the DC motor.
Since the ENA pin is connected to level 1, it is enabled, and the remaining control
signals depend on the two PWM signals, PWM1 (input1) and PWM2 (input2).
▪ Main Program:
Exercise 712: Write a program to control a DC motor with ON and OFF buttons, combined
with a precise second counter displayed on 2 seven-segment LEDs. When pressing ON and
the seconds are between 05 and 25, the motor will rotate forward; between 35 and 55, it
will rotate in reverse; for all other ranges, the motor will stop. When pressing OFF, the
motor will stop. Save the file as 'bai_712_dc_motor_on_off_tn_theo_giay'.
Exercise 713: Write a program to control both a stepper motor and a DC motor with 3
buttons: ON, OFF, and INV. When pressing ON, both motors will rotate forward. Pressing
INV will reverse the direction of both motors. Pressing OFF will stop both motors. This
combines Exercise 701 and 711. Save the file as 'bai_713_step_dc_motor_on_off_inv'.
4. Control DC Motor Speed Using PWM
This section explains how to select the duty cycle and calculate the appropriate parameters
for controlling the speed variation of the DC motor based on the hardware design of the
experiment kit.
The PWM library for controlling the DC motor is 'tv_08_dc_motor,' which contains the
following functions:
▪ Function 713: Set up the CCP1 module to control the DC motor for forward rotation.
▪ Function 714: Set up the CCP1 module to control the DC motor for reverse rotation.
▪ Function 715: Set up the CCP1 or CCP2 module to control forward or reverse rotation.
▪ Function 716: Set up the PWM duty cycle to control forward or reverse rotation.
▪ Function 717: Turn off both CCP1 and CCP2 modules, set PWM to 0.
Exercise 715: The program uses CCP1 in PWM mode to control the motor for forward
rotation with speed variation using 2 control buttons: UP, DW, and STOP. When pressing
UP, the motor rotates forward and accelerates. When pressing DW, the motor decelerates.
Pressing STOP will stop the motor. The control levels are 50, displayed on 2 seven-
segment LEDs. Use the calculated parameters from above. Save the file as
'bai_715_dc_motor_pwm_ccp1_up_dw_stop'.
▪ Subroutine 'bai_715_dc_motor_pwm_ccp1_tv':
▪ Main Program:
Exercise 716: Use the calculated results from above and program according to the
requirements.
• Button BT0 acts as a stop button to stop the motor.
• Button BT1 controls the motor to run at 35% of the maximum speed.
• Button BT2 controls the motor to run at 70% of the maximum speed.
• Button BT3 controls the motor to run at 100% of the maximum speed.
• Display the control level and PWM value on the 7-segment LEDs.
Save the file as 'bai_716_dc_pwm_ccp1_3_cap_tocdo'."
Exercise 717: The program controls the DC motor with 2 buttons: UP and DW.
• When pressing and holding UP, the motor rotates forward and automatically accelerates
until it reaches maximum speed; if released, the motor stops.
• When pressing and holding DW, the motor rotates in reverse and automatically accelerates
until it reaches maximum speed; if released, the motor stops.
• The control levels are 50, displayed on 2 seven-segment LEDs. The PWM value is
displayed on 4 seven-segment LEDs. Use the calculated parameters from above.
• There are 8 color LEDs on the left to indicate the state: off means stopped, on means
running. When rotating forward, the 8 color LEDs on the left will be lit, and the 8 color
LEDs on the right will be off. When rotating in reverse, the 8 color LEDs on the left will
be off, and the 8 color LEDs on the right will be lit, for a total of 16 LEDs on the left side.
Save the file as 'bai_717_dc_motor_up_dw_stop_auto'.
▪ Subroutine:
▪ Main Program:
Exercise 718: Like Exercise 717, but when the key is released, the control level gradually
decreases just like when increasing, using a 50 ms delay. The purpose is to slow down
when the key is released and resume increasing speed immediately from the current level
when the key is pressed again. Save the file as
'bai_718_dc_motor_up_dw_stop_slow_auto'.
Exercise 719: Like Exercise 717 but using a rotary encoder (RT). Initially, after power-
up, reset, or programming, the speed level is displayed as 0.
• When turning the RT to the right, the motor will rotate forward and gradually accelerate
up to a maximum speed of +20 (the highest).
• Turning the RT back from +20 to 0, the motor will gradually decelerate and stop when it
reaches 0.
• When turning the RT to the left, the motor will rotate in reverse and gradually accelerate
up to a maximum speed of -20 (the highest).
• Turning the RT back from -20 to 0, the motor will gradually decelerate and stop when it
reaches 0.
• Additionally, use the RT button as a Stop button: when pressed, the motor will stop.
Save the file as 'bai_719_dc_motor_up_dw_stop_auto_rotary'.
Exercise 720: Like Exercise 715 but change the motor speed to 10 levels from 0 to 9,
which can be selected by 10 buttons (0 to 9) on a touch keypad. The forward or reverse
direction is selected by button A, and stop is selected by button B. Save the file as
'bai_720_dc_motor_up_dw_stop_touch'.