按键控制步进电机的运行,处理器是stm32

[复制链接]
5509|16
 楼主| jiangsai 发表于 2013-4-25 09:01 | 显示全部楼层 |阅读模式
下面是程序
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"
  3. #include "stm32_eval.h"
  4. #include <stdio.h>
  5. //#include "ARMJISHU_TouchScreen_ADS7843.h"
  6. #include "sys.h"               
  7.        
  8. void USART_Configuration(void);
  9. void RCC_Configuration(void);
  10. void GPIO_Configuration(void);
  11. void Delay(vu32 nCount);
  12. void Delay1(vu32 nCount);

  13. /* Values magic to the Board keys */
  14. #define  NOKEY  0
  15. #define  KEY1   1
  16. #define  KEY2   2
  17. #define  KEY3   3
  18. #define  KEY4   4
  19. #define  KEY5   5            

  20. /*按键相关定义*/
  21. #define RCC_KEY1                                    RCC_APB2Periph_GPIOB
  22. #define GPIO_KEY1_PORT                              GPIOB   
  23. #define GPIO_KEY1                                   GPIO_Pin_0
  24. #define RCC_KEY2                                    RCC_APB2Periph_GPIOB
  25. #define GPIO_KEY2_PORT                              GPIOB
  26. #define GPIO_KEY2                                   GPIO_Pin_1
  27. #define RCC_KEY3                                    RCC_APB2Periph_GPIOB
  28. #define GPIO_KEY3_PORT                              GPIOB
  29. #define GPIO_KEY3                                   GPIO_Pin_2
  30. #define RCC_KEY4                                    RCC_APB2Periph_GPIOB
  31. #define GPIO_KEY4_PORT                              GPIOB
  32. #define GPIO_KEY4                                   GPIO_Pin_3
  33. #define RCC_KEY5                                    RCC_APB2Periph_GPIOB
  34. #define GPIO_KEY5_PORT                              GPIOB
  35. #define GPIO_KEY5                                   GPIO_Pin_4

  36. /* Private function prototypes -----------------------------------------------*/
  37. #ifdef __GNUC__
  38. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  39.                                              set to 'Yes') calls __io_putchar() */
  40.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  41. #else
  42.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  43. #endif /* __GNUC__ */
  44.   
  45. /*static void Delay_ARMJISHU(__IO uint32_t nCount)
  46. {
  47.   for (; nCount != 0; nCount--)
  48.   {
  49.       if(GPIO_ADS7843_INT_VALID)
  50.       {
  51.         ARMJISHU_TouchScreen_ADS7843();
  52.       }       
  53.   }
  54. }                                                   */

  55. void delay(void)
  56.   {
  57.       unsigned long ik ;
  58.       for(ik=0;ik<0x0baf8;ik++) ;
  59.   }
  60. void delay1(void)
  61.   {
  62.       unsigned long ik ;
  63.       for(ik=0;ik<0x00af8;ik++) ;
  64.   }

  65. /* Private functions ---------------------------------------------------------*/

  66. /*******************************************************************************
  67. * Function Name  : GPIO_KEY_Config.
  68. * Description    : Configures the Extension Button.
  69. * Input          : None.
  70. * Output         : None.
  71. * Return         : None.
  72. *******************************************************************************/
  73. void GPIO_KEY_Config(void)
  74. {
  75.   GPIO_InitTypeDef GPIO_InitStructure;

  76.   /* Configure KEY1 Button */
  77.   RCC_APB2PeriphClockCmd(RCC_KEY1, ENABLE);

  78.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  79.   GPIO_InitStructure.GPIO_Pin = GPIO_KEY1;
  80.   GPIO_Init(GPIO_KEY1_PORT, &GPIO_InitStructure);
  81.     /* Configure KEY2 Button */
  82.   RCC_APB2PeriphClockCmd(RCC_KEY2, ENABLE);

  83.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  84.   GPIO_InitStructure.GPIO_Pin = GPIO_KEY2;
  85.   GPIO_Init(GPIO_KEY2_PORT, &GPIO_InitStructure);
  86.       /* Configure KEY3 Button */
  87.   RCC_APB2PeriphClockCmd(RCC_KEY3, ENABLE);

  88.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  89.   GPIO_InitStructure.GPIO_Pin = GPIO_KEY3;
  90.   GPIO_Init(GPIO_KEY3_PORT, &GPIO_InitStructure);
  91.         /* Configure KEY4 Button */
  92.   RCC_APB2PeriphClockCmd(RCC_KEY4, ENABLE);

  93.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  94.   GPIO_InitStructure.GPIO_Pin = GPIO_KEY4;
  95.   GPIO_Init(GPIO_KEY4_PORT, &GPIO_InitStructure);
  96.         /* Configure KEY5 Button */
  97.   RCC_APB2PeriphClockCmd(RCC_KEY5, ENABLE);

  98.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  99.   GPIO_InitStructure.GPIO_Pin = GPIO_KEY5;
  100.   GPIO_Init(GPIO_KEY5_PORT, &GPIO_InitStructure);
  101. }
  102. /*******************************************************************************
  103. * Function Name  : ReadKey
  104. * Description    : Reads key from demoboard.
  105. * Input          : None
  106. * Output         : None
  107. * Return         : Return RIGHT, LEFT, SEL, UP, DOWN or NOKEY
  108. *******************************************************************************/
  109. u8 ReadKeyDown(void)
  110. {
  111.   /* 1 key is pressed */
  112.   if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1)==0)
  113.   {
  114.     return KEY1;
  115.   }       
  116.     /* 2 key is pressed */
  117.   if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2)==0)
  118.   {
  119.     return KEY2;
  120.   }
  121.    /* 3 key is pressed */
  122.   if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT, GPIO_KEY3)==0)
  123.   {
  124.     return KEY3;
  125.   }
  126.     /* 4 key is pressed */
  127.   if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT, GPIO_KEY4)==0)
  128.   {
  129.     return KEY4;
  130.   }
  131.     /* 5 key is pressed */
  132.   if(!GPIO_ReadInputDataBit(GPIO_KEY5_PORT, GPIO_KEY5)==0)
  133.   {
  134.     return KEY5;
  135.   }
  136.    
  137.   /* No key is pressed */
  138.   else
  139.   {
  140.     return NOKEY;
  141.   }
  142. }
  143. void motor_Turn_on_1(void)                 //正转
  144. {
  145.                               GPIO_Write(GPIOA, 0x08);
  146.                                                       delay();
  147.                                                       GPIO_Write(GPIOA,0x0C);
  148.                                                       delay();
  149.                                                       GPIO_Write(GPIOA,0x04);
  150.                                                       delay();
  151.                                                       GPIO_Write(GPIOA,0x06);
  152.                                                       delay();       
  153.                                                           GPIO_Write(GPIOA, 0x02);
  154.                                                       delay();
  155.                                                           GPIO_Write(GPIOA, 0x03);
  156.                                                       delay();
  157.                                                           GPIO_Write(GPIOA, 0x01);
  158.                                                       delay();
  159.                                                            GPIO_Write(GPIOA, 0x09);
  160.                                                       delay();
  161. }
  162. void motor_Turn_on_2(void)                 //反转
  163. {
  164.                               GPIO_Write(GPIOA,0x09);
  165.                                                       delay();
  166.                                                           GPIO_Write(GPIOA,0x01);
  167.                                                       delay();
  168.                                                           GPIO_Write(GPIOA,0x03);
  169.                                                       delay();
  170.                                                       GPIO_Write(GPIOA,0x02);
  171.                                                       delay();
  172.                                                           GPIO_Write(GPIOA, 0x06);
  173.                                                       delay();       
  174.                                                            GPIO_Write(GPIOA, 0x04);
  175.                                                       delay();
  176.                                                            GPIO_Write(GPIOA, 0x0C);
  177.                                                       delay();
  178.                                                            GPIO_Write(GPIOA, 0x08);
  179.                                                       delay();       
  180. }
  181. void motor_Turn_on_3(void)                //         正转两圈

  182. {
  183.   u8 i,j;
  184.   u8 r;
  185.   u8 N=64;
  186.   for(r=0;r<N;r++)
  187.   {
  188.     i=ReadKeyDown();
  189.    if(i==5)
  190. {
  191.    break;   //如果K5按下,退出此循环
  192. }
  193.   for(j=0;j<64;j++)                 //电机外轴旋转一周,不是里里面面所看到的一周
  194.   {
  195.                               GPIO_Write(GPIOA, 0x08);
  196.                                                       delay();
  197.                                                       GPIO_Write(GPIOA,0x0C);
  198.                                                       delay();
  199.                                                       GPIO_Write(GPIOA,0x04);
  200.                                                       delay();
  201.                                                       GPIO_Write(GPIOA,0x06);
  202.                                                       delay();       
  203.                                                           GPIO_Write(GPIOA, 0x02);
  204.                                                       delay();
  205.                                                           GPIO_Write(GPIOA, 0x03);
  206.                                                       delay();
  207.                                                           GPIO_Write(GPIOA, 0x01);
  208.                                                       delay();
  209.                                                           GPIO_Write(GPIOA, 0x09);
  210.                                                       delay();

  211.   }
  212. }
  213. }
  214. void motor_Turn_on_4(void)                //         反转两圈
  215. {
  216.   u8 i,j;
  217.   u8 r;
  218.   u8 N=64;
  219.   for(r=0;r<N;r++)
  220.   {
  221.     i=ReadKeyDown();
  222.    if(i==5)
  223. {
  224.    break;   //如果K5按下,退出此循环
  225. }
  226.   for(j=0;j<64;j++)                 //电机外轴旋转一周,不是里里面面所看到的一周
  227.   {
  228.     GPIO_Write(GPIOA,0x09);
  229.         delay();
  230.         GPIO_Write(GPIOA,0x01);
  231.         delay();
  232.         GPIO_Write(GPIOA,0x03);
  233.         delay();
  234.         GPIO_Write(GPIOA,0x02);
  235.         delay();
  236.     GPIO_Write(GPIOA, 0x06);
  237.         delay();       
  238.         GPIO_Write(GPIOA, 0x04);
  239.         delay();
  240.         GPIO_Write(GPIOA, 0x0C);
  241.         delay();
  242.         GPIO_Write(GPIOA, 0x08);
  243.         delay();       
  244.   }
  245. }
  246. }
  247. void motor_Turn_on_5(void)                //          停止
  248. {
  249.   GPIO_ResetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
  250. }
  251. void motor_ccw(u8 key)
  252.   {
  253.    
  254.         /* Turn Off Select LED */
  255.     switch(key)
  256.     {
  257.           case 0:
  258.                  
  259.           motor_Turn_on_1();   //正转
  260.           break;

  261.         case 1:
  262.                  
  263.           motor_Turn_on_2();         //反转
  264.           break;

  265.         case 2:                                        //正转两圈
  266.                  
  267.           motor_Turn_on_3();
  268.           break;

  269.         case 3:
  270.                
  271.           motor_Turn_on_4();                //反转两圈
  272.           break;
  273.         case 4:
  274.                   motor_Turn_on_5();                //停止
  275.           break;
  276.         default:
  277.           motor_Turn_on_5();
  278.           break;
  279.     }
  280. }   
  281. int main(void)
  282. {                       
  283. //        int m=2, n=1;
  284.     u8 KeyNum = 0;
  285.         #ifdef DEBUG
  286.         debug();
  287.     #endif       

  288.         USART_Configuration();         //必须放在前面
  289.         RCC_Configuration();
  290.         GPIO_Configuration();       
  291. //        ADS7843_Init();       

  292.     while (1)
  293.    {
  294.                 KeyNum=ReadKeyDown();
  295.                    motor_ccw(KeyNum-1);
  296.             
  297.     }         
  298. }

  299. void USART_Configuration(void)
  300. {
  301.      USART_InitTypeDef USART_InitStructure;

  302.      USART_InitStructure.USART_BaudRate = 115200;

  303.          USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  304.          USART_InitStructure.USART_StopBits = USART_StopBits_1;

  305.          USART_InitStructure.USART_Parity = USART_Parity_No;

  306.          USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  307.          USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;       

  308.          STM_EVAL_COMInit(COM1, &USART_InitStructure);
  309. }          

  310. void RCC_Configuration()
  311. {
  312.          //定义错误状态变量
  313.          ErrorStatus HSEStartUpStatus;
  314.        
  315.          //将RCC寄存器重新设置为默认值
  316.          RCC_DeInit();

  317.          //打开外部高速时钟晶振
  318.          RCC_HSEConfig(RCC_HSE_ON);

  319.          //等待外部高速时钟晶振工作
  320.          HSEStartUpStatus = RCC_WaitForHSEStartUp();
  321.          if(HSEStartUpStatus == SUCCESS)
  322.          {
  323.                  //设置AHB时钟(HCLK)为系统时钟
  324.                  RCC_HCLKConfig(RCC_SYSCLK_Div1);

  325.                  //设置高速AHB时钟(APB2)为HCLK时钟
  326.                  RCC_PCLK2Config(RCC_HCLK_Div1);

  327.                  //设置低速AHB时钟(APB1)为HCLK的2分频
  328.                  RCC_PCLK1Config(RCC_HCLK_Div2);
  329.                
  330.                  //设置FLASH代码延时
  331.                  FLASH_SetLatency(FLASH_Latency_2);

  332.                  //使能预取指缓存
  333.                  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  334.                  //设置PLL时钟,为HSE的9倍频 8MHz * 9 = 72MHz
  335.                  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  336.                   //使能PLL
  337.                  RCC_PLLCmd(ENABLE);

  338.                  //等待PLL准备就绪
  339.                  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

  340.                  //设置PLL为系统时钟源
  341.                  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  342.                  //判断PLL是否是系统时钟
  343.                  while(RCC_GetSYSCLKSource() != 0x08);
  344.          }

  345.          //开启TIM3的时钟
  346.          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  347.          //开启GPIOB的时钟和复用功能
  348.          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);

  349. }

  350. void GPIO_Configuration()
  351. {
  352.          GPIO_InitTypeDef GPIO_InitStructure;

  353.          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//打开外设A,的时钟

  354.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;

  355.          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//IO口的方向

  356.          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  357.          GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化外设0,1,2,3,6的端口
  358. }

  359. /**
  360.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Retargets the C library printf function to the USART.
  361.   * @param  None
  362.   * @retval None
  363.   */
  364. PUTCHAR_PROTOTYPE
  365. {
  366.   /* Place your implementation of fputc here */
  367.   /* e.g. write a character to the USART */
  368.   USART_SendData(EVAL_COM1, (uint8_t) ch);

  369.   /* Loop until the end of transmission */
  370.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  371.   {
  372.   }

  373.   return ch;
  374. }

  375. #ifdef  USE_FULL_ASSERT

  376. /**
  377.   * @brief  Reports the name of the source file and the source line number
  378.   *         where the assert_param error has occurred.
  379.   * @param  file: pointer to the source file name
  380.   * @param  line: assert_param error line source number
  381.   * @retval None
  382.   */

  383. void assert_failed(uint8_t* file, uint32_t line)
  384. {
  385.   /* User can add his own implementation to report the file name and line number,
  386.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  387.   /* Infinite loop */
  388.   while (1)
  389.   {
  390.   }
  391. }
  392. #endif
  393. /**
  394.   * @}
  395.   */

  396. /**
  397.   * @}
  398.   */
  399. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
