CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 23232|回复: 12

[教程]Cubieboard通过GPIO驱动步进电机--完结

[复制链接]
发表于 2013-2-16 17:50:01 | 显示全部楼层 |阅读模式
本帖最后由 WindLand 于 2013-4-7 17:38 编辑

2013-4-3更新:由于标准GPIO库(gpio-sunxi.ko)已经可用了,不建议再使用sun4i-gpio.ko这个驱动。此文仅作思路上的参考,不用照搬
This driver is not recommended now. For the new gpio driver  "gpio-sunxi" is coming. So don't follow this.

1.首先你要编译并加载sun4i-gpio.ko驱动,如何编译:参考 <如何编译单个驱动文件>

2.修改script.bin配置,在末尾加上gpio配置段,如何修改:参考<如何修改script.bin | script.fex>
添加的内容如下:
  1. [gpio_para]
  2. gpio_used = 1
  3. gpio_num = 67
  4. gpio_pin_1 = port:PG03<1><default><default><1>
  5. gpio_pin_2 = port:PB19<1><default><default><1>
  6. gpio_pin_3 = port:PB18<1><default><default><1>
  7. gpio_pin_4 = port:PG06<1><default><default><1>
  8. gpio_pin_5 = port:PG05<1><default><default><1>
  9. gpio_pin_6 = port:PG04<1><default><default><1>
  10. gpio_pin_7 = port:PG01<1><default><default><1>
  11. gpio_pin_8 = port:PG02<1><default><default><1>
  12. gpio_pin_9 = port:PG00<1><default><default><1>
  13. gpio_pin_10 = port:PH14<1><default><default><1>
  14. gpio_pin_11 = port:PH15<1><default><default><1>
  15. gpio_pin_12 = port:PI06<1><default><default><1>
  16. gpio_pin_13 = port:PI05<1><default><default><1>
  17. gpio_pin_14 = port:PI04<1><default><default><1>
  18. gpio_pin_15 = port:PG11<1><default><default><1>
  19. gpio_pin_16 = port:PG10<1><default><default><1>
  20. gpio_pin_17 = port:PG09<1><default><default><1>
  21. gpio_pin_18 = port:PG08<1><default><default><1>
  22. gpio_pin_19 = port:PG07<1><default><default><1>
  23. gpio_pin_20 = port:PE08<1><default><default><1>
  24. gpio_pin_21 = port:PE07<1><default><default><1>
  25. gpio_pin_22 = port:PE06<1><default><default><1>
  26. gpio_pin_23 = port:PE05<1><default><default><1>
  27. gpio_pin_24 = port:PE04<1><default><default><1>
  28. gpio_pin_25 = port:PI09<1><default><default><1>
  29. gpio_pin_26 = port:PI08<1><default><default><1>
  30. gpio_pin_27 = port:PI07<1><default><default><1>
  31. gpio_pin_28 = port:PD04<1><default><default><1>
  32. gpio_pin_29 = port:PD03<1><default><default><1>
  33. gpio_pin_30 = port:PD02<1><default><default><1>
  34. gpio_pin_31 = port:PD01<1><default><default><1>
  35. gpio_pin_32 = port:PD00<1><default><default><1>
  36. gpio_pin_33 = port:PE11<1><default><default><1>
  37. gpio_pin_34 = port:PE10<1><default><default><1>
  38. gpio_pin_35 = port:PE09<1><default><default><1>
  39. gpio_pin_36 = port:PD12<1><default><default><1>
  40. gpio_pin_37 = port:PD11<1><default><default><1>
  41. gpio_pin_38 = port:PD10<1><default><default><1>
  42. gpio_pin_39 = port:PD09<1><default><default><1>
  43. gpio_pin_40 = port:PD08<1><default><default><1>
  44. gpio_pin_41 = port:PD07<1><default><default><1>
  45. gpio_pin_42 = port:PD06<1><default><default><1>
  46. gpio_pin_43 = port:PD05<1><default><default><1>
  47. gpio_pin_44 = port:PD20<1><default><default><1>
  48. gpio_pin_45 = port:PD19<1><default><default><1>
  49. gpio_pin_46 = port:PD18<1><default><default><1>
  50. gpio_pin_47 = port:PD17<1><default><default><1>
  51. gpio_pin_48 = port:PD16<1><default><default><1>
  52. gpio_pin_49 = port:PD15<1><default><default><1>
  53. gpio_pin_50 = port:PD14<1><default><default><1>
  54. gpio_pin_51 = port:PD13<1><default><default><1>
  55. gpio_pin_52 = port:PB02<1><default><default><1>
  56. gpio_pin_53 = port:PD25<1><default><default><1>
  57. gpio_pin_54 = port:PD24<1><default><default><1>
  58. gpio_pin_55 = port:PD26<1><default><default><1>
  59. gpio_pin_56 = port:PD27<1><default><default><1>
  60. gpio_pin_57 = port:PD23<1><default><default><1>
  61. gpio_pin_58 = port:PD22<1><default><default><1>
  62. gpio_pin_59 = port:PD21<1><default><default><1>
  63. gpio_pin_60 = port:PI11<1><default><default><1>
  64. gpio_pin_61 = port:PI13<1><default><default><1>
  65. gpio_pin_62 = port:PI10<1><default><default><1>
  66. gpio_pin_63 = port:PI12<1><default><default><1>
  67. gpio_pin_64 = port:PB13<1><default><default><1>
  68. gpio_pin_65 = port:PB11<1><default><default><1>
  69. gpio_pin_66 = port:PB10<1><default><default><1>
  70. gpio_pin_67 = port:PH07<1><default><default><1>
