野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12309|回复: 1

STM32步进电机驱动程序,ULN2003+28BYJ48

[复制链接]
发表于 2017-3-18 17:17:01 | 显示全部楼层 |阅读模式
本帖最后由 CSWZH8 于 2017-3-18 17:20 编辑
  1. //motor,2½½øμç»úÇy¶ˉ
  2. #include "motor.h"
  3. #include <includes.h>

  4. volatile unsigned char motor_flag=0;//&#191;a&#198;&#244;2&#189;&#189;&#248;μ&#231;&#187;ú±ê&#188;&#199;
  5. volatile unsigned int  rad=0;  //×a&#182;ˉè|êy
  6. volatile unsigned char direct=0;//×a&#182;ˉ·&#189;&#207;ò,'s'&#203;3ê±&#213;&#235;,'n'&#196;&#230;ê±&#213;&#235;

  7. uint16_t  CCW[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};   //&#196;&#230;ê±&#213;&#235;£&#172;ê1ó&#195;μí&#203;&#196;&#206;&#187;£&#172;P1^0---P1^3
  8. uint16_t   CW[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};    //&#203;3ê±&#213;&#235;



  9. //3&#245;ê&#188;&#187;ˉ2&#189;&#189;&#248;μ&#231;&#187;ú&#191;&#216;&#214;&#198;òy&#189;&#197;
  10. void motor_init(void)
  11. {
  12.                 /*&#182;¨ò&#229;ò&#187;&#184;&#246;GPIO_InitTypeDefààDíμ&#196;&#189;á11ì&#229;*/
  13.         GPIO_InitTypeDef motor_GPIO_InitStructure;
  14.   //2&#189;&#189;&#248;μ&#231;&#187;úê&#228;è&#235;òy&#189;&#197;&#206;aPB8,PB9,PB10,PB11
  15.         RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOB, ENABLE );                                                                                                                           
  16.         motor_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;        
  17.         motor_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
  18.         motor_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19.         GPIO_Init (GPIOB , & motor_GPIO_InitStructure);        
  20.         GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11);
  21. }

  22. //&#209;óê±oˉêy
  23. void delaynms(unsigned int aa)
  24. {
  25.         //OSTimeDly(aa,OS_OPT_TIME_DLY, NULL);
  26.         Delay_us(500);
  27. }
  28. //&#209;óê±oˉêy
  29. void delay500us(void)
  30. {
  31.         

  32. }
  33. //μ&#231;&#187;ú·′&#207;ò×a&#182;ˉcntè|
  34. void motor_ccw(unsigned int cnt)   //·′&#207;ò×a&#182;ˉ
  35. {
  36.   unsigned char i,j,k=cnt;
  37.         while(k--)
  38.     for(j=0;j<8;j++)       //′ó&#209;-&#187;·8′&#206;×a&#182;ˉò&#187;&#214;ü      
  39.     {
  40.        for(i=0;i<8;i++)    //D&#161;&#209;-&#187;·8′&#206;×a&#182;ˉ45&#182;è
  41.        {
  42.          GPIO_Write(GPIOB, ( GPIO_ReadOutputData(GPIOB) & 0xf0ff )|(CCW[i]<<8 ));
  43.          delaynms(1);
  44.        }
  45.     }
  46.         GPIO_Write(GPIOB, ( GPIO_ReadOutputData(GPIOB) & 0xf0ff ));
  47. }
  48. //μ&#231;&#187;ú&#213;y&#207;ò×a&#182;ˉcntè|
  49. void motor_cw(unsigned int cnt)  //&#213;y&#207;ò×a&#182;ˉ
  50. {
  51.   unsigned char i,j,k=cnt;
  52.         while(k--)
  53.     for(j=0;j<8;j++)    //′ó&#209;-&#187;·8′&#206;×a&#182;ˉò&#187;&#214;ü
  54.     {
  55.       for(i=0;i<8;i++)  //D&#161;&#209;-&#187;·8′&#206;×a&#182;ˉ45&#182;è
  56.       {
  57.         GPIO_Write(GPIOB, ( GPIO_ReadOutputData(GPIOB) & 0xf0ff )|(CW[i]<<8 ));
  58.         delaynms(1);   
  59.       }
  60.     }
  61.         GPIO_Write(GPIOB, ( GPIO_ReadOutputData(GPIOB) & 0xf0ff ));
  62. }

  63. //&#184;ù&#190;Ycè·&#182;¨×a&#182;ˉ·&#189;&#207;ò£&#172;2¢×a&#182;ˉcntè|êy
  64. void roll(unsigned char c,unsigned int cnt)
  65. {
  66.         if(c=='s' || c=='S')
  67.                          motor_cw( cnt);
  68.         else if(c=='n' || c=='N')
  69.                    motor_ccw( cnt) ;
  70.         GPIO_Write(GPIOB, ( GPIO_ReadOutputData(GPIOB) & 0xf0ff ));
  71. }
复制代码


注释好像有问题,呃呃呃

回复

使用道具 举报

 楼主| 发表于 2017-3-18 17:20:52 | 显示全部楼层
void Delay_us( unsigned int  nTime)//10usò&#187;&#184;&#246;μ¥&#206;&#187;(&#191;é&#196;üóD&#206;êìa£&#161;£&#161;£&#161;)
{
                unsigned int i,j;
                for(i=nTime;i>0;i--)
                        for(j=180;j>0;j--);
}
补上延时程序
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-6-28 00:18 , Processed in 0.045212 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表