PWM speed control of DC motor using MSP430 Launchpad and L293D
In the previous article we learned how to control a dc brushed motor using MSP430G2553 and L293D motor driver chip,the main focus in that article was about starting and controlling the direction of motors.The motors were allowed to be rotated at their maximum speed.In this article we are going to implement a simple PWM (Pulse Width Modulation) based speed control for DC brushed motors using MSP430 Launchpad.
Here i am using a Motor Control Booster Pack for MSP430G2553 Launchpad called Robot Shield
The Full details of the Motor Control/RS485 communication Booster Pack for MSP430 Launchpad can be found here.
All the C source files and Circuits used in this tutorial can be downloaded from our GitHub Page.
Please use the codes in the " MSP430-Booster-Pack-PWM-Speed-Control " Folder in the Repository.
If you are new to Github Check this article to download code .
Pulse Width Modulation using MSP430
PWM is a scheme for varying the power provided to the load by changing the ON/OFF time of the waveform applied to the load.In this method we will be applying a square wave to the Load (here DC) motor and by controlling the ON time (time in which waveform is high),we will able to control the average voltage applied to the motor and hence its speed.
As the ON time is increased the average voltage applied to the motor also increases hence the speed of the motor increases.When the ON time is reduced the average voltage applied to the motor also get reduced and the speed goes down.
Here we will be using the L293D motor driver chip present in the MSP430 Motor Control Shield to control the DC motors.To control the speed of the motors connected to L293D we will be applying PWM signals to the enable pins 1&2_EN and 3&4_EN.
The pins P2.0 and P2.6 of MSP430G2553 are connected to L293D enable pins 1&2_EN and 3&4_EN respectively (highlighted in red).The connections are shown in the circuit diagram below.
#pragma vector = TIMER0_A1_VECTOR
The MSP430G2553 is clocked at 8MHz DCO clock by loading the constants into the Basic Clock System register (BCSCTL1) and DCO control register (DCOCTL).The Port2 pins required for controlling the L293D chip is intialized and then logic levels ( 1A,2A and 3A,4A) for controlling the direction of Right (MOTOR_R) and Left (MOTOR_L) motor are set .Please note that we are implementing Software PWM here.
Timer A is then configured to operate from a 125Khz clock.The interrupt for CCR1 is enabled and Timer A is started in Continuous mode.The value in CCR1 register is used to control the width of the pulse.When Timer A counts upto the value in CCR1,CCIFG is set and the code jumps to the Interrupt Service Routine (ISR) for CCR1 interrupt ( case 0x002).Inside the interrupt both P2.0 and P2.6 are set thus enabling the two motors.
P2.0 and P2.6 remains set till the Timer Overflows and the code jumps into the overflow section in the ISR (case 0x00A).Inside the ISR P2.0 and P2.6 are cleared thus disabling the motor.
Now the speed of the motor is controlled by adjusting the value in CCR1 register.When the value in CCR1 is increased to lets say 64,000 the duration of ON time is reduced thus reducing the effective voltage available to motor and thereby its speed.In the figure below you can see the effect of increasing CCR1 value (here CCR1 = 64000).You can also see the change in power by watching LED's D7 (LEFT) and D5 (RIGHT) on the MSP430 Motor Control Booster Pack,the LED's glow dimly.
Now when you reduce the CCR1 value to lets say 6000,you are increasing the ON time and thus increasing the speed of the motor as illustrated by the figure below.The Led's D7 (LEFT) and D5 (RIGHT) in this case will glow brightly signifying an increase in effective power.