用自带驱动控制一个步进电机
时间:10-02
整理:3721RD
点击:
最近忙活了一下硬件结构,现在继续开始开发。为了节省时间和精力,也是自己驱动渣渣,所以直接用自带的驱动了。主要用到了sprintf(),system()函数以及usleep()函数。
sprintf()函数原型为int sprintf(char *str, char * format [, argument, ...]),用于将格式化的数据写入字符串,这里是将需要在终端输入的指令写入tmp中。
system()原型为int system(const char * string),会调用fork()产生子进程,由子进程来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。也就是将上面转换指令为tmp放到后台运行,并能返回现在的程序。
usleep()原型为void usleep(int micro_seconds),功能为把进程挂起一段时间, 单位是微秒(百万分之一秒)。这个没什么说的。
然后将GPIO的初始化、释放、方向设置以及压值设置分别做成独立函数,要记得用malloc()后进行free(),之后进行调用就很方便了。
这是程序在板子上的工作打印内容。
用这块板子控制步进电机很稳,现象如下所示。
http://v.youku.com/v_show/id_XMTgzMjMxMjYzNg==.html
程序如下所示。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #define GPIO_OUTPUT 1 //Set the gpio derection output
- #define GPIO_INPUT 0 //Set the gpio derection input
- #define GPIO_HIGH 1 //Set the gpio output high
- #define GPIO_LOW 0 //Set the gpio input low
- /**
- * @brief: init_GPIO function
- * Describe : GPIO_x_y init
- * @Param: x:
- * @Param: y:
- */
- void init_GPIO(int x, int y)
- {
- char* tmp;
- int gpio;
-
- tmp = (unsigned char*)malloc(64);
- gpio = 32 * x + y;
- sprintf(tmp,"echo %d > /sys/class/gpio/export",gpio);
- printf("GPIO%d_%d init.\n",x, y);
- system(tmp);
- free(tmp);
- }
- /**
- * @brief: release_GPIO function
- * Describe : to release GPIO_x_y
- * @Param: x:
- * @Param: y:
- */
- void release_GPIO(int x, int y)
- {
- char* tmp;
- int gpio;
-
- tmp = (unsigned char*)malloc(64);
- gpio = 32 * x + y;
- sprintf(tmp,"echo %d > /sys/class/gpio/unexport",gpio);
- printf("GPIO%d_%d release.\n",x, y);
- system(tmp);
- free(tmp);
- }
- /**
- * @brief: direction_GPIO function
- * Describe : to release GPIO_x_y
- * @Param: x:
- * @Param: y:
- * @Param: n: direction 1:out 0:in
- */
- void direction_GPIO(int x, int y, int n)
- {
- char* tmp;
- int gpio;
-
- tmp = (unsigned char*)malloc(64);
- gpio = 32 * x + y;
- if(n==1){
- sprintf(tmp,"echo 'out' > /sys/class/gpio/gpio%d/direction",gpio);
- printf("The direction of GPIO%d_%d is output.\n",x, y);
- }
-
- if(n==0){
- sprintf(tmp,"echo 'in' > /sys/class/gpio/gpio%d/direction",gpio);
- printf("The direction of GPIO%d_%d is input.\n",x, y);
- }
- system(tmp);
- free(tmp);
- }
- /**
- * @brief: value_GPIO function
- * Describe : when the direction of the
- * gpio is out, set it's value
- * @Param: x:
- * @Param: y:
- * @Param: n: out value 1:High 0:low
- */
- void value_GPIO(int x, int y, int n)
- {
- char* tmp;
- int gpio;
-
- tmp = (unsigned char*)malloc(64);
- gpio = 32 * x + y;
-
- sprintf(tmp,"echo %d > /sys/class/gpio/gpio%d/value", n, gpio);
- printf("The value of GPIO%d_%d is %d.\n",x, y, n);
- system(tmp);
- free(tmp);
- }
- //gpio_fd = open("/sys/class/gpio/gpio12/value",O_RDONLY);
- /**
- * @brief: main function
- * @Param: argc: number of parameters
- * @Param: argv: parameters list
- */
- int main(int argc, char *argv[])
- {
- int i =1000;
- printf("hello world ! \n");
- init_GPIO(5,9);
- init_GPIO(5,7);
- direction_GPIO(5,9,GPIO_OUTPUT);
- direction_GPIO(5,7,GPIO_OUTPUT);
- value_GPIO(5,7,GPIO_HIGH);
- while(i>0){
- value_GPIO(5,9,GPIO_HIGH);
- usleep(10000); //10ms
- value_GPIO(5,9,GPIO_LOW);
- usleep(10000); //10ms
- i--;
- }
- release_GPIO(5,9);
- release_GPIO(5,7);
- return 1;
- }