51单片机驱动步进电机调速 有大神能教教我么

2019-07-15 13:33发布

图片1.png

软件我用C语言 照着书弄得 不知道为什么不行
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int

sbit mode=P2^0;
sbit direction=P2^1;
sbit clk=P2^2;
sbit key1=P2^3;
sbit key2=P2^4;
sbit key3=P2^5;
sbit key4=P2^6;
sbit lcars=P2^7;
sbit lcden=P3^0;

uchar code state[3][16]={"speed(n/min):120","direction:ccw"."direction:cw"};

char countkey;
unit count_int;
unit code zhouqi[]={2500,125,62,12};
unit code speed[]={3,60,120,600};

void delayms(unit x)
{
unit i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}

void write_com(uchar com)
{
lcdrs=0;
P1=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}

void write_data(unchar date)
{
lccdrs=1;
P1=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}

void init_lcddisply()
{
uchar unm;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+0x00);
for(num=0;num<16;num++)
{
write_date(state[2][num]);
delayms(5);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_date(state[0][num]);
delayms(5);
}
}

  void write_ddat_to_lcd(unchar add,unit dat)
{
unchar gewei,shiwei,baiwei;
gewei=dat%10;
shiwei=dat%100/10;
baiwei=dat/100;
write_com(0x80+add);
write_com(0x30+baiwei);
write_com(0x30+shiwei);
write_com(0x30+gewei);
}

void keyscan()
{
uchar i;
if(key1=0)
{
  delayms(5);
  if(key1=0)
  {
   while(!key1);
    countkey++;
   if(countkey>3)
    countkey=0;
write_dat_to_lcd(0x4d,speed[countkey];
}
}
if(key==2)
{
delayms(5);
if(key2==0)
{
  while(!key2);
  countkey--;
  if(countkey<0)
  countkey=3;
  write_dat_to_lcd(0x4d,speed[countkey];
  }
  }

  if(key3==0)
  {
  delayms(5);
  if(key3==0)
  {
  while(!key3);
  direction=1;
  write_com(0x80+0x000;
  for(i=0;i<16;i++)
  {
   write_date(state[2]);
   delayms(2);
   }
   }
   }

  if(key4==0)
  {
  delayms(5);
  if(key4==0)
  {
  while(!key4);
  direction=0;
  write_com(0x80+0x000;
  for(i=0;i<16;i++)
  {
   write_date(state[1]);
   delayms(2);
   }
   }
   }
   }

   void main()
   {
   countkey=1;
   mode=1;
   direction=1;
   init_lcddisplay();
   TMOD=0x02;
   TH0=255-200;
   TL0=255-200;
   EA=1;
   ET0=1;
   TR0=1;
   while(1)
   {
   keyscan();
   }
   }

   void timer0_int(1) interrupt 1
   {
   TR0=0;
   count_int++;
   if(count_int>zhouqi[countkey])
   {
   clk=~clk;
   count_int=0;
   }
   TR0=1;
   }


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。