Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1067073
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类: C/C++

2011-05-09 15:23:01

选用的步进电机的型号为28BYJ-48(或MP28GA5V,转速比1/64),驱动电路选用uln2003芯片的驱动板,其控制时序图如下:

四相八拍:A->AB->B ->BC ->C ->CD ->D ->DA

ABCD指的是uln2003芯片驱动板的1N11N21N31N4,波形在上表示有输入信号。

此外至少需要4根杜邦线,还需提供一个5V的直流电源,接线方式如下:

PE0IN1
    PE1IN2
    PE2IN3
    PE3IN4
    5V电源(一个是地,一个是正)。

  1. /***************************************************************************//**
  2.  * @file : stm32_pio.h
  3.  * @brief : STM32F1xx CoX PIO Peripheral Interface
  4.  * @version : V1.0
  5.  * @date : 28 Feb. 2011
  6.  * @author : CooCox
  7. *******************************************************************************/
  8. #ifndef __STM_PIO_H
  9. #define __STM_PIO_H

  10. #include "cox_pio.h"

  11. /***************************************************************************//**
  12.  * Define STM32F1xx CoX PIO Peripheral Interface
  13. *******************************************************************************/
  14. extern COX_PIO_PI pi_pio;

  15. #endif
  1. /***************************************************************************//**
  2.  * @file : stm32_pio.c
  3.  * @brief : STM32F1xx CoX PIO Peripheral Interface
  4.  * @version : V1.0
  5.  * @date : 28 Feb. 2011
  6.  * @author : CooCox
  7.  ******************************************************************************/
  8. #include "stm32_pio.h"
  9. #include "stm32f10x.h"


  10. /***************************************************************************//**
  11.  * @brief Get pointer to GPIO peripheral due to GPIO port
  12.  * @param[in] portNum : Port Number value, should be in range from 0 to 6.
  13.  * @return Pointer to GPIO peripheral
  14. *******************************************************************************/
  15. static GPIO_TypeDef* STM32_GetGPIO(uint8_t port)
  16. {
  17.     GPIO_TypeDef *pGPIO = COX_NULL;

  18.     switch(port)
  19.     {
  20.         case 0: pGPIO = GPIOA; break;
  21.         case 1: pGPIO = GPIOB; break;
  22.         case 2: pGPIO = GPIOC; break;
  23.         case 3: pGPIO = GPIOD; break;
  24.         case 4: pGPIO = GPIOE; break;
  25.         case 5: pGPIO = GPIOF; break;
  26.         case 6: pGPIO = GPIOG; break;
  27.         default: break;
  28.     }

  29.     return pGPIO;
  30. }


  31. /***************************************************************************//**
  32.  * @brief Initializes the PIO peripheral
  33.  * @param[in] pio : The specified peripheral
  34.  * @return Result, may be :
  35.  * -COX_ERROR : Error occurred, parameter is not supported
  36.  * -COX_SUCCESS : Previous argument of the specified option
  37. *******************************************************************************/
  38. static COX_Status STM32_PIO_Init (COX_PIO_Dev pio)
  39. {

  40.     GPIO_TypeDef* pGPIO = COX_NULL;
  41.     uint8_t port, pin;

  42.     port = (pio >> 8) & 0xFF;
  43.     pin = (pio >> 0) & 0xFF;

  44.     pGPIO = STM32_GetGPIO(port);

  45.     if(port >6 || port<0 || pin<0 || pin > 15)
  46.         return COX_ERROR;

  47.     /* Enable GPIO and AFIO clocks */
  48.     switch(port)
  49.     {
  50.         case 0: RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN); break;
  51.         case 1: RCC->APB2ENR |= (RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN); break;
  52.         case 2: RCC->APB2ENR |= (RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN); break;
  53.         case 3: RCC->APB2ENR |= (RCC_APB2ENR_IOPDEN | RCC_APB2ENR_AFIOEN); break;
  54.         case 4: RCC->APB2ENR |= (RCC_APB2ENR_IOPEEN | RCC_APB2ENR_AFIOEN); break;
  55.         default: break;
  56.     }
  57.   return COX_SUCCESS;
  58. }


  59. /***************************************************************************//**
  60.  * @brief Set direction (Input or Output)
  61.  * @param[in] pio : The specified PIO peripheral
  62.  * @param[in] dir : Direction, should be
  63.  * -0: Input
  64.  * -1: Output
  65.  * @return Result, may be :
  66.  * -COX_ERROR : Error occurred, parameter is not supported
  67.  * -COX_SUCCESS : Previous argument of the specified option
  68. *******************************************************************************/
  69. static COX_Status STM32_PIO_SetDir(COX_PIO_Dev pio, uint8_t dir)
  70. {

  71.     GPIO_TypeDef* pGPIO = COX_NULL;
  72.     uint8_t port, pin;

  73.     port = (pio >> 8) & 0xFF;
  74.     pin = (pio >> 0) & 0xFF;

  75.     pGPIO = STM32_GetGPIO(port);

  76.     /* Direction is input:GPIO_Mode_IN_FLOATING */
  77.     if(dir == 0){
  78.         if(pin>7)
  79.         {
  80.             /* Configure the eight high port pins */
  81.             pin = pin-8;
  82.             /* MODE[1:0]=00 */
  83.             pGPIO -> CRH &= ~(0x3 << (pin*4));
  84.             /* CNF[1:0] =01 */
  85.             pGPIO -> CRH &= ~(0x8 << (pin*4));
  86.             pGPIO -> CRH |= (0x4 <<(pin*4));
  87.         }

  88.         else
  89.         {
  90.             /* Configure the eight low port pins */
  91.             pGPIO -> CRL &= ~(0x3 << (pin*4));
  92.             pGPIO -> CRL &= ~(0x8 << (pin*4));
  93.             pGPIO -> CRL |= (0x4 << (pin*4));
  94.         }
  95.     }

  96.     /* Direction is output:GPIO_Mode_Out_PP */
  97.     else {
  98.         if(pin>7)
  99.         {
  100.             pin = pin-8;
  101.             /* MODE[1:0]=11 */
  102.             pGPIO -> CRH |= (0x3 <<(pin*4));
  103.             /* CNF[1:0] =00 */
  104.              pGPIO -> CRH &= ~(0xc <<(pin*4));
  105.         }
  106.         else
  107.         {
  108.             pGPIO -> CRL |= (0x3 << (pin*4));
  109.             pGPIO -> CRL &= ~(0xc<<(pin*4));
  110.         }
  111.     }
  112.     return COX_SUCCESS;
  113. }


  114. /******************************
  115. 注:暂存的内容只能恢复到当前文章的编辑器中,如需恢复到其他文章中,请编辑该文章并从暂存箱中恢复;或者直接复制以上内容,手工恢复到相关文章。
  116. 恢复到编辑器   关闭
阅读(13535) | 评论(0) | 转发(0) |
0

上一篇:0欧电阻

下一篇:sqlite 中sql 的位运算符

给主人留下些什么吧!~~