直流电机按键控制

/*占空比,方波,直流电机高电平有效*/
#include
#define uchar unsigned char
#define uint unsigned int 
sbit wela=P2^0;
sbit dula=P2^1;
uchar code tablewe[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code tabledu[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Data[8];
sbit dc1=P1^1;   //定义电机信号输出端口1
sbit dc2=P1^2;	 //定义电机信号输出端口2
void delay(uchar t)
{
	while(--t);
}
void delayMS(uchar t)
{
	while(t--)
	{
		delay(245);
		delay(245);
	}
}
void init();
uchar keyscan();
void display(uchar f,uchar n);
void main()
{
	uchar num;
	init();
	Data[0]=0x5E; 	//‘d’
	Data[1]=0x39;   //'c'
	Data[2]=0x06;   //'1'
	
	Data[5]=0x5E;   //'d'
	Data[6]=0x39;   //'c'
	Data[7]=0x5b;   //'2'
	while(1)
	{
		num=keyscan();
		switch (num)
		{
			case 1:
				dc1=!dc1;	  //电平取反
				Data[0]=0x5E; 	//'d'
				Data[1]=0x39; 	//'c'
				Data[2]=0x06;	//'1'
				
				if(dc1)		  //高电平
				{
					 Data[5]=0x3F; 	 //'o'
					 Data[6]=0x54;   //'n'
					 Data[7]=0;
				}
				else
				{
					 Data[5]=0x3F; 	   //'o'
					 Data[6]=0x71; 	   //'f'
					 Data[7]=0x71; 	   //'f'
				}
				break;
			case 2:
				dc2=!dc2;		    //电平取反
				Data[0]=0x5E; 	    //'d'
				Data[1]=0x39; 		//'c'
				Data[2]=0x5b;		//'2'

				if(dc2)			   //高电平
				{
					 Data[5]=0x3F; 	    //'o'
					 Data[6]=0x54; 		//'n' 
				   	 Data[7]=0;
				}
				else
				{
					 Data[5]=0x3F; 	   //'o'
					 Data[6]=0x71; 	   //'f'
					 Data[7]=0x71;	   //'f'
				}
				break;
		}
	}
}
void init()		 //初始化定时器
{
	TMOD=0X01;
	EA=1;
	ET0=1;
	TR0=1;
}
uchar keyscan()		 //键盘扫描函数
{
	uchar value;
	if(P3!=0xff)
	{
		delayMS(10);
		if(P3!=0xff)
		{
			value=P3;
			while(P3!=0xff);
			switch(value)
			{
				case 0xfe:return 1;break;
				case 0xfd:return 2;break;
				case 0xfb:return 3;break;
				case 0xf7:return 4;break;
				case 0xef:return 5;break;
				case 0xdf:return 6;break;
				case 0xbf:return 7;break;
				case 0x7f:return 8;break;
				default :return 0;break;
			}
		}
	}
	return 0;
}
void display(uchar f,uchar n)	   //显示函数
{
	static uchar i;
	P0=0;
	dula=1;
	dula=0;

	P0=tablewe[i+f];
	wela=1;
	wela=0;

	P0=Data[i];
	dula=1;
	dula=0;

	i++;
	if(i==n)
		i=0;
}
void timer0() interrupt 1			 //定时器0中断
{
	TH0=(65536-2000)/256;
	TL0=(65536-2000)%256;

	display(0,8);
}

  

转载于:https://www.cnblogs.com/fengyepiaolei/archive/2011/11/18/2254590.html

你可能感兴趣的:(直流电机按键控制)