本帖最后由 wjb711 于 2013-8-19 20:59 编辑
参考文档
http://www.codelast.com/?p=5232
所需工具 步进电机一个, 步进驱动板一个, 如图(树老大的店有售, 本人不是托, 本人用的系从树老大的店采购)
友情给个树老大的广告:步进电机购买地址
http://raspi.taobao.com/category ... cene=taobao_shop#bd
步进电机的接法
驱动板的接法如图
口述如下
int1->gpio0
int2->gpio1
int3->gpio2
int4->gpio3
电源要接5v和接地
程序如下(抄别人的)
/* moto.c
* A program to control a stepper motor through the GPIO on Raspberry Pi.
*
* Author: Darran Zhang
*/
#include <wiringPi.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define CLOCKWISE 1
#define COUNTER_CLOCKWISE 2
void delayMS(int x);
void rotate(int* pins, int direction);
int main(int argc,char* argv[]) {
if (argc < 4) {
printf("Usage example: ./motor 0 1 2 3 \n");
return 1;
}
/* number of the pins which connected to the stepper motor driver board */
int pinA = atoi(argv[1]);
int pinB = atoi(argv[2]);
int pinC = atoi(argv[3]);
int pinD = atoi(argv[4]);
int pins[4] = {pinA, pinB, pinC, pinD};
if (-1 == wiringPiSetup()) {
printf("Setup wiringPi failed!");
return 1;
}
/* set mode to output */
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
delayMS(50); // wait for a stable status
for (int i = 0; i < 500; i++) {
rotate(pins, CLOCKWISE);
}
return 0;
}
/* Suspend execution for x milliseconds intervals.
* @param ms Milliseconds to sleep.
*/
void delayMS(int x) {
usleep(x * 1000);
}
/* Rotate the motor.
* @param pins A pointer which points to the pins number array.
* @param direction CLOCKWISE for clockwise rotation, COUNTER_CLOCKWISE for counter clockwise rotation.
*/
void rotate(int* pins, int direction) {
for (int i = 0; i < 4; i++) {
if (CLOCKWISE == direction) {
for (int j = 0; j < 4; j++) {
if (j == i) {
digitalWrite(pins[3 - j], 1); // output a high level
} else {
digitalWrite(pins[3 - j], 0); // output a low level
}
}
} else if (COUNTER_CLOCKWISE == direction) {
for (int j = 0; j < 4; j++) {
if (j == i) {
digitalWrite(pins[j], 1); // output a high level
} else {
digitalWrite(pins[j], 0); // output a low level
}
}
}
delayMS(4);
}
}
然后编译
g++ motor.c -o motor -lwiringPi
最后运行
./motor 0 1 2 3
马达就转起来了