#include <REGX51.H>
//dieu khien dong co theo kieu 2 buoc
//dieu khien dong co quay trai roi quay phai roi lap lai nhu vay
unsigned char M1[]={0x88,0x44,0x22,0x11};
unsigned char M2[]={0x11,0x22,0x44,0x88};
void khoitao(void) //ham tao xung
{
TMOD=0x02;
EA=0;
TH0=0xFF; //tao xung tan so 2KHz
TL0=0x1A;
ET0=1;
EA=1;
TR0=1;
}
void tre(unsigned int ms) //tao tre dong co buoc
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<125;j++);
}
unsigned char dem=0,xung;
unsigned char phantramxung;
void timer() interrupt 1 //ngat timer 0
{
TR0=0;
TF0=0;
dem++;
if(dem<phantramxung) P2=0x03;
else P2=0x00;
if(dem==10)dem=0;
TR0=1;
}
char tangtoc(void){ //tang toc DC
if(P1_0==0)
{while(P1_0==0)
{;
}
phantramxung++;
if(phantramxung>=9)phantramxung=10;
}
return phantramxung;
}
char giamtoc(void){ //giam toc DC
if(P1_1==0)
{while(P1_1==0){;}
phantramxung--;
if(phantramxung<=1)phantramxung=1;
}
return phantramxung;
}
char quaydc(void){ // cho motor DC quay
if(P1_2==0)
{while(P1_2==0){;}
P1_6=1;
P1_7=0;
phantramxung=4;
}
return phantramxung;
}
char dungdc(void){ // dung dong co DC
if(P1_3==0)
{while(P1_3==0)
{;
}
P1_7=1;
P1_6=1;
phantramxung=0;
}
return phantramxung;
}
void quaynguoc(void)
{
int j,x=3;
while(x--)
{
for(j=0;j<4;j++)
{
P3=M2[j];
tre(2);
}
}
}
void quaythuan(void)
{
int j,x=3;
while(x--)
{
for(j=0;j<4;j++)
{
P3=M1[j];
tre(2);
}
}
}
unsigned char n;
char chonchedo(void){
if(P1_4==0)n=1;
switch(n)
{
case 0 :{break;}
case 1 :{while(1){ quaythuan();
tre(50);
quaythuan();
tre(50);
quaynguoc();
tre(50);}
break;}
}
return(n);
}
void main(void){
khoitao();
while(1){
chonchedo();
tangtoc();
giamtoc();
quaydc();
dungdc();
}
}
//dieu khien dong co theo kieu 2 buoc
//dieu khien dong co quay trai roi quay phai roi lap lai nhu vay
unsigned char M1[]={0x88,0x44,0x22,0x11};
unsigned char M2[]={0x11,0x22,0x44,0x88};
void khoitao(void) //ham tao xung
{
TMOD=0x02;
EA=0;
TH0=0xFF; //tao xung tan so 2KHz
TL0=0x1A;
ET0=1;
EA=1;
TR0=1;
}
void tre(unsigned int ms) //tao tre dong co buoc
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<125;j++);
}
unsigned char dem=0,xung;
unsigned char phantramxung;
void timer() interrupt 1 //ngat timer 0
{
TR0=0;
TF0=0;
dem++;
if(dem<phantramxung) P2=0x03;
else P2=0x00;
if(dem==10)dem=0;
TR0=1;
}
char tangtoc(void){ //tang toc DC
if(P1_0==0)
{while(P1_0==0)
{;
}
phantramxung++;
if(phantramxung>=9)phantramxung=10;
}
return phantramxung;
}
char giamtoc(void){ //giam toc DC
if(P1_1==0)
{while(P1_1==0){;}
phantramxung--;
if(phantramxung<=1)phantramxung=1;
}
return phantramxung;
}
char quaydc(void){ // cho motor DC quay
if(P1_2==0)
{while(P1_2==0){;}
P1_6=1;
P1_7=0;
phantramxung=4;
}
return phantramxung;
}
char dungdc(void){ // dung dong co DC
if(P1_3==0)
{while(P1_3==0)
{;
}
P1_7=1;
P1_6=1;
phantramxung=0;
}
return phantramxung;
}
void quaynguoc(void)
{
int j,x=3;
while(x--)
{
for(j=0;j<4;j++)
{
P3=M2[j];
tre(2);
}
}
}
void quaythuan(void)
{
int j,x=3;
while(x--)
{
for(j=0;j<4;j++)
{
P3=M1[j];
tre(2);
}
}
}
unsigned char n;
char chonchedo(void){
if(P1_4==0)n=1;
switch(n)
{
case 0 :{break;}
case 1 :{while(1){ quaythuan();
tre(50);
quaythuan();
tre(50);
quaynguoc();
tre(50);}
break;}
}
return(n);
}
void main(void){
khoitao();
while(1){
chonchedo();
tangtoc();
giamtoc();
quaydc();
dungdc();
}
}
Tiện xin hỏi mọi người về mạch điều khiển thực của code này , cám ơn nhiều lắm , mọi người giúp nhé, gợi ý thôi cũng được
Comment