Sunteți pe pagina 1din 6

KS Project Manual eFY june 2009

Microcontroller-based dc Motor controller

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

Fig. 1: Circuit of the microcontroller-based DC motor controller

KS Project Manual eFY june 2009


transistors T1 and T2, respectively. D1 and D2 act as free-wheeling diodes. Poles P1 and P2 of RL2 are connected to IN1 and IN2 pins of motor driver L293D. OUT1 and OUT2 of L293D drive motor M. Fig. 2 shows the power supply circuit. The 230V AC mains is stepped down by transformer X1 to deliver the secondary output of 9V, 500 mA. The transformer output is rectified by a full-wave bridge rectifier comprising diodes D3 through D6, filtered by capacitor C6 and then regulated by ICs 7805 (IC5) and 7806 (IC6). Capacitors C7 and C8 bypass the ripples present in the regulated 5V and 6V power supplies. LED5 acts as a power-on indicator and resistor R10 limits the current through LED5. The proposed panel arrangement for the microcontroller-based DC motor controller is shown in Fig. 3. An actual-size, single-side PCB for the microcontroller-based DC motor controller is shown in Fig. 4 and its component layout in Fig. 5.

Fig. 2: Circuit of the power supply

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

KS Project Manual eFY june 2009


RL2 energises. Pin IN1 of motor driver L293D receives the PWM signal and pin IN2 connects to ground. As a result, the motor rotates in one direction (say, clockwise). When switch S4 is pressed again, relay RL2 de-energises. Pin IN2 of motor driver L293D receives the PWM signal and pin IN1 connects to ground. The motor now rotates in opposite direction (anti-clockwise). When switch S3 is pressed, different modes are selected in cyclic manner as given below: 1. Continuous mode. The motor rotates continuously with the set speed in either direction 2. Reversible mode. The motor reverses automatically after the set time 3. Jogging mode. The motor rotates for the set time in either direction and then stops for a few seconds and again rotates for the set time. It is also called pulse rotation Switches S5 and S6 are used to set the speed of the motor, either in increasing order or decreasing order, in continuous mode only. Switches S7 and S8 are used to set the time either in increasing order or decreasing order.

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

Fig. 5: Component layout for the PCB

any key is pressed, the program jumps to one of the functions (start, direction, mode, etc).

Display. It uses the following functions: 1. writecmd function sends com-

KS Project Manual eFY june 2009


Parts List
Semiconductors: IC1 IC2 IC3 IC4 IC5 IC6 T1, T2 D1-D6 LED1-LED5 AT89C51 microcontroller 74LS04 hex inverter NE555 timer L293D motor driver 7805, 5V regulator 7806, 6V regulator BC548 npn transistor 1N4007 rectifier diode 5mm LED

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;

} void writedata(unsigned char b) // send data to LCD { busy();

KS Project Manual eFY june 2009


// rs // rw // P0 // en // en // } void busy() LCD { check busy flag = 1; select data register = 0; write enable = b; write enable = 1; send byte to LCD = 0; apply strobe pulse led1=1; led3=1; led4=0; PWM=1; RL2=1; RL1=0; // send high on PWM pin // select one direction // switch on RL1 // run LED=1 } } void mode() // change mode of rotation { keydly(); // key debounce delay writecmd(0x80); // display message on first line first column m++; // increment count if(m==3) m=0; // if it is 3 reset it if(m==0) { writestr(mode:continuous ); // otherwise display mode time(15); } else if(m==1) {writestr(mode:reversible ); time(15); } else if(m==2) {writestr(mode:jogging ); time(15); } } void decspeed() // increase speed { int z; keydly(); // key debounce writecmd(0xC0); // select second line on LCD if(y<14) // if not max pulse width { x--; y++; // increase it convert it in to z=y-5+0x30; // 1 to 10 scale and ASCII writestr(speed: ); // diaplay speed on LCD writedata(z); writestr( ); } else if(y==14) writestr(min speed: 9 );// if max speed display message } void incspeed() // increase speed { int w; keydly(); writecmd(0xC0); // key debounce if(y>6) // if not minimum width { x++; y-- ; // decrease it w=y-5+0x30; // do same as above writestr(speed: ); writedata(w); writestr( ); } else if(y==6) writestr(max speed: 1 ); // if min speed display message } void inctime() increase time { int p; keydly(); key debounce delay writecmd(0xC0); if(t<180) if not max time { // led3=0; led4=1; RL2=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

time(20); // wait for 1 sec PWM=0;

} 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 {

//

//

KS Project Manual eFY june 2009


t+=20; // increase it by 1 sec p=t/20; p=p+0x30; // convert it in to ASCII writestr(time: ); // display it writedata(p); writestr( sec ); } else if(t==180) writestr(max time: 9 sec); // if max time display message } void dectime() // decrease time { int q; keydly(); // key debounce delay writecmd(0xC0); if(t>20) // if not min time { t-=20; // decrease it q=t/20; q=q+0x30; // do same as above writestr(time: ); writedata(q); writestr( sec ); } else if(t==20) writestr(min time: 1 sec); // if min time display message } void keydly() // key debounce delay { int a,b; for(a=0;a<50;a++) for(b=0;b<1000;b++); } void time(unsigned int c) // change time in seconds { int k; TL1 = 0xAF; // use timer 1 TH1 = 0x3C; // to generate 50 ms delay TR1 = 1; // start timer for(k=0;k<=c;k++) // rotate loop in multiples of 20 { while(TF1==0); // wait till timer overflow TF1 = 0; // reset the flag TL1 = 0xAF; // reload it TH1 = 0x3C; } TR1 = 0; // stop timer } void delay(unsigned int c1) / / c h a n g e time in micro seconds { int a; TH0=0x9B; select timer 0 TL0=0x9B; to generate 100 micro second delay TR0=1; start timer // // // // for second key keydly(); // key debounce r=0; // run flag reset writecmd(0x01); writestr(motor stop);// display message led1=0; // Run OFF led2=1; // stop LED ON led3=0; // clockwise direction OFF led4=0; // anticlockwise direction OFF PWM=0; // low logic to PWM pin RL1=1; // relay1 off break; case 0xFB: // for third key mode(); // select mode if(r==1) start(); // jump to start if run flag is set break; case 0xF7: // for fourth key direction(); // change direction if(r==1) start(); // jump to start if run flag is set break; case 0xEF: // for fifth key incspeed(); // increase speed if(r==1) start(); // jump to start if run flag is set break; case 0xDF: // for sixth key decspeed(); // decrease speed if(r==1) start(); // jump to start if run flag is set break; case 0xBF: // for seventh key inctime(); // increase time if(r==1) start(); // jump to start if run flag is set break; case 0x7F: // for eigth key dectime(); // decrease time if(r==1) start(); // jump to start if run flag is set break; } if(r==1) goto loop; // if run flag is set jump of key detect else goto agin; // if not jump to again }

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: