Design of motor counter based on 51 single chip microcomputer

content

1. Project function

2. Simulation

3. Program design


Data download address: motor counter based on 51 single-chip microcomputer

1. Project function

Can measure the time of forward and reverse rotation of stepper motor

2. Simulation

When pressing forward, the stepper motor rotates forward, and the left digital tube starts timing.

When the reverse is pressed, the stepper motor reverses, and the right number starts timing.

3. Program design

#include <REGX51.H>
#define uchar unsigned char	 //宏定义
#define uint unsigned int
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit key5=P3^4;


#define duan P0
sbit w1=P2^0;
sbit w2=P2^1;
sbit w3=P2^2;
sbit w4=P2^3;
sbit w5=P2^4;
sbit w6=P2^5;
sbit w7=P2^6;
sbit w8=P2^7;

uint m_turn_on_off = 0;
uint pramindex = 0;
uint pramindex2 = 0;
uint counts = 0;
uint counts2 = 0;
unsigned int f=60;
unsigned char b=0;
unsigned char x;
unsigned char mada[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09}; 
/*****************数码管变量定义*******************/
uchar code wei1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};	//数码管显示段码

/********************************************************
函数名称:void Delay(unsigned int num)
函数作用:US延时函数
参数说明:
********************************************************/
void Delay(unsigned int num)
{
  while( --num ) ;
}
/********************************************************
函数名称:void display(uchar a,uchar b,uchar c,uchar d)
函数作用:正常显示
参数说明:a:十位,b:个位,c:十分位,d:百分位
********************************************************/
void display(uint dat,uint dat2)
{
	uchar a,b,c,d;
	uchar a1,b1,c1,d1;
	a = dat / 1000;
	b = dat % 1000 / 100;
	c = dat % 100 / 10;
	d = dat %10;
	a1 = 10;
	b1 = dat2 % 1000 / 100;
	c1 = dat2 % 100 / 10;
	d1 = dat2 %10;

		duan=wei1[a];	 //不带小数点	
		w1=0;
		Delay(150);
		w1=1;

		duan=wei1[b];	 
		w2=0;	
		Delay(150);
		w2=1;

		duan=wei1[c];	 //不带小数点
		w3=0;	
		Delay(150);
		w3=1;
		
		duan=wei1[d];	 //不带小数点
		w4=0;	
		Delay(150);
		w4=1;
	
	
		duan=wei1[a1];	 //不带小数点	
		w5=0;
		Delay(150);
		w5=1;

		duan=wei1[b1];	 
		w6=0;	
		Delay(150);
		w6=1;

		duan=wei1[c1];	 //不带小数点
		w7=0;	
		Delay(150);
		w7=1;
		
		duan=wei1[d1];	 //不带小数点
		w8=0;	
		Delay(150);
		w8=1;
	
}

void delay_ms(unsigned int ms)
{
 unsigned char i;
 while(ms--)
 for(i=0;i<123;i++);
}
key_scan()
{
   if(key1==0)
   {
     b=0;
   } 
   if(key2==0)
   {
     b=1;
   }
   if(key3==0)
   {
     b=2;
   }
   if(key4==0)
   { 
			if(f>28)
			{	
				f=f-8;
			}
  }
  if(key5==0)
  {
		if(f<90)
	  { 
			f=f+5;
	  }	 
   }
 
}
void  main()
{ 
  TMOD=0X01;//0000 0001 定时器0 模式1 16位 定时
  TH0=(65536-50000)/256;//高八位  初始赋值	10000us=10ms
  TL0=(65536-50000)%256;//低八位  初始赋值  10000us=10ms
	IT0=1;  //下降沿触发
  EX0=1; //允许外部中断0
	TR0=1;//定时器0 开始计数
	ET0=1;//开始定时器0 中断
	EA=1;
 while(1)
{
	
	if(b==0)
	{
		m_turn_on_off = 0;
		
			for(x=0;x<8;x++)
			{
				P1=mada[x];
				
				delay_ms(80);
			}
			pramindex++;	
			if(pramindex == 1)
			{
				counts ++;
				pramindex = 0;
			}
	 }
    if(b==1)
    {
			m_turn_on_off = 1;
			for(x=8;x>0;x--)
			{
				P1=mada[x-1];
				delay_ms(80);
       }
			pramindex2++;	
			if(pramindex2 == 1)
			{
				counts2 ++;
				pramindex2 = 0;
			}
     }

  }
 } 

 

Guess you like

Origin blog.csdn.net/qq_35654286/article/details/124232322