步进电机控制代码:
c#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code FFW[] =
{
0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09
};
uchar code REV[] =
{
0x09, 0x08, 0x0c, 0x04, 0x06, 0x02, 0x03, 0x01
};
sbit K1 = P3 ^ 0;
sbit K2 = P3 ^ 1;
sbit K3 = P3 ^ 2;
void DelayMS( uint ms )
{
uchar i;
while ( ms-- )
{
for ( i = 0; i < 120; i++ )
;
}
}
void SETP_MOTOR_FFW( uchar n )
{
uchar i, j;
for ( i = 0; i < 5 * n; i++ )
{
for ( j = 0; j < 8; j++ )
{
if ( K3 == 0 )
break;
P1 = FFW[j];
DelayMS( 25 );
}
}
}
void SETP_MOTOR_REV( uchar n )
{
uchar i, j;
for ( i = 0; i < 5 * n; i++ )
{
for ( j = 0; j < 8; j++ )
{
if ( K3 == 0 )
break;
P1 = REV[j];
DelayMS( 25 );
}
}
}
void main()
{
uchar N = 3;
while ( 1 )
{
if ( K1 == 0 )
{
P0 = 0xfe;
SETP_MOTOR_FFW( N );
if ( K3 == 0 )
break;
}else if ( K2 == 0 )
{
P0 = 0xfd;
SETP_MOTOR_REV( N );
if ( K3 == 0 )
break;
}else {
P0 = 0xfb;
P1 = 0x03;
}
}
}
效果:
直流电机:
c#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit K1 = P3 ^ 0;
sbit K2 = P3 ^ 1;
sbit K3 = P3 ^ 2;
sbit LED1 = P0 ^ 0;
sbit LED2 = P0 ^ 1;
sbit LED3 = P0 ^ 2;
sbit MA = P1 ^ 0;
sbit MB = P1 ^ 1;
void main( void )
{
LED1 = 1;
LED2 = 1;
LED3 = 0;
while ( 1 )
{
if ( K1 == 0 )
{
while ( K1 == 0 )
;
LED1 = 0;
LED2 = 1;
LED3 = 1;
MA = 0;
MB = 1;
}
if ( K2 == 0 )
{
while ( K1 == 0 )
;
LED1 = 1;
LED2 = 0;
LED3 = 1;
MA = 1;
MB = 0;
}
if ( K3 == 0 )
{
while ( K1 == 0 )
;
LED1 = 1;
LED2 = 1;
LED3 = 0;
MA = 0;
MB = 0;
}
}
}
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!