请各位大虾指点一下,综合A/D转换控制电机转速。当输入的模拟电压为0~2V时,电机转速缓慢;当输入模拟电压为2~3V时,电机中速旋转;当输入模拟电压为3~5V时电机高速旋转。
#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define p0 P0
unsigned char s;
long int ll;
uchar shu;
uchar a;
sbit CP=P0^0;//CP
sbit DIR=P0^1;//DR
sbit RL=P0^2;//RL
sbit LL=P0^3;
sbit CS=P2^0;
sbit RD1=P2^1;
sbit WR1=P2^2;
sbit EOC=P2^3;
void ADC();
void delay1(uchar c)
{
uchar x,y;;
for(x=c;x>0;x--)
for(y=5;y>0;y--);
}
void delay(uint tt)
{
uint x,y;
for (x=tt;x>0;x--)
for (y=110;y>0;y--);
}
void main(void)
{
s=1;
DIR=0;
while(1)
{
ADC();
}
}
void ADC(void)
{
WR1=1;
CS=0;
delay(2);
WR1=0;
CS=0;
delay(2);
WR1=1;
CS=0;
delay(2);
while(!EOC);
RD1=0;
CS=0;
delay(10);
shu=P3;
RD1=1;
if(shu<102) a=6;
if(shu>102&&shu<153) a=3;
if(shu>153) a=1;
if(s==1)
{
CP=0;
delay1(a);
CP=1;
delay1(a);
DIR=0;
if(LL==1)
{
delay(5);
if(LL==1)
{s=2;}
}
}
if(s==2)
{
CP=0;
delay1(a);
CP=1;
delay1(a);
DIR=1;
if(RL==1)
{
delay(5);
if(RL==1)
{s=1;}
}
}
} |