This whole program works fine with stepper rotating according to the kepad entry. But, it only takes one digit input. I need it so that it takes multiple inputs and runs when I press "*" or any other key i want to specify. It takes the input and starts running directly!
#include <stdio.h> //Define I/O Functions #include <reg51.h> //Define 8051 Registers #define DATA P2 //Define DATA to Port2
void lcd_cmd(unsigned char); //LCD Command Function 0void lcd_display(unsigned char); void lcdready(void);
int get_key(void);//LCD Display Function int read_switches(void); //KeyScan Function
void stepper(char); void stepper_rev(char);
void MSdelay(unsigned int); //DelayMs Function
sbit rs = P3^4; //Register Select sbit rw = P3^5; //LCD Read/Write sbit en = P3^6; //LCD Enable
sbit rowA=P1^0; sbit rowB=P1^1; sbit rowC=P1^2; sbit rowD=P1^3; sbit C1=P1^4; sbit C2=P1^5; sbit C3=P1^6; sbit C4=P1^7; sbit busy=P2^7;
sbit p= P3^0; sbit q= P3^1; sbit r= P3^2; sbit s= P3^3;
sbit start=P1^7; sbit m=P3^7;
int i=0;
void main() { lcd_cmd(0x80); lcd_cmd(0x01); lcd_cmd(0x0E);
while(1) { int a; m=0; a=get_key1(); lcd_cmd(0x80); lcd_cmd(0x06); lcd_display(a | 0x30); if(start==1) { stepper(a); } else stepper_rev(a); } } void stepper(char a) { //char x; // for(x=a;x<0;x++) m=1; do { // a=a-48; p=1;q=1;r=0;s=0;//P3 = 0x03; //0011 MSdelay(50000); p=0;q=1;r=1;s=0;//P3 = 0x06; //0110 MSdelay(50000); p=0;q=0;r=1;s=1;//P3 = 0x0C; //1100 MSdelay(50000); p=1;q=0;r=0;s=1;//P3 = 0x09; //1001 MSdelay(50000); a--; // lcd_cmd(0xc0); //lcd_display(a | 0x30); }while(a); m=0; } void stepper_rev(char a) { //char x; // for(x=a;x<0;x++) m=1; do { // a=a-48; if(start!=0) {return;} p=1;q=0;r=0;s=1;//P3 = 0x09; //1001 MSdelay(50000); p=0;q=0;r=1;s=1;//P3 = 0x0C; //1100 MSdelay(50000); p=0;q=1;r=1;s=0;//P3 = 0x06; //0110 MSdelay(50000); p=1;q=1;r=0;s=0;//P3 = 0x03; //0011 MSdelay(50000); a--; lcd_cmd(0xc0); lcd_display(a | 0x30); }while(a); m=0; } int read_switches(void) { rowA=0; rowB=1; rowC=1; rowD=1; if(C1==0){MSdelay(50); while(C1==0); return 1;} if(C2==0){ MSdelay(50); while(C2==0); return 2;} if(C3==0) {MSdelay(50); while(C3==0); return 3;} //if(C4==0) {MSdelay(50); while(C4==0); return '/';}
rowA=1; rowB=0; rowC=1; rowD=1; if(C1==0){ MSdelay(50); while(C1==0); return 4;} if(C2==0) {MSdelay(50); while(C2==0); return 5;} if(C3==0) {MSdelay(50); while(C3==0); return 6;} //if(C4==0) {MSdelay(50); while(C4==0); return '*';}
rowA=1; rowB=1; rowC=0; rowD=1; if(C1==0){ MSdelay(50); while(C1==0); return 7;} if(C2==0){ MSdelay(50); while(C2==0); return 8;} if(C3==0) {MSdelay(50); while(C3==0); return 9;} //if(C4==0) {MSdelay(50); while(C4==0); return '-';}
rowA=1; rowB=1; rowC=1; rowD=0; if(C1==0) {MSdelay(50); while(C1==0); return 'c';} if(C2==0) {MSdelay(50); while(C2==0); return 0;} if(C3==0){MSdelay(50); while(C3==0); return 'a';} //if(C4==0) {MSdelay(50); while(C4==0); return '+';} // return; }
int get_key1(void) { char key='n'; while(key=='n') {key= read_switches();} return key; } //char get_key2(void) //{ // char key='n'; // while(key=='n') // {key= read_switches();} // return key; //}
void MSdelay(unsigned int itime) { unsigned int i,j; for(i=0;i<itime;i++); for(j=0;j<1275;j++); }
void lcd_cmd(unsigned char cmnd) { lcdready(); DATA = cmnd; rs = 0; //RS:Register Select rw = 0; //RW:Read/Write en = 1; //LCD Enable MSdelay(250); en= 0; return; }
void lcd_display(unsigned char dat) { lcdready(); DATA = dat; rs = 1; //RS:Register Select rw = 0; //RW:Read/Write en= 1; MSdelay(1); en= 0; return; } void lcdready() { busy = 1; //make the busy pin at input rs = 0; rw = 1; while(busy==1){ //wait here for busy flag en = 0; //strobe the enable pin MSdelay(1); en = 1; } }
So you don't want the stepper motor to change speed instantly you press a key?
Isn't it then obvious that your code that handle keys shouldn't instantly update the same variable that the stepper motor code looks at?
Isolate the two code blocks. Let the keyboard code retrieve key after key after key in a sequence until you either press an invalid sequence (then try to flag some error) or you finally press the "activate" key (which in your case would be the '*') in which case it's time to finally assign the new speed/direction information to the variables that the drive code cares for.