Demo entry 6318944

大大

   

Submitted by anonymous on Nov 07, 2016 at 15:49
Language: C. Code size: 2.9 kB.

/******************************/
/*        ²½½øµç»ú¿ØÖÆ        */
/******************************/
#include <reg51.h>
#define uint unsigned int 
#define uchar unsigned char 
#define tim -20000

#define LCD_COM	 0  // Command
#define LCD_DAT  1  // Data
sbit LcdRS=P1^7;
sbit LcdRW=P1^6;
sbit LcdEN=P1^5;


void time(uint t)
{
  while(t--);
}

void LCD_WRITE(uchar x,bit WS)
{
  P3=x; 
  LcdRW=0;  LcdRS=WS;
  LcdEN=1;  time(100);  LcdEN=0;
}

void LCD_Initial()
{
  LCD_WRITE(0x38,LCD_COM);   time(200);
  LCD_WRITE(0x38,LCD_COM);   time(200);
  LCD_WRITE(0x01,LCD_COM);   time(200);
  LCD_WRITE(0x06,LCD_COM);   time(200);
  LCD_WRITE(0x0c,LCD_COM);   time(200);
}

void GotoXY(uchar x,uchar y)
{
  uchar code table[4]={0x00,0x40,0x10,0x50};
  LCD_WRITE(0x80+table[x]+y, LCD_COM); 
}

void PutCh(uchar m)
{
  LCD_WRITE(m,LCD_DAT);
}

void Print(uchar *str)
{
  while(*str!=0)
  {
	PutCh(*str);
	str++;
  }
}

void PutUint(int s)
{
  uchar str[10];
  str[0]=(s/100)%10+'0';
  str[1]=(s/10)%10+'0';
  str[2]=(s/1)%10+'0';
  str[3]=0;	str[4]=0;
  Print(str);
}


code uchar tab[]={1,3,2,6,4,12,8,9};
int set=0,step;
sbit SA=P1^0;
sbit SB=P1^1;
sbit SC=P1^2;
sbit SD=P1^3;

/*******************************/
void Time0() interrupt 1
{
  static int m=1;
  static char i=0;
  TH0=tim>>8; TL0=tim&0xff; 
  if(set!=m)
  {
    if(set>m) {i++; m++; }
	  else {i--;	m--;}
    if(i<0) i=7;
    if(i>7) i=0;
    SA=(tab[i]&1);
    SB=(tab[i]&2);
    SC=(tab[i]&4);
    SD=(tab[i]&8);
  }
  step=m;
}

uchar Getkey()
{
  uchar const tab[4]={1,2,4,8};
  uchar const tran[16]={7,4,1,10,8,5,2,0,9,6,3,11,12,13,14,15};
  uchar i,m,key=0xff;
  for(i=0;i<4;i++)
  {
    P2=~tab[i];
    m=(~P2)>>4;
	switch(m)
	{
	  case 1: key=i+0; break;
	  case 2: key=i+4; break;
	  case 4: key=i+8; break;
	  case 8: key=i+12; break;
	}
  }
  if(key!=0xff) key=tran[key];
  return key;
}

void main()
{ 
  uchar key;
  int num;
  IE=0x82;  TMOD=0X01;
  TH0=tim>>8; TL0=tim&0xff; TR0=1;
  time(5000);
  P1=0xf1; LCD_Initial();
  GotoXY(0,0); Print("Step Motor      ");
  GotoXY(1,0); Print("Set=    Step=   ");
  while(1)
  {
	time(10000);
	GotoXY(0,13); PutUint(num);
	GotoXY(1,4); PutUint(set);
	GotoXY(1,13); PutUint(step);
	key=Getkey();
    if(key!=255)
	{
      switch(key)	  
	  {
	    case 0: num=(num*10)%1000;  break;
	    case 1: num=(num*10)%1000+1;  break;
	    case 2: num=(num*10)%1000+2;  break;
	    case 3: num=(num*10)%1000+3;  break;
	    case 4: num=(num*10)%1000+4;  break;
	    case 5: num=(num*10)%1000+5;  break;
	    case 6: num=(num*10)%1000+6;  break;
	    case 7: num=(num*10)%1000+7;  break;
	    case 8: num=(num*10)%1000+8;  break;
	    case 9: num=(num*10)%1000+9;  break;
	    case 10: num=0;    break;
	    case 11: if(num<=359) set=num;  break;
	  }
	  time(20000);
	}
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).