控制步进电机正传和反转
通过侦测GPIO4的高低电平来实现
脚本如下
/********** motor2.c ***************/
#include <wiringPi.h>
#include <stdlib.h>
int main()
{
if( wiringPiSetup() == -1 )
exit( 1 );
pinMode( 0, OUTPUT );
pinMode( 1, OUTPUT );
pinMode( 2, OUTPUT );
pinMode( 3, OUTPUT );
pinMode( 4, INPUT );
//增加GPIO4为判断引脚
int x=3;
int y=1;
for (;;)
{
if (digitalRead(4) == 0)
//如果是低电平,默认还是原来的转法
{
digitalWrite( 0, HIGH );
delay( x );
digitalWrite( 0, LOW );
delay( y );
digitalWrite( 1, HIGH );
delay( x );
digitalWrite( 1, LOW );
delay( y );
digitalWrite( 2, HIGH );
delay( x );
digitalWrite( 2, LOW );
delay( y );
digitalWrite( 3, HIGH );
delay( x );
digitalWrite( 3, LOW );
delay( y );
}
else
//如果是高电平, 就反转, 把顺序倒转过来,3,2,1,0 分别接高电平
{
digitalWrite( 3, HIGH );
delay( x );
digitalWrite( 3, LOW );
delay( y );
digitalWrite( 2, HIGH );
delay( x );
digitalWrite( 2, LOW );
delay( y );
digitalWrite( 1, HIGH );
delay( x );
digitalWrite( 1, LOW );
delay( y );
digitalWrite( 0, HIGH );
delay( x );
digitalWrite( 0, LOW );
delay( y );
}
}
}
/********** motor2.c ***************/
实验结果是 如果GPIO4未接, 顺时针旋转
如果GPIO4接高3.3伏,逆时针旋转。 |