Microcontroller › 8051 › 8051 program
- This topic has 0 replies, 1 voice, and was last updated 12 years, 5 months ago by Gaurav.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
April 19, 2012 at 12:46 pm #1487GauravParticipant
any one can arrenge this program i want to use only 5 keys and replace motor relay with LM293 D IC
#include<reg51.h>#include <string.h>sbit rs = P3^1; // rs pin of LCDsbit en = P3^0; // en pin of LCDsbit rw = P3^2; // rw pin of LCDsbit b = P0^7; // busy flagsbit led1=P2^0; // run indicatorsbit led2=P2^1; // stop indicatorsbit led3=P2^2; // clockwise direction indicatorsbit led4=P2^3; // anticlockwise direction indicatorsbit PWM=P2^4; // PWM outputsbit RL1=P2^5; // relay 1 pinsbit RL2=P2^6; // relay 2 pinunsigned int x=10; // ontimeunsigned int y=10; // offtimeunsigned int m=0; // modeunsigned int d=0; // directionunsigned int t=100; // timeunsigned int r=0; // run flagvoid start(void); // function initilizationvoid 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 flagrs = 0; // select command registerrw = 0; // write enableP0 = a; // send byte to LCDen = 1; // apply strobe pulseen = 0;}void writedata(unsigned char b) // send data to LCD{busy(); // check busy flagrs = 1; // select data registerrw = 0; // write enableP0 = b; // write enableen = 1; // send byte to LCDen = 0; // apply strobe pulse}void busy() // check busy flag of LCD{en = 0; // disable displayP0 = 0xFF; // P0 as inputrs = 0; // select command registerrw = 1; // read enablewhile(b==1) // if busy bit is 1{en=0; // remain withine loopen=1;}en=0;}void writestr(unsigned char *s) // send string message to LCD{unsigned char l,i;l = strlen(s); // get length of stringfor(i=0;i<l;i++){writedata(*s); // till the length of strings++; // send characters one by one}}void start() // start rotating motor{if(m==0) // for m=0 start continuous mode{RL1=0; // switch on RL1r=1; // set run flagP1=0xFF; // send all 1’s to P1while(P1==0xFF) // till no key is pressed{led1=1; // indication on run LEDPWM=1; // send high logic to PWM pindelay(x); // on time delayled1=0;PWM=0; // now send low logic to PWMdelay(y); // off time delay}}else if(m==1) // for m=1 start reversible mode{r=1; // set run flagP1=0xFF; // send all 1’s to P1while(P1==0xFF) // till no key is pressed{led1=1; // run LED=1PWM=1; // send high on PWM pinRL2=1; // select one directionRL1=0; // switch on RL1time(t); // wait for desired timeRL1=1; // switch off RL1led1=0; // run LED=0;time(20); // wait for 1 secled1=1; // again run LED=1RL2=0; // select other directionRL1=0; // switch on RL1time(t); // wait for desire timeRL1=1; // switch off RL1led1=0; // run LED=0time(20); // wait for 1 sec}PWM=0;}else if(m==2) // for m=2 start jogging mode{r=0; // reset run flagPWM=1; // send high on PWM pinRL1=0; // switch on RL1time(20); // wait for 1 secRL1=1; // switch off RL1PWM=0; // send low on PWM pin}}void direction() // alter the direction{keydly(); // key debounce delayd++; // increment countif((d%2)==0) // check for even or odd{led3=1; // indicate on LEDsled4=0;RL2=1; // switch ON / OFF RL2}else{led3=0;led4=1;RL2=0;}}void mode() // change mode of rotation{keydly(); // key debounce delaywritecmd(0x80); // display message on first line first columnm++; // increment countif(m=) m=0; // if it is 3 reset itif(m==0) writestr(“mode:continuous “); // otherwise display modeelse if(m==1) writestr(“mode:reversible “);else if(m==2) writestr(“mode:jogging “);}void incspeed() // increase speed{int z;keydly(); // key debouncewritecmd(0xC0); // select second line on LCDif(y<15) // if not max pulse width{x–;y++; // increase it convert it in toz=y-5+0x30; // 1 to 10 scale and ASCIIwritestr(“speed: “); // diaplay speed on LCDwritedata(z);writestr(” “);}else if(y==15) writestr(“max speed: 10 “); // if max width display message}void decspeed() // increase speed{int w;writecmd(0xC0); // key debouncekeydly();if(x<15) // if not minimum width{x++;y–; // decrease itw=y-5+0x30; // do same as abovewritestr(“speed: “);writedata(w);writestr(” “);}else if(x==15) writestr(“min speed: 1 “); // if min width display message}void inctime() // increase time{int p;keydly(); // key debounce delaywritecmd(0xC0);if(t<180) // if not max time{t+=20; // increase it by 1 secp=t/20;p=p+0x30; // convert it in to ASCIIwritestr(“time: “); // display itwritedata(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 delaywritecmd(0xC0);if(t>20) // if not min time{t-=20; // decrease itq=t/20;q=q+0x30; // do same as abovewritestr(“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 1TH1 = 0x3C; // to generate 50 ms delayTR1 = 1; // start timerfor(k=0;k<=c;k++) // rotate loop in multiples of 20{while(TF1==0); // wait till timer overflowTF1 = 0; // reset the flagTL1 = 0xAF; // reload itTH1 = 0x3C;}TR1 = 0; // stop timer}void delay(unsigned int c1) // change time in micro seconds{int a;TH0=0x9B; // select timer 0TL0=0x9B; // to generate 100 micro second delayTR0=1; // start timerfor(a=0;a<c1;a++) // rotate loop between 5 to 15{while(TF0==0); // wait until timer overflowTF0=0; // reset the flag}TR0=0; // stop timer}void main(){TMOD=0x12; // timer1 in 16 bit timer, timer 0 in 8 bit auto reload modeP2=0xE0; // LEDs off, relays OFFP0=0x00; // P0, P3 output portsP3=0x00;writecmd(0x3C); // initilize LCDwritecmd(0x0E);writecmd(0x01);writecmd(0x84); // display messagewritestr(“DC Motor”); // DC motor controller inwritecmd(0xC3); // center of LCDwritestr(“Controller”);time(50); // wait for 2.5 secwritecmd(0x80);writestr(“mode:continuous “); // display current mode and speedwritecmd(0xC0);writestr(“speed: 5 “);led3=1; // stop LED onled2=1; // clockwise directionagin:P1=0xFF; // P1 as input portwhile(P1==0xFF); // wait until any key pressloop:switch(P1){case 0xFE: // for first keykeydly(); // key debounceled2=0; // stop LED offstart(); // sart rotating motorbreak;case 0xFD: // for second keykeydly(); // key debouncer=0; // run flag resetwritecmd(0x01);writestr(“motor stop”); // display messageled2=1; // stop LED onPWM=0; // low logic to PWM pinRL1=1; // relay1 offbreak;case 0xFB: // for third keymode(); // select modeif(r==1) start(); // jump to start if run flag is setbreak;case 0xF7: // for fourth keydirection(); // change directionif(r==1) start(); // jump to start if run flag is setbreak;case 0xEF: // for fifth keyincspeed(); // increase speedif(r==1) start(); // jump to start if run flag is setbreak;case 0xDF: // for sixth keydecspeed(); // decrease speedif(r==1) start(); // jump to start if run flag is setbreak;case 0xBF: // for seventh keyinctime(); // increase timeif(r==1) start(); // jump to start if run flag is setbreak;case 0x7F: // for eigth keydectime(); // decrease timeif(r==1) start(); // jump to start if run flag is setbreak;}if(r==1) goto loop; // if run flag is set jump of key detectelse goto agin; // if not jump to again} -
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.