,程序编译没问题,但实际运行按键根本不起作用,通电后电机就按一个方向转。。仿真发现再没按按键情况下,程序会直接return KEY4; 前面的key1,key2,key3不返回。按键本身都没问题,求大神赐教是设么原因。。帮忙找找错,
 楼主| jiangsai 发表于 2013-4-25 09:04 | 显示全部楼层
还有我如果把 void motor_ccw(u8 key)函数中case3,case4屏蔽后,前面的key1,key2,key3功能就正常了。。求大神们解释哈。。我是新手,程序优化可能不好,现在只求纠错。。
uet_cache 发表于 2013-4-25 09:08 | 显示全部楼层
如果你 电机控制子函数没问题,那就是按键问题。如果都按一个方向转,是不是有一个键一直起作用。你可以把大循环中电机控制子函数屏掉,只检测按键,结合硬件,看按键扫描有没有起作用。
建 议你做按键消抖处理,防止误操作。
 楼主| jiangsai 发表于 2013-4-25 09:38 | 显示全部楼层
uet_cache 发表于 2013-4-25 09:08
如果你 电机控制子函数没问题,那就是按键问题。如果都按一个方向转,是不是有一个键一直起作用。你可以把 ...

没按按键的时候仿真发现程序始终在不断返回key4的值,所以不断执行key4的功能。。。郁闷呀。。按键没问题。。我把key4定义到另外的按键后,仿真还是不断返回key4.。。至于消抖,是我下一步的工作。。我想先把按键基本功能实现再说。。求大神解释哈。小弟感激不尽。。
uet_cache 发表于 2013-4-25 10:03 | 显示全部楼层
你的问题应该很简单,就是按键的问题,慢慢查硬件吧。。
 楼主| jiangsai 发表于 2013-4-25 10:33 | 显示全部楼层
