Вы находитесь на странице: 1из 8

#include<reg51.h> #include <string.

h> sbit rs = P3^1; sbit en = P3^0; sbit rw = P3^2; sbit b = P0^7; sbit led1=P2^0; sbit led2=P2^1; sbit led3=P2^2; sbit led4=P2^3; ator sbit PWM=P2^4; sbit RL1=P2^5; sbit RL2=P2^6; unsigned unsigned unsigned unsigned unsigned unsigned void void void void void void void void void void void int int int int int int x=10; y=10; m=0; d=0; t=100; r=0;

// // // // // // // //

rs pin of LCD en pin of LCD rw pin of LCD busy flag run indicator stop indicator clockwise direction indicator anticlockwise direction indic

// PWM output // relay 1 pin // relay 2 pin // // // // // // ontime offtime mode direction time run flag

start(void); mode(void); direction(void); incspeed(void); decspeed(void); inctime(void); dectime(void); time(unsigned int); delay(unsigned int); keydly(void); busy(void);

// function initilization

void writecmd(unsigned char a) { busy(); ag rs = 0; d register rw = 0; P0 = a; LCD en = 1; pulse en = 0; } void writedata(unsigned char b) { busy(); ag rs = 1; egister rw = 0; P0 = b; en = 1; LCD en = 0; pulse }

// send command to LCD // check busy fl // select comman // write enable // send byte to // apply strobe

// send data to LCD // check busy fl // select data r // write enable // write enable // send byte to // apply strobe

void busy() // check busy fl ag of LCD { en = 0; // disable displ ay P0 = 0xFF; // P0 as input rs = 0; // select comman d register rw = 1; // read enable while(b==1) // if busy bit i s 1 { en=0; // remain within e loop en=1; } en=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 st ring s++; // send characte rs one 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 1's to P1 while(P1==0xFF) // till no key is presse d { led1=1; // indication on run LED PWM=1; // send high log ic to PWM pin delay(x); // on time delay PWM=0; // now send low logic to PWM delay(y); // off time dela y } } else if(m==1) // for m=1 start reversi ble mode { r=1; // set run flag P1=0xFF; // send all 1's to P1 while(P1==0xFF) // till no key is presse

d { led1=1; led3=1; led4=0; PWM=1; PWM pin RL2=1; rection RL1=0; time(t); red time RL1=1; 1 led1=0; time(20); c led1=1; =1 led3=0; led4=1; RL2=0; direction RL1=0; time(t); re time RL1=1; 1 led1=0; time(20); c } PWM=0; } else if(m==2) mode { r=1; g P1=0xFF; all 1's to P1 while(P1==0xFF) d { led1=1; PWM=1; PWM pin RL1=0; time(t); c RL1=1; 1 PWM=0; WM pin led1=0; time(20); } // send low on P // switch off RL // switch on RL1 // wait for 1 se // send high on // till no key is presse // send // reset run fla // for m=2 start jogging // run LED=0 // wait for 1 se // switch off RL // switch on RL1 // wait for desi // select other // again run LED // run LED=0; // wait for 1 se // switch off RL // switch on RL1 // wait for desi // select one di // run LED=1 // send high on

} } void direction() { keydly(); delay d++; nt if((d%2)==0) { led3=1; EDs led4=0; RL2=1; FF RL2 } else { led3=0; led4=1; RL2=0; } } void mode() e mode of rotation { keydly(); // key debounce delay writecmd(0x80); ay message on first line first column m++; // increment count if(m==3) m=0; is 3 reset it if(m==0) { writestr("mode:continuous "); 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++;

// alter the direction // key debounce // increment cou // check for even or odd // indicate on L // switch ON / O

// chang

// displ

// if it // otherwise display mode

// 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 "); 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 "); n width display message } void inctime() // increase time { int p; keydly(); // key debounce delay writecmd(0xC0); if(t<180) // if not max time { 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"); isplay message } void dectime() // decrease time { int q;

// if max width

// if mi

// if max time d

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 mi n 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 5 0 ms delay TR1 = 1; // start timer for(k=0;k<=c;k++) // rotate loop in multip les of 20 { while(TF1==0); // wait till timer overf low TF1 = 0; // reset the fla g TL1 = 0xAF; // reload it TH1 = 0x3C; } TR1 = 0; // stop timer } void delay(unsigned int c1) // change time in micro seconds { int a; TH0=0x9B; // select timer 0 TL0=0x9B; // to generate 1 00 micro second delay TR0=1; // start timer for(a=0;a<c1;a++) // rotate loop between 5 to 15 { while(TF0==0); // wait until timer over flow TF0=0; // reset the fla g } TR0=0; // stop timer

} void main() { TMOD=0x12; bit timer, timer 0 in 8 bit auto reload mode P2=0xE0; ays OFF P0=0x00; ports P3=0x00; writecmd(0x3C); writecmd(0x0E); writecmd(0x01); writecmd(0x84); writestr("DC Motor"); writecmd(0xC3); writestr("Controller"); agin:P1=0xFF; while(P1==0xFF); ess loop:switch(P1) { case 0xFE: keydly(); writecmd(0x01); writestr("motor start"); time(50); // wait for 2.5 sec writecmd(0x80); writestr("mode:continuous "); d writecmd(0xC0); writestr("speed: 5 led1=1; led2=0; led3=1; ection ON led4=0; direction OFF start(); motor break; case 0xFD: y keydly(); r=0; t writecmd(0x01); writestr("motor stop"); // display message led1=0; // Run OFF led2=1; // stop LED ON led3=0; // clockwise dir ection OFF led4=0; direction OFF PWM=0; PWM pin RL1=1; // relay1 off // low logic to // anticlockwise // key debounce // run flag rese // for second ke // sart rotating // anticlockwise "); // Run LED ON // stop LED OFF // clockwise dir // display current mode and spee

// timer1 in 16 // LEDs off, rel // P0, P3 output // initilize LCD // display message // DC motor controller in // center of LCD // P1 as input port // wait until any key pr

// for first key // key debounce

break; case 0xFB: mode(); if(r==1) start(); flag is set break; case 0xF7: y direction(); if(r==1) start(); flag is set break; case 0xEF: incspeed(); d if(r==1) start(); flag is set break; case 0xDF: decspeed(); d if(r==1) start(); flag is set break; case 0xBF: ey inctime(); if(r==1) start(); flag is set break; case 0x7F: dectime(); if(r==1) start(); flag is set break; } if(r==1) goto loop; mp of key detect else goto agin; }

// for third key // select mode // jump to start if run // for fourth ke // change direction // jump to start if run // for fifth key // increase spee // jump to start if run // for sixth key // decrease spee // jump to start if run // for seventh k // increase time // jump to start if run // for eigth key // decrease time // jump to start if run

// if run flag is set ju // if not jump to again

Вам также может понравиться