想做一个光控步进电机LCD显示,做出来发现问题了,求指教
时间:10-02
整理:3721RD
点击:
出现问题:接通电源,电机只是转动一下然后不动了。还有LCD显示一下就变暗了,改变周围光强亮暗,LCD数值显示没有变化,这是为什么啊?
程序如下:
#include "reg52.h"
void delay(unsigned int t);
//Motor
unsigned char get_data;
unsigned char temp;
unsigned int compare=0;
unsigned int dji;
unsigned char code tab1[]="welcome on!";
unsigned char code tab2[]="light:";
unsigned char code tab3[]="mcd";
sbit F1 = P1^4;
sbit F2 = P1^5;
sbit F3 = P1^6;
sbit F4 = P1^7;
sbit key= P3^2;
sbit cs = P3^5;
sbit wr = P3^6;
sbit rd = P3^7;
sbit RS = P3^5;
sbit RW = P3^6;
sbit E = P3^4;
unsigned char code FFW[8]={0x90,0x10,0x30,0x20,0x60,0x40,0xc0,0x80}; //正转
unsigned char code FFZ[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90}; //反转
unsigned int K;
/**********************************************************************
* *
* 步进电机驱动 *
* *
***********************************************************************/
void motor_ffw()
{
unsigned char i,j;
for (j=100*(dji-compare); j>0; j--) //转1*n圈
{
for(i=0;i<8;i++)
{
P1=FFW[i];
delay(1);
}
}
}
void motor_ffz()
{
unsigned char i,j;
for (j=100*(compare-dji); j>0; j--) //转1*n圈
{
for(i=0;i<8;i++)
{
P1=FFZ[i];
delay(1); //调速
}
}
}
/******************************************************
*
* 延时程序
*
********************************************************/
void delay(unsigned int t)
{
unsigned int k;
while(t--)
{
for(k=0; k<115; k++) //1ms基准延迟程序
{ }
}
}
/*写指令*/
writecom(unsigned char com)
{
RS=0;
RW=0;
P0=com;
E=0;
delay(1);
E=1;
delay(1);
E=0;
}
/*写数据*/
writedata(unsigned char date)
{
RS=1;
RW=0;
P0=date;
E=0;
delay(1);
E=1;
delay(1);
E=0;
}
/*写数字*/
void writecount(unsigned int Data)
{
switch(Data)
{ case 0: writedata(0x30);break;
case 1: writedata(0x31);break;
case 2: writedata(0x32);break;
case 3: writedata(0x33);break;
case 4: writedata(0x34);break;
case 5: writedata(0x35);break;
case 6: writedata(0x36);break;
case 7: writedata(0x37);break;
case 8: writedata(0x38);break;
case 9: writedata(0x39);break;
}
}
/*------------------------------------------------
ADC0804驱动函数
------------------------------------------------*/
void AD_init()
{
cs=1;
wr=1;
rd=1;
}
void AD_start()
{
P1=0xff;
cs=0;
wr=0;
wr=1;
cs=1;
}
unsigned int AD_read()
{
cs=0;
rd=0;
delay(1);
get_data=P1;
rd=1;
cs=1;
return(get_data);
}
/*------------------------------------------------
显示函数
------------------------------------------------*/
/*显示函数,这里只是控制显示的光强值,显示为4位10进制数,*/
void disp(unsigned int a)
{
unsigned int c;
a*=4; /*将转换所得模拟信号(最大为255)转成10进制数,因为10K光敏电阻对应的光强约为1cd,即1000mcd,为方便显示,故将模拟信号乘以4,估算以作参考*/
writecom(0xc0+7);
writecount(a/1000);
writecom(0xc0+8);
writecount(a%1000/100);
writecom(0xc0+9);
writecount(a%100/10);
writecom(0xc0+10);
writecount(a%10);
writecom(0xc0+11);
for(c=0;c<3;c++)
writedata(tab3[c]);
}
/*------------------------------------------------
判断函数
------------------------------------------------*/
pduan(unsigned int a)
{
unsigned int x;
if(a<51) x=0;
else if(a<102) x=1;
else if(a<153) x=2;
else if(a<204) x=3;
else x=4;
return(x);
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main()
{
unsigned int i,j;
writecom(0x38);
writecom(0x0c);
writecom(0x06);
writecom(0x80+1);
for(i=0;i<11;i++)
writedata(tab1[i]);
writecom(0xc0+1);
for(j=0;j<6;j++)
writedata(tab2[j]);
AD_init();
while(1)
{
AD_start();
temp=AD_read();
disp(temp);
dji=pduan(get_data);
if(key) /*判断是否执行暂停状态*/
{
if(dji>compare) /*判断步进机为正转还是反转*/
{
motor_ffw();
compare=dji;
}
else if(dji<compare)
{
motor_ffz();
compare=dji;
}
else compare=dji;
};
};
}
程序如下:
#include "reg52.h"
void delay(unsigned int t);
//Motor
unsigned char get_data;
unsigned char temp;
unsigned int compare=0;
unsigned int dji;
unsigned char code tab1[]="welcome on!";
unsigned char code tab2[]="light:";
unsigned char code tab3[]="mcd";
sbit F1 = P1^4;
sbit F2 = P1^5;
sbit F3 = P1^6;
sbit F4 = P1^7;
sbit key= P3^2;
sbit cs = P3^5;
sbit wr = P3^6;
sbit rd = P3^7;
sbit RS = P3^5;
sbit RW = P3^6;
sbit E = P3^4;
unsigned char code FFW[8]={0x90,0x10,0x30,0x20,0x60,0x40,0xc0,0x80}; //正转
unsigned char code FFZ[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90}; //反转
unsigned int K;
/**********************************************************************
* *
* 步进电机驱动 *
* *
***********************************************************************/
void motor_ffw()
{
unsigned char i,j;
for (j=100*(dji-compare); j>0; j--) //转1*n圈
{
for(i=0;i<8;i++)
{
P1=FFW[i];
delay(1);
}
}
}
void motor_ffz()
{
unsigned char i,j;
for (j=100*(compare-dji); j>0; j--) //转1*n圈
{
for(i=0;i<8;i++)
{
P1=FFZ[i];
delay(1); //调速
}
}
}
/******************************************************
*
* 延时程序
*
********************************************************/
void delay(unsigned int t)
{
unsigned int k;
while(t--)
{
for(k=0; k<115; k++) //1ms基准延迟程序
{ }
}
}
/*写指令*/
writecom(unsigned char com)
{
RS=0;
RW=0;
P0=com;
E=0;
delay(1);
E=1;
delay(1);
E=0;
}
/*写数据*/
writedata(unsigned char date)
{
RS=1;
RW=0;
P0=date;
E=0;
delay(1);
E=1;
delay(1);
E=0;
}
/*写数字*/
void writecount(unsigned int Data)
{
switch(Data)
{ case 0: writedata(0x30);break;
case 1: writedata(0x31);break;
case 2: writedata(0x32);break;
case 3: writedata(0x33);break;
case 4: writedata(0x34);break;
case 5: writedata(0x35);break;
case 6: writedata(0x36);break;
case 7: writedata(0x37);break;
case 8: writedata(0x38);break;
case 9: writedata(0x39);break;
}
}
/*------------------------------------------------
ADC0804驱动函数
------------------------------------------------*/
void AD_init()
{
cs=1;
wr=1;
rd=1;
}
void AD_start()
{
P1=0xff;
cs=0;
wr=0;
wr=1;
cs=1;
}
unsigned int AD_read()
{
cs=0;
rd=0;
delay(1);
get_data=P1;
rd=1;
cs=1;
return(get_data);
}
/*------------------------------------------------
显示函数
------------------------------------------------*/
/*显示函数,这里只是控制显示的光强值,显示为4位10进制数,*/
void disp(unsigned int a)
{
unsigned int c;
a*=4; /*将转换所得模拟信号(最大为255)转成10进制数,因为10K光敏电阻对应的光强约为1cd,即1000mcd,为方便显示,故将模拟信号乘以4,估算以作参考*/
writecom(0xc0+7);
writecount(a/1000);
writecom(0xc0+8);
writecount(a%1000/100);
writecom(0xc0+9);
writecount(a%100/10);
writecom(0xc0+10);
writecount(a%10);
writecom(0xc0+11);
for(c=0;c<3;c++)
writedata(tab3[c]);
}
/*------------------------------------------------
判断函数
------------------------------------------------*/
pduan(unsigned int a)
{
unsigned int x;
if(a<51) x=0;
else if(a<102) x=1;
else if(a<153) x=2;
else if(a<204) x=3;
else x=4;
return(x);
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main()
{
unsigned int i,j;
writecom(0x38);
writecom(0x0c);
writecom(0x06);
writecom(0x80+1);
for(i=0;i<11;i++)
writedata(tab1[i]);
writecom(0xc0+1);
for(j=0;j<6;j++)
writedata(tab2[j]);
AD_init();
while(1)
{
AD_start();
temp=AD_read();
disp(temp);
dji=pduan(get_data);
if(key) /*判断是否执行暂停状态*/
{
if(dji>compare) /*判断步进机为正转还是反转*/
{
motor_ffw();
compare=dji;
}
else if(dji<compare)
{
motor_ffz();
compare=dji;
}
else compare=dji;
};
};
}