uet_cache 发表于 2013-4-25 10:03
你的问题应该很简单,就是按键的问题,慢慢查硬件吧。。

我所有按键不接,依然是这程序,点击都按照一个方向转
dream_yi 发表于 2013-4-25 14:56 | 显示全部楼层
不是太确定,但是好像是单目运算符的优先级要高于双目运算符吧,那样的话你的           if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1)==0)逻辑就有点乱了.
 楼主| jiangsai 发表于 2013-4-25 15:12 | 显示全部楼层
dream_yi 发表于 2013-4-25 14:56
不是太确定,但是好像是单目运算符的优先级要高于双目运算符吧,那样的话你的           if(!GPIO_ReadInput ...

这个我已经改成if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1)),在key4那里陷入了死循环了
 楼主| jiangsai 发表于 2013-4-25 15:16 | 显示全部楼层
有木有同**行一下,帮忙看看吧。。
hawksabre 发表于 2013-4-25 19:23 | 显示全部楼层
程序老长啊   不想看  飘过
yushiqian2012 发表于 2013-4-25 23:56 | 显示全部楼层
jiangsai 发表于 2013-4-25 09:04
还有我如果把 void motor_ccw(u8 key)函数中case3,case4屏蔽后,前面的key1,key2,key3功能就正常了。。 ...

