调整时钟64MHz。
RTT任务增加马达开启和关闭功能
| | |
| | | #ifndef _CLOCK_H_ |
| | | #define _CLOCK_H_ |
| | | #include "stdint.h" |
| | | #define SYSTEM_CLOCK (48000000UL) |
| | | #define SYSTEM_CLOCK (64000000UL) |
| | | void Clock_Config(void); |
| | | void Ostick_config(void); |
| | | void delay_1ms(uint32_t count); |
| | |
| | | /* ÀÛ¼Æ TIM_Period¸öºó²úÉúÒ»¸ö¸üлòÕßÖÐ¶Ï |
| | | µ±¶¨Ê±Æ÷´Ó0¼ÆÊýµ½65535£¬¼´Îª65535´Î£¬ÎªÒ»¸ö¼ÆÊýÖÜÆÚ */ |
| | | #define HALL_PERIOD_COUNT (0xFFFF) |
| | | #define SPEED_PRESCALER_COUNT (4800u) |
| | | #define SPEED_PRESCALER_COUNT (6400u) |
| | | #define SPEED_PERIOD_COUNT (30000u) |
| | | /* ͨÓÿØÖƶ¨Ê±Æ÷ʱÖÓÔ´TIMxCLK = HCLK = 64MHz |
| | | É趨¶¨Ê±Æ÷ƵÂÊΪ = TIMxCLK / (SPEED_PRESCALER_COUNT) / SPEED_PERIOD_COUNT = 33.333Hz |
| | |
| | | #ifndef _PWM_H_ |
| | | #define _PWM_H_ |
| | | #include "gd32e23x_timer.h" |
| | | //100us * 30000 = 3s |
| | | //1us * 30000 = 30ms |
| | | #define PERIOD_CAP (30000U) |
| | | #define SPEED_TIMEOUT (300U) //9S |
| | | |
| | | //48Mzhz / 3000 = 16Khz = 62.5us |
| | | #define PERIOD_CMP (3000u) |
| | | //64Mzhz / 3200 = 20Khz = 50us |
| | | #define PERIOD_CMP (3200u) |
| | | #define TIMER2_PERIOD (63U) |
| | | |
| | | void TimerInit(void); |
| | | void SetPwmDuty(uint16_t ch, uint32_t duty); |
| | |
| | | rcu_osci_on(RCU_IRC8M); //ʹÓÃÄÚ²¿8MRCʱÖÓ |
| | | rcu_osci_stab_wait(RCU_IRC8M); |
| | | rcu_system_clock_source_config(RCU_CKSYSSRC_PLL); //sysclk is PLL |
| | | rcu_pll_config(RCU_PLLSRC_IRC8M_DIV2, RCU_PLL_MUL12); //8MHz/2 =4MHz PLL =4MHz*12 =48MHz |
| | | rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1); //AHB 48M |
| | | rcu_apb1_clock_config(RCU_APB1_CKAHB_DIV1); //APB1 48M |
| | | rcu_apb2_clock_config(RCU_APB2_CKAHB_DIV1); //APB2 48M |
| | | rcu_adc_clock_config(RCU_ADCCK_APB2_DIV4); //max 14M,current:48/4=12M |
| | | rcu_pll_config(RCU_PLLSRC_IRC8M_DIV2, RCU_PLL_MUL16); //8MHz/2 =4MHz PLL =4MHz*16 =64MHz |
| | | rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1); //AHB 64M |
| | | rcu_apb1_clock_config(RCU_APB1_CKAHB_DIV1); //APB1 64M |
| | | rcu_apb2_clock_config(RCU_APB2_CKAHB_DIV1); //APB2 64M |
| | | rcu_adc_clock_config(RCU_ADCCK_APB2_DIV8); //max 14M,current:64/8=8M |
| | | rcu_usart_clock_config(RCU_USART0SRC_IRC8M); //USART0 8M |
| | | rcu_rtc_clock_config(RCU_RTCSRC_NONE); |
| | | |
| | |
| | | motor_drive.timeout = 0; |
| | | motor_drive.speed = 0; |
| | | motor_drive.enable_flag = 0; |
| | | motor_pluse = 2000; |
| | | motor_pluse = 0; |
| | | memset(motor_drive.speed_group, 0, SPEED_FILTER_NUM); |
| | | |
| | | hall_enable(); |
| | | // hall_enable(); |
| | | }; |
| | | /** |
| | | * @brief ʹÄÜ»ô¶û´«¸ÐÆ÷ |
| | |
| | | float f = 0; |
| | | |
| | | /* ¼ÆËãËÙ¶È£º |
| | | µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(48000000.0/6400)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(48000000.0/6400) * time)Ϊʱ¼ä³¤¡£ |
| | | µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(64000000.0/6400)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(64000000.0/6400) * time)Ϊʱ¼ä³¤¡£ |
| | | */ |
| | | |
| | | if (time == 0) |
| | | motor_drive.speed_group[count++] = 0; |
| | | else |
| | | { |
| | | // f = (1.0f / (48000000.0f / SPEED_PRESCALER_COUNT) * time); |
| | | // f = (1.0f / (64000000.0f / SPEED_PRESCALER_COUNT) * time); |
| | | // f = (1.0f / 12.0f) / (f / 60.0f); |
| | | f = time; |
| | | motor_drive.speed_group[count++] = f; |
| | |
| | | // /* »ñÈ¡»ô¶û´«¸ÐÆ÷Òý½Å״̬,×÷Ϊ»»ÏàµÄÒÀ¾Ý */ |
| | | 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)); //TODO Ö÷¶¨Ê±Æ÷´¦»ñµÃ¼ÆÊýʱ¼ä |
| | | update_motor_speed(motor_step, 300u * GetSpeedTimerOutcnt() + timer_counter_read(TIMER2)); |
| | | SEGGER_RTT_printf(0, "Hall state is:%d!\n", motor_step); |
| | | // update_motor_speed(motor_step, 300u * GetSpeedTimerOutcnt() + timer_counter_read(TIMER2)); |
| | | SEGGER_RTT_printf(0, "motor speed is:%d,%d!\n", GetSpeedTimerOutcnt(), timer_counter_read(TIMER2)); |
| | | timer_counter_value_config(TIMER2, 0); |
| | | motor_drive.timeout = 0; |
| | | // // exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI); |
| | | } |
| | | |
| | | // if (RESET != exti_interrupt_flag_get(EXTI_4)) |
| | | // { |
| | | // SEGGER_RTT_printf(0, "HALL_A_EXTI triggle!\n"); |
| | | // } |
| | | // else if (RESET != exti_interrupt_flag_get(EXTI_5)) |
| | | // { |
| | | // SEGGER_RTT_printf(0, "HALL_C_EXTI triggle!\n"); |
| | | // } |
| | | // else if (RESET != exti_interrupt_flag_get(EXTI_15)) |
| | | // { |
| | | // SEGGER_RTT_printf(0, "HALL_B_EXTI triggle!\n"); |
| | | // } |
| | | motor_phasechange(); |
| | | // HAL_TIM_GenerateEvent(&htimx_bldcm, TIM_EVENTSOURCE_COM); // Èí¼þ²úÉú»»Ïàʼþ£¬´Ëʱ²Å½«ÅäÖÃдÈë |
| | | } |
| | |
| | | */ |
| | | extern void stop_pwm_output(void) |
| | | { |
| | | timer_channel_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCX_DISABLE); |
| | | // timer_channel_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCX_DISABLE); |
| | | // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCXN_DISABLE); |
| | | timer_channel_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCX_DISABLE); |
| | | // timer_channel_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCX_DISABLE); |
| | | // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCXN_DISABLE); |
| | | timer_channel_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCX_DISABLE); |
| | | // timer_channel_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCX_DISABLE); |
| | | // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCXN_DISABLE); |
| | | SetPwmDuty(MOTOR_OUT_CH_U, 0); |
| | | SetPwmDuty(MOTOR_OUT_CH_V, 0); |
| | | SetPwmDuty(MOTOR_OUT_CH_W, 0); |
| | | MOTOR_W_L_DISABLE; |
| | | MOTOR_V_L_DISABLE; |
| | | MOTOR_U_L_DISABLE; |
| | |
| | | #include "gd32e23x.h" |
| | | #include "pwm.h" |
| | | #include "motor.h" |
| | | #include "bldc_ctrl.h" |
| | | static uint32_t RttDataOutPutFlag = 0; |
| | | static void RTT_KeyValue(uint32_t key); |
| | | extern void RTT_TaskInit(void) |
| | |
| | | } |
| | | if (0 != RttDataOutPutFlag) |
| | | { |
| | | SEGGER_RTT_printf(0, "%d\n", output_index++); |
| | | // SEGGER_RTT_printf(0, "%d\n", output_index++); |
| | | output_index = (uint32_t)get_motor_speed(); |
| | | SEGGER_RTT_printf(0, "motor speed is:%d!\n", output_index); |
| | | } |
| | | } |
| | | |
| | |
| | | case 'd': |
| | | case 'D': |
| | | RttDataOutPutFlag = !RttDataOutPutFlag; |
| | | SEGGER_RTT_printf(0, "Index:\tTemp\tBase\tCP\tleakCurr\tCurr\tAC_Vot\tState\tError\n"); |
| | | SEGGER_RTT_printf(0, "RTT Output data stop or start!\n"); |
| | | break; |
| | | case 'v': |
| | | case 'V': |
| | |
| | | SEGGER_RTT_printf(0, "Software Compile time is :%s %s\n\r", &COMPILE_DATE, &COMPILE_TIME); |
| | | break; |
| | | case 'o': |
| | | timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0); |
| | | timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE); |
| | | case 'O': |
| | | set_bldcm_speed(2000); |
| | | set_bldcm_enable(); |
| | | break; |
| | | case 's': |
| | | timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE); |
| | | case 'S': |
| | | set_bldcm_disable(); |
| | | break; |
| | | default: |
| | | break;` |
| | | break; |
| | | } |
| | | } |
| | |
| | | Set_SDH2136_Enable(); |
| | | // Set_SDH2136_Disable(); |
| | | bldcm_data.direction = MOTOR_FWD; |
| | | bldcm_data.is_enable = 1; |
| | | bldcm_data.dutyfactor = 1500; |
| | | bldcm_data.is_enable = 0; |
| | | bldcm_data.dutyfactor = 2000; |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | */ |
| | | #include "pwm.h" |
| | | #include "bldc_ctrl.h" |
| | | static volatile uint32_t timer_outcnt = 0; |
| | | /***************************************************************************************************** |
| | | Timer2 is used to speed cal |
| | |
| | | |
| | | timer_deinit(TIMER2); |
| | | |
| | | timercontralcfg.prescaler = 4799; //48Mhz /4800 =0.01Mhz = 100us |
| | | timercontralcfg.prescaler = TIMER2_PERIOD; //64Mhz /64 =1Mhz = 1us |
| | | timercontralcfg.alignedmode = TIMER_COUNTER_EDGE; |
| | | timercontralcfg.counterdirection = TIMER_COUNTER_UP; |
| | | timercontralcfg.period = PERIOD_CAP; //100us*10000=1s |
| | | timercontralcfg.period = PERIOD_CAP; //1us*30000=30ms |
| | | |
| | | timer_init(TIMER2, &timercontralcfg); |
| | | timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP); |
| | |
| | | } |
| | | extern void TIMER2_IRQHandler_CallBack(void) |
| | | { |
| | | timer_outcnt++; |
| | | if (++timer_outcnt > SPEED_TIMEOUT) |
| | | { |
| | | //to do |
| | | set_bldcm_disable(); |
| | | } |
| | | } |
| | | |
| | | extern uint32_t GetSpeedTimerOutcnt(void) |