采用A3984驅動芯片
同時也兼容不部分驅動器
控制主要包括三點:PWM脈沖輸出,DIR方向選擇,EN控制使能
采用定時器輸出比較模式
單片機源程序如下:
- #include "stm32f4xx.h"
- #include "MicroStepDriver.h"
- #include "bsp_debug_usart.h"
- #include "mpu6050.h"
-
- /**
- * @brief 主函數
- * @param 無
- * @retval 無
- */
- int main(void)
- {
- // signed int step; //步數
- // signed int now_place; //當前角度
- //步進電機初始化
- MSD_Init();
- while(1)
- {
- }
-
- }
- /*********************************************END OF FILE**********************/
復制代碼- #include "MicroStepDriver.h"
- #include <stdio.h>
- #include <math.h>
- //系統加減速參數
- //speedRampData srd;
- ////記錄步進電機的位置
- //int stepPosition = 0;
- ////系統電機、串口狀態
- //struct GLOBAL_FLAGS status = {FALSE, FALSE,TRUE};
- /**
- * @brief 定時器中斷優先級配置
- * @param 無
- * @retval 無
- */
- static void TIM_NVIC_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- // 設置中斷組為0
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- // 設置中斷來源
- NVIC_InitStructure.NVIC_IRQChannel = MSD_PULSE_TIM_IRQ;
- // 設置搶占優先級
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- // 設置子優先級
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /**
- * @brief 初始化電機驅動用到的引腳
- * @param 無
- * @retval 無
- */
- static void MSD_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //電機脈沖輸出 GPIO 初始化
- MSD_PULSE_AHBxClock_FUN(MSD_PULSE_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = MSD_PULSE_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(MSD_PULSE_PORT, &GPIO_InitStructure);
- /* 連接 PXx 到 定時器輸出通道*/
- GPIO_PinAFConfig(MSD_PULSE_PORT,MSD_PULSE_PIN_SOURCE,MSD_PULSE_PIN_AF);
-
- //電機方向輸出 GPIO 、細分、reset、sleep初始化
- MSD_DIR_AHBxClock_FUN(MSD_DIR_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = MSD_DIR_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(MSD_DIR_PORT, &GPIO_InitStructure);
- GPIO_ResetBits(MSD_DIR_PORT,MSD_DIR_PIN);
-
- //電機使能輸出 GPIO 初始化
- MSD_ENA_AHBxClock_FUN(MSD_ENA_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = MSD_ENA_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(MSD_ENA_PORT, &GPIO_InitStructure);
- GPIO_ResetBits(MSD_ENA_PORT,MSD_ENA_PIN);
- }
- ///*
- // * 注意:TIM_TimeBaseInitTypeDef結構體里面有5個成員,TIM6和TIM7的寄存器里面只有
- // * TIM_Prescaler和TIM_Period,所以使用TIM6和TIM7的時候只需初始化這兩個成員即可,
- // * 另外三個成員是通用定時器和高級定時器才有.
- // *-----------------------------------------------------------------------------
- // *typedef struct
- // *{ TIM_Prescaler 都有
- // * TIM_CounterMode TIMx,x[6,7]沒有,其他都有
- // * TIM_Period 都有
- // * TIM_ClockDivision TIMx,x[6,7]沒有,其他都有
- // * TIM_RepetitionCounter TIMx,x[1,8,15,16,17]才有
- // *}TIM_TimeBaseInitTypeDef;
- // *-----------------------------------------------------------------------------
- // */
- /* ---------------- PWM信號 周期和占空比的計算--------------- */
- // ARR :自動重裝載寄存器的值
- // CLK_cnt:計數器的時鐘,等于 Fck_int / (psc+1) = 168M/(psc+1)
- // PWM 信號的周期 T = (ARR+1) * (1/CLK_cnt) = (ARR+1)*(PSC+1) / 72M
- // 占空比P=CCR/(ARR+1)
- static void TIM_Mode_Config(void)
- {
- // 開啟定時器時鐘,即內部時鐘CK_INT=168M
- MSD_PULSE_TIM_APBxClock_FUN(MSD_PULSE_TIM_CLK,ENABLE);
- /*--------------------時基結構體初始化-------------------------*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- // 自動重裝載寄存器的值,累計TIM_Period+1個周期后產生一個更新或者中斷
- TIM_TimeBaseStructure.TIM_Period=MSD_PULSE_TIM_PERIOD;
- // 驅動CNT計數器的時鐘 = Fck_int/(psc+1)
- TIM_TimeBaseStructure.TIM_Prescaler= MSD_PULSE_TIM_PSC;
- // 時鐘分頻因子 ,配置死區時間時需要用到
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- // 計數器計數模式,設置為向上計數
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- // 重復計數器的值,最大值為255
- //TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
- // 初始化定時器
- TIM_TimeBaseInit(MSD_PULSE_TIM, &TIM_TimeBaseStructure);
- /*--------------------輸出比較結構體初始化-------------------*/
- TIM_OCInitTypeDef TIM_OCInitStructure;
- // 配置為PWM模式2
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- // 輸出使能
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- // 互補輸出禁能
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
- // 設置占空比大小
- TIM_OCInitStructure.TIM_Pulse = MSD_PULSE_TIM_PERIOD/2;
- // 輸出通道電平極性配置
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- // 輸出通道空閑電平極性配置
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
-
- MSD_PULSE_OCx_Init(MSD_PULSE_TIM, &TIM_OCInitStructure);
- //使能TIM1_CH1預裝載寄存器
- MSD_PULSE_OCx_PreloadConfig(MSD_PULSE_TIM, TIM_OCPreload_Enable);
- //使能TIM1預裝載寄存器
- TIM_ARRPreloadConfig(MSD_PULSE_TIM, ENABLE);
-
- //設置中斷源,只有溢出時才中斷
- TIM_UpdateRequestConfig(MSD_PULSE_TIM,TIM_UpdateSource_Regular);
- // 清除中斷標志位
- TIM_ClearITPendingBit(MSD_PULSE_TIM, TIM_IT_Update);
- // 使能中斷
- TIM_ITConfig(MSD_PULSE_TIM, TIM_IT_Update, ENABLE);
- // 使能計數器
- TIM_Cmd(MSD_PULSE_TIM, ENABLE);
- }
- /**
- * @brief 初始化電機相關的外設
- * @param 無
- * @retval 無
- */
- void MSD_Init(void)
- {
- MSD_GPIO_Config();
-
- TIM_NVIC_Config();
- TIM_Mode_Config();
- }
- /*********************************************END OF FILE**********************/
復制代碼
- #include "mpu6050.h"
- #include "string.h"
- struct STime stcTime;
- struct SAcc stcAcc;
- struct SGyro stcGyro;
- struct SAngle stcAngle;
- struct SMag stcMag;
- struct SDStatus stcDStatus;
- struct SPress stcPress;
- struct SLonLat stcLonLat;
- struct SGPSV stcGPSV;
- //unsigned char [250];
- //CopeSerialData為串口中斷調用函數,串口每收到一個數據,調用一次這個函數。
- //處理串口接收到的陀螺儀數據
- void CopeSerialData(unsigned char ucData)
- {
- static unsigned char ucRxBuffer[250];
- static unsigned char ucRxCnt = 0;
-
- ucRxBuffer[ucRxCnt++]=ucData;
- if (ucRxBuffer[0]!=0x55) //數據頭不對,則重新開始尋找0x55數據頭
- {
- ucRxCnt=0;
- return;
- }
- if (ucRxCnt<11) {return;}//數據不滿11個,則返回
- else
- {
- switch(ucRxBuffer[1])
- {
- case 0x50: memcpy(&stcTime,&ucRxBuffer[2],8);break;//memcpy為編譯器自帶的內存拷貝函數,需引用"string.h",將接收緩沖區的字符拷貝到數據共同體里面,從而實現數據的解析。
- case 0x51: memcpy(&stcAcc,&ucRxBuffer[2],8);break;
- case 0x52: memcpy(&stcGyro,&ucRxBuffer[2],8);break;
- case 0x53: memcpy(&stcAngle,&ucRxBuffer[2],8);break;
- case 0x54: memcpy(&stcMag,&ucRxBuffer[2],8);break;
- case 0x55: memcpy(&stcDStatus,&ucRxBuffer[2],8);break;
- case 0x56: memcpy(&stcPress,&ucRxBuffer[2],8);break;
- case 0x57: memcpy(&stcLonLat,&ucRxBuffer[2],8);break;
- case 0x58: memcpy(&stcGPSV,&ucRxBuffer[2],8);break;
- }
- ucRxCnt=0;
- }
- }
復制代碼
所有資料51hei提供下載:
定時器.7z
(355.16 KB, 下載次數: 37)
2019-3-27 19:01 上傳
點擊文件名下載附件
簡單步進電機驅動程序
|