看了一会儿,实在太长了楼主。
建议:
一个模块一个模块的调试,
比如调试扫描按键,保证扫描的没有问题。
还有,如果要分析的话,只给代码不行吧,你的那些扫描按键显示怎么接的怎么也得说明一下啊
mmuuss586 发表于 2013-4-26 08:32 | 显示全部楼层
控制几个啊
mmuuss586 发表于 2013-4-26 08:33 | 显示全部楼层
我最近在做控制8个的,用的是STM32F407VGT6
mmuuss586 发表于 2013-4-26 08:35 | 显示全部楼层
你的有加、减速算法吗?
可以设置启动频率、运行频率、停止频率吗?
触觉的爱 发表于 2013-4-26 09:49 | 显示全部楼层
最大的可能是按键检测部分吧,参考一下别人的按键检测程序,甚至可以套用的函数

另外,多选一、优先选择这些,应该把它们结合成一个整体,以免编译器帮你“优化”掉了
 楼主| jiangsai 发表于 2013-4-27 07:53 | 显示全部楼层
mmuuss586 发表于 2013-4-26 08:35
你的有加、减速算法吗?
可以设置启动频率、运行频率、停止频率吗?

加减速算法不就是延长时间的设置吗。。我做的是个毕业设计。。小白一只,好多不懂哦
mmuuss586 发表于 2013-4-27 08:27 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

49

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部