如何采用51单片机控制电机的旋转角度?

控制/MCU

1878人已加入

描述

typedefunsignedcharuchar;

typedefunsignedintuint;

typedefunsignedlongulong;

ucharcodebeatCode[8]={

0xE,0xC,0xD,0x9,0xB,0x3,0x7,0x6

};

ulongbeats=0;

ucharT0RH=0,T0RL=0;

voidturnMotor(ulongangle){

EA=0;

beats=angle*4076/360;

EA=1;

}

voidsetTimer0(uintms){

ulongtmp;

tmp=11059326/12;

tmp=tmp*ms/1000;

tmp=65536-tmp;

tmp+=28;

T0RL=tmp;

T0RH=tmp>>8;

}

voidinterruptTimer0()interrupt1{

staticucharindex=0;

TH0=T0RH;

TL0=T0RL;

if(beats){

uchartmp=P1;

tmp&=0xF0;

tmp|=beatCode[index];

P1=tmp;

index=++index&0x07;

beats--;

}

else{

P1|=0x0F;

}

}

voidmain(){

EA=1;

TMOD=0x01;

setTimer0(2);

TH0=T0RH;

TL0=T0RL;

ET0=1;

TR0=1;

turnMotor(2*360+180);

while(1);

}

下面是使用库函数循环左移实现的:

#include

#include

typedefunsignedcharuchar;

typedefunsignedintuint;

typedefunsignedlongulong;

ulongbeats=0;

ucharT0RH=0,T0RL=0;

voidturnMotor(ulongangle){

EA=0;

beats=angle*4076/360;

EA=1;

}

voidsetTimer0(uintms){

ulongtmp;

tmp=11059326/12;

tmp=tmp*ms/1000;

tmp=65536-tmp;

tmp+=28;

T0RL=tmp;

T0RH=tmp>>8;

}

voidinterruptTimer0()interrupt1{

staticucharbeatCode=0x33;

TH0=T0RH;

TL0=T0RL;

if(beats){

uchartmp=P1;

tmp&=0xF0;

beatCode=_crol_(beatCode,1);

tmp|=beatCode;

P1=tmp;

beats--;

}

else{

P1|=0x0F;

}

}

voidmain(){

EA=1;

TMOD=0x01;

setTimer0(2);

TH0=T0RH;

TL0=T0RL;

ET0=1;

TR0=1;

turnMotor(2*360+180);

while(1);

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分