Professional Documents
Culture Documents
Microcontroller-Based DC Motor Controller: KS Project Manual eFY June 2009
Microcontroller-Based DC Motor Controller: KS Project Manual eFY June 2009
otion control plays a vital role in industrial automation. Manufacturing plants in industries like chemical, pharmaceutical, plastic and textile, all require motion control. And it may be a flatbelt application, flow-control application or mixing of substances. Different types of motorsAC, DC, servo or stepperare used depending upon the application. Of these, DC motors are widely used because controlling a DC motor is somewhat easier than other kinds of motors. The motion of a DC motor is controlled using a DC drive. DC drive changes the speed and direction of motion of the motor. Some of the DC drives are just a rectifier with a series resistor that converts standard AC supply into DC and gives it to the motor through a switch and a series resistor to change the speed and direction of rotation of the motor. But many of the DC drives have an inbuilt microcontroller that provides programmable facilities, message display on LCD, precise control and also protection for motors. Using the DC drive you can program the motion of the motor, i.e., how it should rotate. Here are some of the features of this DC motor controller: 1. Controlled through microcontroller AT89C51 2. Message displayed on the LCD module
read/write (R/ W) pins, respectively. Contrast of the LCD is set by preset VR1. Port pins P1.0 through P1.7 are connected to switches S1 through S8 for performing the Fig. 3: Proposed panel arrangement for the DC motor controller various operations. 3. Start, Power-on reset signal for the miFunctions of stop and crocontroller is generated by the comDifferent Switches change of bination of capacitor C1 and resistor direction of R1. Switch S9 provides manual reset to Switch Function the motor the microcontroller. A 12MHz crystal S1 To start motor controlled provides the basic clock frequency to S2 To stop the motor by pushbutthe microcontroller. Capacitors C2 and S3 Change the mode ton switchC3 provide stability to the oscillator. S4 Change the direction es and inEA pin (pin 31) of the microcontroller dicated by S5 Increase speed is connected to 5V to enable internal LED access. Port pins P2.0 through P2.3 of S6 Decrease speed 4 . the microcontroller are used for LED S7 Increase time Changes indication of run, stop, clockwise and S8 Decrease time the running anti-clockwise rotation. Port pins P2.4 mode of the motor to continuous, rethrough P2.6 are connected to the inputs versible or jogging of inverters N3, N2 and N1 of 74LS04 5. Changes the speed of the motor (IC2). The output of inverter N3 is used 6. Time settings are possible for to trigger pin 2 of NE555 timer. forward and reverse running of the Timer NE555 is configured as a motor monostable and its time period is decided by preset VR2 and capacitor C4. circuit description When pin 2 of NE555 goes low, output Fig. 1 shows the circuit of the micropin 3 becomes high for the predetercontroller-based DC motor controller. mined period. At the heart of the DC motor controller The output of NE555 is connected is microcontroller AT89C51. Port pins to pole P of relay RL1. Normally-open P0.0 through P0.7 of the microcontrol(N/O) contacts of relay RL1 are conler are interfaced with data pins D0 nected to N/O1 and N/C2 contacts of through D7 of the LCD module, rerelay RL2. N/C1 and N/O2 contacts spectively. Port pins P3.0, P3.1 and P3.2 of RL2 are connected to ground. The control the LCD operation through outputs of inverters N2 and N1 drive enable (E), register-select (RS) and relays RL1 and RL2 with the help of
operation
The eight pushbutton switches are connected for eight different functions as shown in the table. When S1 is pressed, the microcontroller sends low logic to port pin P2.5. The high output of inverter N2 drives transistor T1 into saturation and relay RL1 energises. So the output of NE555 is fed to inputs IN1 and IN2 of L293D through both the contacts of relay RL2. Now at the same time, after RL1 energises, the microcontroller starts generating PWM signal on port pin P2.4, which is fed to trigger pin 2 of NE555 through inverter N3. The base frequency of the generated PWM signal is 500 Hz, which means the time period is 2 ms (2000s). The output pulse width varies from 500 s to 1500 s. The R-C time constant of the monostable multivibrator is kept slightly less then 500 s to generate exactly the same inverted PWM as is generated by the microcontroller. When switch S2 is pressed, port-pin P2.5 goes high and RL1 de-energises to stop the motor. When switch S3 is pressed, relay
Fig. 4: A single-side, actual-size PCB layout for the microcontroller-based DC motor controller
software
The program is written in C language and compiled using Keil Vision3 compiler. It is well commented and easy to understand. The program has three major sections: initialisation and key press, display and motor control. Initialisation and key press. It consists of the main function that initialises timers, ports, LCD module and LED indication and then waits for key press. When any key is pressed, the program calls that particular function. In the main function, first the ports are initialised as input/output, then the LCD is initialised and cleared. At power-on reset or manual reset, message DC motor controller is displayed on the LCD and LED1 through LED4 turn off. When start switch S1 is pressed, message Motor Start is displayed for a second, and then the mode and current speed are displayed. Run LED and clockwise-direction LED are on. Now the program waits for key press. When
any key is pressed, the program jumps to one of the functions (start, direction, mode, etc).
Resistors (all -watt, 5% carbon): R1 - 10-kilo-ohm R2-R5 - 2-kilo-ohm R6-R10, R12, R13 - 220-ohm R11 - 1-kilo-ohm VR1, VR2 - 10-kilo-ohm preset Capacitors: C1 C2, C3 C4, C7, C8 C5 C6 10F, 16V electrolytic 33pF ceramic disk 0.1F ceramic disk 0.01F ceramic disk 1000F, 25V electrolytic
Miscellaneous: X1 - 230V AC primary to 9V, 500mA secondary transformer XTAL - 12MHz crystal RL1 - 6V, 1C/O relay RL2 - 6V, 2C/O relay M - 6V DC motor S1-S9 - Push-to-on switch LCD module - 162 line - 14-pin bergstik SIP Connector (male & female)
mand byte to the LCD. It takes one argument byte and sends it to P0. 2. writedata function sends data byte to be displayed on the LCD. It also takes one argument byte and sends it to P0. 3. writestr function writes the whole string (message) on the LCD. It
takes pointer as an argument that points address of the first character of the string, then through the pointer it sends all the characters one by one to P0. 4. busy function checks the status of the busy flag of the LCD. If the flag is set, the LCD is not ready and the program remains within the loop. When the flag is reset, the LCD is ready and the program comes out of the loop. Motor control. It uses the following functions: 1. start function starts motor rotation in one of the modes (continuous, reversible or jogging). The mode is selected by mode-selection flag m. As explained earlier, in continuous mode the motor keeps rotating with the set speed. In reversible mode, the motor changes direction automatically after the set time. In jogging mode, the motor rotates for the set time, stops for a few seconds and then rotates. 2. stop function stops rotating the motor and displays the message on the LCD module. This is indicated by glowing of the stop LED. 3. direction function increments the counter by 1 every time and checks whether the count is even or odd. If the count is even, it selects clock-wise direction, and if the count is odd, it selects anticlockwise direction. This is also indicated on direction CLK and ACLK LEDs. 4. mode function changes modes in cyclic manner. It increases modeselection flag m every time. If m=0 it selects continuous mode, if m=1 it
selects reversible mode and if m=2 it selects jogging mode. If m=3 it is reset to 0 again and selects continuous mode and likewise. 5. incspeed function increases the width of the pulse by 100 s. The generated PWM is of 500 Hz. That means total time is 2 ms=2000 s. The width of the pulse is varied from 500 s to 1500 s in steps of 100 s. To display the speed, the variable is first converted into speed factor 1 to 9 and then into ASCII. 6. decspeed function is the same as incspeed but here the width of the pulse is decreased by 100 s. 7. inctime function increases the reversible time of the motor by one second. It increases the variable in multiples of 20. To display it on the LCD module, it is first divided by 20 and then converted into ASCII. 8. dectime function is the same as inctime but it decreases reversible time by one second. Delay. It uses the following functions: 1. keydly function generates a fix delay of around 50 ms for key debouncing. 2. delay function generates a variable delay by timer 0. The basic delay is of 100 s. It is rotated in loop from five to 15 times to generate minimum 500s and maximum 1500s delay. 3. time function again generates a variable delay by timer 1. The basic delay is of 50 ms. It is rotated in multiples of 20 from 20 to 180 to generate minimum 1second and maximum 9-second delay.
dcmd.c
#include<reg51.h> #include <string.h> sbit rs = P3^1; // rs pin of LCD sbit en = P3^0; // en pin of LCD sbit rw = P3^2; // rw pin of LCD sbit b = P0^7; // busy flag sbit led1=P2^0; // run indicator sbit led2=P2^1; // stop indicator sbit led3=P2^2; // clockwise direction indicator sbit led4=P2^3; // anticlockwise direction indicator sbit PWM=P2^4; // PWM output sbit RL1=P2^5; // relay 1 pin sbit RL2=P2^6; // relay 2 pin unsigned int x=10; ontime unsigned int y=10; offtime unsigned int m=0; // mode unsigned int d=0; // direction unsigned int t=100; // time unsigned int r=0; // run flag void start(void); // function initilization void mode(void); void direction(void); void incspeed(void); void decspeed(void); void inctime(void); void dectime(void); void time(unsigned int); void delay(unsigned int); // // void keydly(void); void busy(void); void writecmd(unsigned char a) // send command to LCD { busy(); // check busy flag rs = 0; // select command register rw = 0; // write enable P0 = a; // send byte to LCD en = 1; // apply strobe pulse en = 0;
time(t); // wait for desired time // check busy flag of RL1=1; led1=0; // switch off RL1 // run LED=0;
en = 0; // disable display P0 = 0xFF; // P0 as input rs = 0; // select command register rw = 1; // read enable while(b==1) // if busy bit is 1 { en=0; // remain withine loop en=1; } en=0;
time(20); // wait for 1 sec led1=1; led3=0; led4=1; RL2=0; RL1=0; // select other direction // switch on RL1 // again run LED=1
time(t); // wait for desire time RL1=1; led1=0; // switch off RL1 // run LED=0 }
} void writestr(unsigned char *s) // send string message to LCD { unsigned char l,i; l = strlen(s); // get length of string for(i=0;i<l;i++) { writedata(*s); // till the length of string s++; // send characters one
} else if(m==2) // for m=2 start jogging mode { r=1; // reset run flag
by one
} } void start() // start rotating motor { if(m==0) // for m=0 start continuous mode { RL1=0; // switch on RL1 r=1; // set run flag P1=0xFF; // send all 1s to P1 while(P1==0xFF) // till no key is pressed { led1=1; // indication on run LED PWM=1; // send high logic to PWM pin delay(x); // on time delay PWM=0; // now send low logic to PWM delay(y); // off time delay } } else if(m==1) // for m=1 start reversible mode { r=1; // set run flag P1=0xFF; // send all 1s to P1 while(P1==0xFF) pressed // till no { key is
P1=0xFF; // send all 1s to P1 while(P1==0xFF) pressed led1=1; PWM=1; RL1=0; // send high on PWM pin // switch on RL1 // till no { key is
time(t); // wait for 1 sec RL1=1; PWM=0; led1=0; time(20); } } // switch off RL1 // send low on PWM pin
} void direction() // alter the direction { keydly(); // key debounce delay d++; // increment count if((d%2)==0) // check for even or odd { led3=1; // indicate on LEDs led4=0; RL2=1; // switch ON / OFF RL2 } else {
//
//
for(a=0;a<c1;a++) // rotate loop between 5 to 15 { while(TF0==0); // wait until timer overflow TF0=0; // reset the flag } TR0=0; // stop timer } void main() { TMOD=0x12; // timer1 in 16 bit,timer 0 in 8 bit auto reload mode P2=0xE0; // LEDs off, relays OFF P0=0x00; // P0, P3 output ports P3=0x00; writecmd(0x3C); // initilize LCD writecmd(0x0E); writecmd(0x01); writecmd(0x84); // display message writestr(DC Motor); // DC motor controller in writecmd(0xC3); // center of LCD writestr(Controller); agin:P1=0xFF; // P1 as input port while(P1==0xFF); // wait until any key press loop:switch(P1) { case 0xFE: // for first key keydly(); // key debounce writecmd(0x01); writestr(motor start); time(50); // wait for 2.5 sec writecmd(0x80); writestr(mode:continuous );// display current mode and speed writecmd(0xC0); writestr(speed: 5 ); led1=1; // Run LED ON led2=0; // stop LED OFF led3=1; // clockwise direction ON led4=0; // anticlockwise direction OFF start(); // sart rotating motor break; case 0xFD: