stm32控制步进电机源码:
stm32单片机源程序:
- /*******************************************************************************
- * 文件名 : main.c
- * 作者 : jackice
- * 时间 :2013/8/10
- * 文件描述 : EN:PA.0 CLK:PA.1;DIR:PA.2
- ******两个电机同时走动,可以通过给定不同频率的脉冲、不同脉冲数达到两个电机走速不同,走的距离不同************
- **********134个脉冲 电机走1mm;1345个脉冲 电机走10mm;13450个脉冲电机走100mm;******
- ********************************************************************************/
- /* 头文件 ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #define en0_0() GPIO_ResetBits(GPIOA , GPIO_Pin_0) //宏定义
- #define en0_1() GPIO_SetBits(GPIOA , GPIO_Pin_0)
- #define en1_0() GPIO_ResetBits(GPIOA , GPIO_Pin_4) //宏定义
- #define en1_1() GPIO_SetBits(GPIOA , GPIO_Pin_4)
- #define clk0_0() GPIO_ResetBits(GPIOA , GPIO_Pin_1)
- #define clk0_1() GPIO_SetBits(GPIOA , GPIO_Pin_1)
- #define clk1_0() GPIO_ResetBits(GPIOA , GPIO_Pin_5)
- #define clk1_1() GPIO_SetBits(GPIOA , GPIO_Pin_5)
- #define dir0_0() GPIO_ResetBits(GPIOA , GPIO_Pin_2)
- #define dir0_1() GPIO_SetBits(GPIOA , GPIO_Pin_2)
- #define dir1_0() GPIO_ResetBits(GPIOA , GPIO_Pin_3)
- #define dir1_1() GPIO_SetBits(GPIOA , GPIO_Pin_3)
- /* 自定义同义关键字 --------------------------------------------------------*/
- /* 自定义参数宏 --------------------------------------------------------*/
- /* 自定义函数宏 --------------------------------------------------------*/
- /* 自定义变量 --------------------------------------------------------*/
- /* 自定义函数声明 --------------------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void delay(u32 i);
- void motor_contrl(u32 Xsteps,u8 Xdirection,u16 Xspeed,u32 Ysteps,u8 Ydirection,u16 Yspeed,u32 setps);
- /*******************************************************************************
- * 函数名 : main
- * 函数描述 : 无
- * 输入参数 : 无
- * 输出结果 : 无
- * 返回值 : 无
- *******************************************************************************/
- int main(void)
- {
-
- /* 设置系统时钟 */
- RCC_Configuration();
- /* 设置GPIO端口 */
- GPIO_Configuration();
- /*打开使能端*/
- en0_1();
- en1_1();
- while(1)
- {
- motor_contrl(1,0,3,1,0,3,13450);
- delay(100000);
- motor_contrl(1,1,3,1,1,3,13450);
- delay(100000);
- }
- }
- /*******************************************************************************
- * 函数名 : RCC_Configuration
- * 函数描述 : 设置系统各部分时钟
- * 输入参数 : 无
- * 输出结果 : 无
- * 返回值 : 无
- *******************************************************************************/
- void RCC_Configuration(void)
- {
- /* 定义枚举类型变量 HSEStartUpStatus */
- ErrorStatus HSEStartUpStatus;
- /* 复位系统时钟设置*/
- RCC_DeInit();
- /* 开启HSE*/
- RCC_HSEConfig(RCC_HSE_ON);
- /* 等待HSE起振并稳定*/
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
-
- /* 判断HSE起是否振成功,是则进入if()内部 */
- if(HSEStartUpStatus == SUCCESS)
- {
- /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* 设置FLASH延时周期数为2 */
- FLASH_SetLatency(FLASH_Latency_2);
- /* 使能FLASH预取缓存 */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* 使能PLL */
- RCC_PLLCmd(ENABLE);
- /* 等待PLL输出稳定 */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* 选择SYSCLK时钟源为PLL */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* 等待PLL成为SYSCLK时钟源 */
- while(RCC_GetSYSCLKSource() != 0x08);
- }
-
- /* 打开APB2总线上的GPIOA时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
- }
- /*******************************************************************************
- * 函数名 : GPIO_Configuration
- * 函数描述 : 设置各GPIO端口功能
- * 输入参数 : 无
- * 输出结果 : 无
- * 返回值 : 无
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* 设置PA4,PA5,PA6,PA7口为推挽输出,最大翻转频率为50MHz*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- }
- /*******************************************************************************
- * 函数名 : delay
- * 函数描述 : 简单延时
- * 输入参数 : i
- * 输出结果 : 无
- * 返回值 : 无
- *******************************************************************************/
- void delay(u32 k)
- {
- u32 j,i;
- for(i=k;i>0;i--)
- for(j=300;j>0;j--);
- }
- /*******************************************************************************
- * 函数名 : motor_contrl
- * 函数描述 : 电机控制,X和Y方向
- * 输入参数 : Xsteps,Xdirection,Xspeed,Ysteps,Ydirection,Yspeed,u32 steps
- * 输出结果 : 无
- * 返回值 : 无
- *******************************************************************************/
- void motor_contrl(u32 Xsteps,u8 Xdirection,u16 Xspeed,u32 Ysteps,u8 Ydirection,u16 Yspeed,u32 steps)
- {
- u32 step,Xstep,Ystep;
- if(Xdirection==1) //判断向左还是向右
- {
- dir0_1(); //向右
- }
- else
- {
- dir0_0(); //向左
- }
- if(Ydirection==1) //判断向上还是向下
- {
- dir1_1(); //向上
- }
- else
- {
- dir1_0(); //向下
- }
-
- for(step=steps;step>0;step--)
- {
- for(Xstep=Xsteps;Xstep>0;Xstep--) //给X轴电机脉冲数,控制X轴电机走的距离
- {
- clk0_0();
- delay(Xspeed); //给脉冲频率,控制电机转速,频率越高速度越快,频率越低速度越慢
- clk0_1();
- delay(Xspeed);
- for(Ystep=Ysteps;Ystep>0;Ystep--) //给Y轴电机脉冲数,控制Y轴电机走的距离
- {
- clk1_0();
- delay(Yspeed); //给脉冲频率,控制电机转速,频率越高速度越快,频率越低速度越慢
- clk1_1();
- delay(Yspeed);
- }
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
完整源码下载:
dianji1.rar
(391.01 KB, 下载次数: 256)
|