51单片机编写程序控制步进电机正反转

注意连接A连接P0.5,B连接P0.4,C连接P0.3,D连接P0.2,
步进电机提高显示,步进角5.625,意思是一个脉冲走的角度,没算减速比64:1
视频里把减速器拿下去了,然后88=64个脉冲正好一周,但是下边是算上减速比了
所以for循环里乘以了64
/
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
uchar code step_table[8]={
0xdc,0xcc,0xec,0xe4,0xf4,0xf0,0xf8,0xd8};
uchar code bujinz[]={0xdc,0xcc,0xec,0xe4,0xf4,0xf0,0xf8,0xd8};
uchar code bujinf[]={0xd8,0xf8,0xf0,0xf4,0xe4,0xec,0xcc3,0xdc};
//uchar code step_table[]={
// 0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09};
////00000001 00000010 00000110 00000100
//11111110 11111101 11111001 11111011
//void delay (uint t)
//{
// uint x,y;
// for(x=0;x // for(y=0;y<110;y++);
//}
void delay (uint t)
{
while(t–);
}

void main()
{
uint i,j;
P0=00; //关闭数码管显示
delay(500);
dula=1;
delay(500);
dula=0;
while(1)
{

	for(j=8*64;j>0;j--)
	{
		for(i=0;i<8;i++)	//发现i<9,10,20都能转8最好	  这里<=可用
		{
			P0=step_table[i];
			delay(200);
		}
	
	}

	for(j=8*64;j>0;j--)
	{
		for(i=8;i>0;i--)  //这里当i定义成uint 时i>=0不行  因为i=0后再减1就是255table[255]不转了
		{
			P0=step_table[i];//解决方法就是把uint改为 int i 或者定义uint 正反分开 都是加	 
			delay(100);		 //8拍少点也可以转 加点其他也可以 但看加多少次没用的 0xff
		}
		
	
	}
	while(1);

}

}

你可能感兴趣的:(51单片机)