复制代码
#注意,sun4i-gpio驱动不是一个标准的linux gpio驱动,无法即时转换输入输出模式,而是定义死在script.bin里的。这里我将67个针脚全都配置为输出模式了。比如:gpio_pin_67 = port:PH07<1><default><default><1> 中,红色的1表示将PH07针脚配置为输出模式,改为0则为输入模式,gpio_pin_数字 和 针脚名称 并没有特定的对应要求,只要一个数字对应一个针脚就可以,使用的时候我们是用针脚名,gpio_pin_数字 可以无视。
针脚名具体参考:http://linux-sunxi.org/Cubieboard#Expansion_ports

3.重启系统,查看/sys/devices/virtual/misc/sun4i-gpio/pin/下是否有67个针脚名,如果没有,那你使用命令:dmesg 查看是否有gpio驱动或者script配置的报错信息,再google解决.
未命名.jpg
如果你有led灯或是其他原件,可以参考<Cubieboard GPIO成功点亮LED>来进行测试

4.先说说控制步进电机的原理,网上已经有一大堆了,如果你完全不了解,可以google搜索"树莓派 步进电机"或"arduino 步进电机"。我这里就简单描述一下:除了步进电机,你还需要一块步进电机的驱动板,买电机的时候就一起买了吧。要让电机转动,只需要在某一时刻对驱动板的4个引脚输出3个低电平一个高电平,下一时刻把高电平往下一个针脚转移。
假设:驱动板a b c d 四条线 连接的 cb上的  1 2 3 4针脚,那么我们需要在t1时间输出电平为:1=>高 2=>低 3=>低 4=>低。
t1+n毫秒时,输出1=>低 2=>高 3=>低 4=>低。以此类推,4为高电平后,为通电一周,下次就再重复1为高电平...

接下来,我写了一些python代码。分为两部分:一是 对针脚电平控制的基础库,这样就能方便的用python对任意针脚进行电平控制;二是 调用基础库对电机进行控制的代码。代码下载见底部附件

5.下载代码之后,先参考确定你要使用哪几个针脚来控制电机,在上面67个中自己选4个。选好之后,把线接上,把针脚名字记好。除了4根控制线,驱动板还有vcc和gnd两根线供电,也接在板子上对应的针脚。比如我使用的控制针脚为'pg0', 'pb18', 'pb19', 'pg3'。针脚名和在板子上的位置还是参考:http://linux-sunxi.org/Cubieboard#Expansion_ports正负极我接的U15的1号和19号针脚

