本帖最后由 fangmoyuasne 于 2018-2-25 11:49 编辑 //motor.c #define m0_xc_max 1050 //250*5 #define m1_xc_max 375 //150*5/2 #define m2_xc_max 0 #define m3_xc_max 500 //100*5//可能需要修改 #define m4_xc_max 1650 //330*5 #define m0_xc_min 0 #define m1_xc_min -375 //-(150*5/2) #define m2_xc_min -1000 //-200*5 #define m3_xc_min 0 #define m4_xc_min 0 //电机初始状态 #define m0_re_state 0 #define m1_re_state 0 #define m2_re_state 0 #define m3_re_state 0 #define m4_re_state 0 #define m5_re_state 0 //电机驱动的宏定义 #define motor00_stop HAL_GPIO_WritePin(M0__GPIO_Port,M0__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (MO__GPIO_Port ,MO__Pin,GPIO_PIN_RESET) #define motor00_zhengzhuan HAL_GPIO_WritePin (M0__GPIO_Port ,M0__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (MO__GPIO_Port ,MO__Pin ,GPIO_PIN_RESET) #define motor00_fanzhuan HAL_GPIO_WritePin (M0__GPIO_Port ,M0__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (MO__GPIO_Port ,MO__Pin ,GPIO_PIN_SET) #define motor01_stop HAL_GPIO_WritePin (M1__GPIO_Port ,M1__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M1_F5_GPIO_Port ,M1_F5_Pin ,GPIO_PIN_RESET) #define motor01_zhengzhuan HAL_GPIO_WritePin (M1__GPIO_Port ,M1__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M1_F5_GPIO_Port ,M1_F5_Pin ,GPIO_PIN_RESET) #define motor01_fanzhuan HAL_GPIO_WritePin (M1__GPIO_Port ,M1__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M1_F5_GPIO_Port ,M1_F5_Pin ,GPIO_PIN_SET) #define motor02_stop HAL_GPIO_WritePin (M2__GPIO_Port ,M2__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M2_B10_GPIO_Port ,M2_B10_Pin ,GPIO_PIN_RESET) #define motor02_zhengzhuan HAL_GPIO_WritePin (M2__GPIO_Port ,M2__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M2_B10_GPIO_Port ,M2_B10_Pin ,GPIO_PIN_SET) #define motor02_fanzhuan HAL_GPIO_WritePin (M2__GPIO_Port ,M2__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M2_B10_GPIO_Port ,M2_B10_Pin ,GPIO_PIN_RESET) #define motor03_stop HAL_GPIO_WritePin(M3__GPIO_Port ,M3__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M3_C9_GPIO_Port,M3_C9_Pin,GPIO_PIN_RESET) #define motor03_zhengzhuan HAL_GPIO_WritePin(M3__GPIO_Port ,M3__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M3_C9_GPIO_Port ,M3_C9_Pin ,GPIO_PIN_RESET) #define motor03_fanzhuan HAL_GPIO_WritePin (M3__GPIO_Port ,M3__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M3_C9_GPIO_Port ,M3_C9_Pin ,GPIO_PIN_SET) #define motor04_stop HAL_GPIO_WritePin (M4__GPIO_Port ,M4__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M4_C14_GPIO_Port ,M4_C14_Pin ,GPIO_PIN_RESET) #define motor04_zhengzhuan HAL_GPIO_WritePin (M4__GPIO_Port ,M4__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M4_C14_GPIO_Port ,M4_C14_Pin ,GPIO_PIN_RESET) #define motor04_fanzhuan HAL_GPIO_WritePin (M4__GPIO_Port ,M4__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M4_C14_GPIO_Port ,M4_C14_Pin ,GPIO_PIN_SET) //motor0_x:电机名称,position:电机的位置,mode:电机模式,0停止,1电机正转,2电机反转 void motor_control(int motor0_x,int *position,int mode) { int xc_max,xc_min; switch(motor0_x) { case 0: { xc_max=m0_xc_max; xc_min=m0_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor00_stop; } else if(*position<xc_max) { motor00_zhengzhuan; *position +=1; } }break; case 2: { if(*position==xc_min) { motor00_stop; } else if(*position>xc_min) { motor00_fanzhuan; *position -=1; } }break; default : { motor00_stop; } } }break; case 1: { xc_max=m1_xc_max; xc_min=m1_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor01_stop; } else if(*position<xc_max) { motor01_zhengzhuan; *position +=1; } }break; case 2: { if(*position==xc_min) { motor01_stop; } else if(*position>xc_min) { motor01_fanzhuan; *position -=1; } }break; default : { motor01_stop; } } }break; case 2: { xc_max=m2_xc_max; xc_min=m2_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor02_stop; } else if(*position<xc_max) { motor02_zhengzhuan; *position +=1; } }break; case 2: { if(*position==xc_min) { motor02_stop; } else if(*position>xc_min) { motor02_fanzhuan; *position -=1; } }break; default : { motor02_stop; } } }break; case 3: { xc_max=m3_xc_max; xc_min=m3_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor03_stop; } else if(*position<xc_max) { motor03_zhengzhuan; *position +=1; } }break; case 2: { if(*position==xc_min) { motor03_stop; } else if(*position>xc_min) { motor03_fanzhuan; *position -=1; } }break; default : { motor03_stop; } } }break; case 4: { xc_max=m4_xc_max; xc_min=m4_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor04_stop; } else if(*position<xc_max) { motor04_zhengzhuan; *position +=1; } }break; case 2: { if(*position==xc_min) { motor04_stop; } else if(*position>xc_min) { motor04_fanzhuan; *position -=1; } }break; default : { motor04_stop; } } }break; default :; } }//main.c //IO定义 static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, M4__Pin|M4_C14_Pin|M5__Pin|M3__Pin |M3_C9_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, M0__Pin|MO__Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOF, M1__Pin|M1_F5_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, M2__Pin|M2_B10_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : M4__Pin M4_C14_Pin M5__Pin */ GPIO_InitStruct.Pin = M4__Pin|M4_C14_Pin|M5__Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pins : M0__Pin MO__Pin */ GPIO_InitStruct.Pin = M0__Pin|MO__Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : M1__Pin M1_F5_Pin */ GPIO_InitStruct.Pin = M1__Pin|M1_F5_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /*Configure GPIO pins : M2__Pin M2_B10_Pin */ GPIO_InitStruct.Pin = M2__Pin|M2_B10_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pins : M3__Pin M3_C9_Pin */ GPIO_InitStruct.Pin = M3__Pin|M3_C9_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); } /* USART1 init function */ static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } //将接受过来的信息进行处理,其中case11,12复位 void xinxichuli(uint8_t x) { switch(x) { case 1: { motor_control (0,&m0_position,1); }break; case 2: { motor_control(0,&m0_position,2); }break; case 3: { motor_control(1,&m1_position,1); }break; case 4: { motor_control(1,&m1_position,2); }break; case 5: { motor_control(2,&m2_position,2); }break; case 6: { motor_control(2,&m2_position,1); }break; case 7://需要修改 { while(1) { if(m1_position ==m1_re_state ) { do { motor_control(1,&m1_position,1); }while(m1_position !=m1_xc_max); } else if(m1_position ==m1_xc_max ) { do { motor_control(M1,&m1_position,2); }while(m1_position !=m1_re_state ); } else if (m1_position ==m1_xc_min ) { do { motor_control(M1,&m1_position,1); }while(m1_position !=m1_re_state ); motor_control(M1,&m1_position,0); } for(int w=0;w<fs_time ;w++) { for(int q=0;q<60;q++) { HAL_Delay (1000); HAL_UART_Receive_IT(&huart1,rec,1); if( rec[0] ==u2_send_buff [11] || rec[0] ==u2_send_buff [12]) { xinxichuli (rec[0]); goto marking; } } } } marking : HAL_Delay(50); }break; case 8://待修改 { if(fs_set_time ==4) { fs_set_time =0; } fs_set_time ++; switch(fs_set_time ) { case 1:fs_time =15;break; case 2:fs_time =30;break; case 3:fs_time=45; break; case 4: fs_time =60;break; default :;break; } }break; case 9://待修改 { if(m0_position !=m0_xc_max ) { do { // motor0 (1); motor_control(M0,&m0_position,1); }while(m0_position !=m0_xc_max ); if(m0_position ==m0_xc_max) { motor_control(M0,&m0_position,0); } } else if(m2_position !=m2_xc_min ) { do { motor_control(M2,&m2_position,2); }while(m2_position !=m2_xc_min ); if(m2_position ==m2_xc_min ) { motor_control(M2,&m2_position,0); } } }break; case 10://待修改 { if(m2_position !=m2_xc_max ) { do { // motor2 (1); motor_control(M2,&m2_position ,1); }while(m2_position !=m2_xc_max ); if(m2_position ==m2_xc_max ) { motor_control(M2,&m2_position,0); } } else if(m0_position !=m0_xc_min ) { do { motor_control(M0,&m0_position,2); }while(m0_position !=m0_xc_min ); if(m0_position ==m0_xc_min ) { motor_control(M0,&m0_position,0); } } }break; case 11: { /* if(m1_position >m1_re_state ) { for(int qqe=m1_position ;qqe>m1_re_state ;qqe--) { motor_control(1,&m1_position,2); } if(m1_position==m1_re_state) { motor_control(M1,&m1_position,0); } } else if(m1_position <m1_re_state ) { for(int qqew=m1_position ;qqew>m1_re_state ;qqew--) { // motor1 (2); motor_control (M1,&m1_position ,2); } if(m1_position==m1_re_state) { motor_control(M1,&m1_position,0); } } else if(m0_position >m0_re_state ) { while(m0_position>m0_re_state ) { motor_control(M0,&m0_position,2); } motor_control(M0,&m0_position,0); } else if(m0_position <m0_re_state ) { while(m0_position<m0_re_state ) { motor_control(M0,&m0_position,1); } motor_control(M0,&m0_position,0); } else if(m2_position >m2_re_state ) { while(m2_position>m2_re_state ) { motor_control(M2,&m2_position,2); } motor_control(M2,&m2_position,0); } else if(m2_position <m2_re_state ) { while(m2_position<m2_re_state ) { motor_control(M2,&m2_position,1); } motor_control(M2,&m2_position,0); } */ ////////////////// if (m1_position >m1_re_state) { for (int qqe = m1_position; qqe>m1_re_state; qqe--) { motor_control(M1,&m1_position,2); } if (m1_position == m1_re_state) { motor_control(M1,&m1_position,0); } } else if (m1_position <m1_re_state) { for (int qqew = m1_position; qqew>m1_re_state; qqew--) { motor_control(M1,&m1_position,2); } if (m1_position == m1_re_state) { motor_control(M1,&m1_position,0); } } else if (m0_position >m0_re_state) { while (m0_position>m0_re_state) { motor_control(M0,&m0_position,2); } motor_control(M0,&m0_position,0); } else if (m0_position <m0_re_state) { while (m0_position<m0_re_state) { motor_control(M0,&m0_position,1); } motor_control(M0,&m0_position,0); } else if (m2_position >m2_re_state) { while (m2_position>m2_re_state) { motor_control(M2,&m2_position,2); } motor_control(M2,&m2_position,0); } else if (m2_position <m2_re_state) { while (m2_position<m2_re_state) { motor_control(M2,&m2_position,1); } motor_control(M2,&m2_position,0); } }break; case 12: { while (m1_position != m1_re_state || m0_position != m0_re_state || m2_position != m2_re_state) { if (m1_position != m1_re_state) { if (m1_position >m1_re_state) { do { motor_control(M1,&m1_position,2); } while (m1_position != m1_re_state); if (m1_position == m1_re_state) { motor_control(M1,&m1_position,0); } } else if (m1_position < m1_re_state) { do { motor_control(M1,&m1_position,1); } while (m1_position != m1_re_state); if (m1_position == m1_re_state) { motor_control(M1,&m1_position,0); } } } else if (m2_position != m2_re_state) { if (m2_position > m2_re_state) { do { motor_control(M2,&m2_position,2); } while (m2_position != m2_re_state); if (m2_position == m2_re_state) { motor_control(M2,&m2_position,0); } } else if (m2_position < m2_re_state) { do { motor_control(M2,&m2_position,1); } while (m2_position != m2_re_state); if (m2_position == m2_re_state) { motor_control(M2,&m2_position,0); } } if (m2_position == m2_re_state) { motor_control(M2,&m2_position,0); } } else if (m0_position != m0_re_state) { if (m0_position > m0_re_state) { do { motor_control(M0,&m0_position,2); } while (m0_position != m0_re_state); } else if (m0_position < m0_re_state) { do { motor_control(M0,&m0_position,1); } while (m0_position != m0_re_state); } else if (m0_position != m0_re_state) { motor_control(M0,&m0_position,0); } } } }break; case 31: { //motor0 (0); motor_control(M0,&m0_position,0); }break; case 32: { // motor0 (0); motor_control(M0,&m0_position,0); }break; case 33: { motor_control(M1,&m1_position,0); }break; case 34: { motor_control(M1,&m1_position,0); }break; case 35: { motor_control(M2,&m2_position,0); }break; case 36: { motor_control(M2,&m2_position,0); }break; } //int main()中的WHILE while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ do { R_stauts =HAL_UART_Receive_IT(&huart1,rec,1); }while(R_stauts != HAL_OK );//接受遥控发送过来的数据 //根据主板状态的不同,处理遥控的数据 xinxichuli (rec[0]); } 从上位机接受到信息1~6电机会进行相应的动作,在电机运行的时候电机的位置也在进行相应的累加或者累减, 在电机M0正转后 (即时接受到信息1),我发送复位信息11,或者12进行复位。在下位机接收到11的时候电机的复位没有动作,但是电机位置信息值变动成初始值,而接受到复位信息12的时候电机M0有反转,但是达到最小行程的电机的继电器还在耦合没有释放,而电机位置信息值同样变动成初始值。求大佬告知原因 |
master.zip
下载8.66 KB, 下载次数: 1, 下载积分: ST金币 -1
æºæ件
stlink V3mini 调试器不能识别stm32f030F4P6芯片,有解决办法吗?
STM32F0 SMBus 中断怎么开启
STM32F072 无法清除EXTI4_15_IRQn pending标志,导致无法进入stop模式
基于GCC编译器的initcall在STM32如何实现?
想找一款pin to pin的MCU(参考STM32F105RC),支持CAN FD的,64PIN封装,各位小伙伴们有推荐吗
L9663如何使用上行缓冲区发送同步脉冲
如何使用定时器在任意IO输出PWM
下载到flash无法运行,开调试就能成功,是什么问题?
STM32是小端模式,但为什么Keil中memory窗口和反汇编窗口的32位指令不一致?
各位朋友,有没有STM32F0系列教程,最好是汇编语言的。
恩 我再编辑补上
还有你这代码 感觉逻辑上有点不太合理 m0_position 通过指针传递给控制函数 在函数内部有对m0_position进行修改 退出又补上 m0_position = i0 感觉那里不得劲
评分
查看全部评分
那个我是用来测试用的, 原来的代码是没有这个的
评分
查看全部评分