| | |
| | | #include "gpio.h" |
| | | #include "motor.h" |
| | | #include "gd32e23x_exti.h" |
| | | static motor_rotate_t motor_drive = {0}; |
| | | /** |
| | | * @brief ʹÄÜ»ô¶û´«¸ÐÆ÷ |
| | | * @param ÎÞ |
| | |
| | | void hall_enable(void) |
| | | { |
| | | /* ʹÄÜ»ô¶û´«¸ÐÆ÷½Ó¿Ú */ |
| | | __HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_TRIGGER); |
| | | __HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_UPDATE); |
| | | exti_interrupt_enable(HALL_A_EXTI); |
| | | exti_interrupt_enable(HALL_B_EXTI); |
| | | exti_interrupt_enable(HALL_C_EXTI); |
| | | |
| | | HAL_TIMEx_HallSensor_Start(&htimx_hall); |
| | | // HAL_TIMEx_HallSensor_Start(&htimx_hall); |
| | | |
| | | LED1_OFF; |
| | | // LED1_OFF; |
| | | |
| | | HAL_TIM_TriggerCallback(&htimx_hall); // Ö´ÐÐÒ»´Î»»Ïà |
| | | // HAL_TIM_TriggerCallback(&htimx_hall); // Ö´ÐÐÒ»´Î»»Ïà |
| | | |
| | | motor_drive.enable_flag = 1; |
| | | } |
| | |
| | | void hall_disable(void) |
| | | { |
| | | /* ½ûÓûô¶û´«¸ÐÆ÷½Ó¿Ú */ |
| | | __HAL_TIM_DISABLE_IT(&htimx_hall, TIM_IT_TRIGGER); |
| | | __HAL_TIM_DISABLE_IT(&htimx_hall, TIM_IT_UPDATE); |
| | | HAL_TIMEx_HallSensor_Stop(&htimx_hall); |
| | | exti_interrupt_disable(HALL_A_EXTI); |
| | | exti_interrupt_disable(HALL_B_EXTI); |
| | | exti_interrupt_disable(HALL_C_EXTI); |
| | | // HAL_TIMEx_HallSensor_Stop(&htimx_hall); |
| | | motor_drive.enable_flag = 0; |
| | | motor_drive.speed = 0; |
| | | } |
| | |
| | | |
| | | #if 1 |
| | | /* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ U µÄ״̬ */ |
| | | if (HAL_GPIO_ReadPin(HALL_INPUTU_GPIO_PORT, HALL_INPUTU_PIN) != GPIO_PIN_RESET) |
| | | if (Get_HallSensorA_State()) |
| | | { |
| | | state |= 0x01U << 0; |
| | | } |
| | | |
| | | /* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ V µÄ״̬ */ |
| | | if (HAL_GPIO_ReadPin(HALL_INPUTV_GPIO_PORT, HALL_INPUTV_PIN) != GPIO_PIN_RESET) |
| | | if (Get_HallSensorB_State()) |
| | | { |
| | | state |= 0x01U << 1; |
| | | } |
| | | |
| | | /* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ W µÄ״̬ */ |
| | | if (HAL_GPIO_ReadPin(HALL_INPUTW_GPIO_PORT, HALL_INPUTW_PIN) != GPIO_PIN_RESET) |
| | | if (Get_HallSensorC_State()) |
| | | { |
| | | state |= 0x01U << 2; |
| | | } |
| | |
| | | |
| | | return state; // ·µ»Ø´«¸ÐÆ÷״̬ |
| | | } |
| | | |
| | | static uint8_t count = 0; |
| | | static void update_motor_speed(uint8_t dir_in, uint32_t time) |
| | | { |
| | | int speed_temp = 0; |
| | |
| | | float f = 0; |
| | | |
| | | /* ¼ÆËãËÙ¶È£º |
| | | µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(72000000.0/128.0)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(72000000.0/128.0) * time)Ϊʱ¼ä³¤¡£ |
| | | µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(48000000.0/128.0)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(48000000.0/128.0) * time)Ϊʱ¼ä³¤¡£ |
| | | */ |
| | | |
| | | if (time == 0) |
| | | motor_drive.speed_group[count++] = 0; |
| | | else |
| | | { |
| | | f = (1.0f / (72000000.0f / HALL_PRESCALER_COUNT) * time); |
| | | f = (1.0f / (48000000.0f / HALL_PRESCALER_COUNT) * time); |
| | | f = (1.0f / 12.0f) / (f / 60.0f); |
| | | motor_drive.speed_group[count++] = f; |
| | | } |
| | |
| | | |
| | | /** |
| | | * @brief »ô¶û´«¸ÐÆ÷´¥·¢»Øµ÷º¯Êý |
| | | * @param htim:¶¨Ê±Æ÷¾ä±ú |
| | | * @param void |
| | | * @retval ÎÞ |
| | | */ |
| | | void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim) |
| | | void HAL_HallExti_TriggerCallback(void) |
| | | { |
| | | /* »ñÈ¡»ô¶û´«¸ÐÆ÷Òý½Å״̬,×÷Ϊ»»ÏàµÄÒÀ¾Ý */ |
| | | uint8_t step = 0; |
| | | step = get_hall_state(); |
| | | |
| | | if (htim == &htimx_hall) // ÅжÏÊÇ·ñÓÉ´¥·¢ÖжϲúÉú |
| | | if (exti_interrupt_flag_get(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI)) // ÅжÏÊÇ·ñÓÉ´¥·¢ÖжϲúÉú |
| | | { |
| | | update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1)); |
| | | // update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1));//TODO Ö÷¶¨Ê±Æ÷´¦»ñµÃ¼ÆÊýʱ¼ä |
| | | motor_drive.timeout = 0; |
| | | exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI); |
| | | } |
| | | |
| | | if (get_bldcm_direction() == MOTOR_FWD) |