这里要吐槽一下,CB的GPIO针脚是2mm间距的,我用标准的2.45的杜邦线插上去相当困难,所以买线的时候要注意买2.0的杜邦线哦。

连接控制线的时候,注意顺序,驱动板上的4个接线口假设依次为a b c d,那么你给他通电的顺序一定要是 a b c d或者 d c b a ,所以要看好顺序,然后在下面修改针脚名的时候,依次排列。

解压之后,先修改motor_main.py 的第5行
#modify the tuple to the pins you used
motor_pins = ('pg0', 'pb18', 'pb19', 'pg3')
  #将这里的引脚名改为你已经连接了的引脚名,注意顺序
保存退出,最后执行
python motor_main.py

电机转了!!有木有!!!!!

下面上图:

1.jpg
虽然你看不出,但它的确在转。。

2.jpg
旁边的 卡王(带天线那位) 是打酱油的,请无视

3.jpg
来个特写,驱动板是必须有的

4.jpg
引脚接线

源码下载:
motor_gpio.zip (2.18 KB, 下载次数: 822)

回复

使用道具 举报

发表于 2013-2-16 21:07:20 | 显示全部楼层
支持啊,求代码
回复 支持 反对

使用道具 举报

发表于 2013-2-19 15:40:05 | 显示全部楼层
请问
gpio_pin_1 = portG03<1><default><default><1>

这里的几列分别代表啥意思?  
我看驱动代码里面对应了这个struct, 但还是不明白它是啥意思

typedef struct
{
        char  gpio_name[32];
        int port;
        int port_num;
        int mul_sel;
        int pull;
        int drv_level;
        int data;
} script_gpio_set_t;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-2-19 21:40:26 | 显示全部楼层
coderplay 发表于 2013-2-19 15:40
请问
gpio_pin_1 = portG03

第一个<1>我已经解释过了。
其他几列请参考
http://linux-sunxi.org/Fex_Guide#.5BTarget.5D

Description of gpio the GPIO configuration in the form:

Port: port + group serial number of features allocated> <internal resistance state> <drive capability> <output level state>
回复 支持 反对

使用道具 举报

发表于 2013-3-15 04:06:31 | 显示全部楼层
终于实现了用gpio控制关机了。
回复 支持 反对

使用道具 举报

发表于 2013-4-7 15:41:24 | 显示全部楼层
版主您好,我手边的开发板是A10处理器  板子和你图片上的不同!但是我看了几篇您发的帖子,咱们的源码还是很相似的!android4.0.4      kernel3.0.8     我的产品:sun4i_3g
我查看我的内核       lichee/linux-3.0/drivers/gpio 文件夹下,并没有找到
sun4i_gpio.c文件 或者   sunxi_gpio.c文件    所以找不到sun4i_gpio.ko文件
我的sys_config1.fex文件中唯独没有找到   [gpio_para]   这系列的参数
这几天都是无从下手,我的目的就是修改PH03  这个pin的高低电平
内核中需不要配置什么参数啊?如SUN4I_GPIO_UGLY=y   这个我不知道是那个!
回复 支持 反对

使用道具 举报

发表于 2013-5-22 16:39:11 | 显示全部楼层
顶一个
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-5-24 14:01:57 | 显示全部楼层
f839903061 发表于 2013-4-7 15:41
版主您好,我手边的开发板是A10处理器  板子和你图片上的不同!但是我看了几篇您发的帖子,咱们的源码还是 ...

你图上选中那行就是sun4i_gpio,选上吧。
回复 支持 反对

使用道具 举报

发表于 2013-5-24 16:06:27 | 显示全部楼层
WindLand 发表于 2013-5-24 14:01
你图上选中那行就是sun4i_gpio,选上吧。

谢谢,通过您的指导,我的gpio在前一段时间已经解决了,今天才看到回复 ,不好意思!
回复 支持 反对

使用道具 举报

发表于 2013-6-27 01:13:00 | 显示全部楼层
谢谢分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-5-1 14:42 , Processed in 0.031715 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部