printf("Configuring Initial Parameters of mcpwm bldc control...\n");
mcpwm_config_tpwm_config;
pwm_config.frequency=1000;//frequency = 1000Hz
pwm_config.cmpr_a=50.0;//duty cycle of PWMxA = 50.0%
pwm_config.cmpr_b=50.0;//duty cycle of PWMxb = 50.0%
pwm_config.counter_mode=MCPWM_UP_COUNTER;
pwm_config.duty_mode=MCPWM_DUTY_MODE_1;
mcpwm_init(MCPWM_UNIT_0,MCPWM_TIMER_0,&pwm_config);//Configure PWM0A & PWM0B with above settings
mcpwm_init(MCPWM_UNIT_0,MCPWM_TIMER_1,&pwm_config);//Configure PWM1A & PWM1B with above settings
mcpwm_init(MCPWM_UNIT_0,MCPWM_TIMER_2,&pwm_config);//Configure PWM2A & PWM2B with above settings
//3. Capture configuration
//configure CAP0, CAP1 and CAP2 signal to start capture counter on rising edge
//we generate a gpio_test_signal of 20ms on GPIO 12 and connect it to one of the capture signal, the disp_captured_function displays the time between rising edge
//In general practice you can connect Capture to external signal, measure time between rising edge or falling edge and take action accordingly
mcpwm_capture_enable(MCPWM_UNIT_0,MCPWM_SELECT_CAP0,MCPWM_POS_EDGE,0);//capture signal on rising edge, pulse num = 0 i.e. 800,000,000 counts is equal to one second
mcpwm_capture_enable(MCPWM_UNIT_0,MCPWM_SELECT_CAP1,MCPWM_POS_EDGE,0);//capture signal on rising edge, pulse num = 0 i.e. 800,000,000 counts is equal to one second
mcpwm_capture_enable(MCPWM_UNIT_0,MCPWM_SELECT_CAP2,MCPWM_POS_EDGE,0);//capture signal on rising edge, pulse num = 0 i.e. 800,000,000 counts is equal to one second
//enable interrupt, so each this a rising edge occurs interrupt is triggered
MCPWM[MCPWM_UNIT_0]->int_ena.val=(CAP0_INT_EN|CAP1_INT_EN|CAP2_INT_EN);//Enable interrupt on CAP0, CAP1 and CAP2 signal
//MCPWMXA to duty mode 1 and MCPWMXB to duty mode 0 or vice versa will generate MCPWM compliment signal of each other, there are also other ways to do it
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_OPR_A,MCPWM_DUTY_MODE_1);//Set PWM0A to duty mode one
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_OPR_B,MCPWM_DUTY_MODE_0);//Set PWM0B back to duty mode zero
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_BYPASS_FED,100,100);//Deadtime of 10us
//MCPWMXA to duty mode 1 and MCPWMXB to duty mode 0 or vice versa will generate MCPWM compliment signal of each other, there are also other ways to do it
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_2,MCPWM_OPR_A,MCPWM_DUTY_MODE_1);//Set PWM2A to duty mode one
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_2,MCPWM_OPR_B,MCPWM_DUTY_MODE_0);//Set PWM2B back to duty mode zero
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_2,MCPWM_BYPASS_FED,100,100);//Deadtime of 10us
//MCPWMXA to duty mode 1 and MCPWMXB to duty mode 0 or vice versa will generate MCPWM compliment signal of each other, there are also other ways to do it
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_2,MCPWM_OPR_A,MCPWM_DUTY_MODE_1);//Set PWM2A to duty mode one
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_2,MCPWM_OPR_B,MCPWM_DUTY_MODE_0);//Set PWM2B back to duty mode zero
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_2,MCPWM_BYPASS_FED,100,100);//Deadtime of 10us
//MCPWMXA to duty mode 1 and MCPWMXB to duty mode 0 or vice versa will generate MCPWM compliment signal of each other, there are also other ways to do it
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_OPR_A,MCPWM_DUTY_MODE_1);//Set PWM1A to duty mode one
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_OPR_B,MCPWM_DUTY_MODE_0);//Set PWM1B back to duty mode zero
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_BYPASS_FED,100,100);//Deadtime of 10uss
//MCPWMXA to duty mode 1 and MCPWMXB to duty mode 0 or vice versa will generate MCPWM compliment signal of each other, there are also other ways to do it
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_OPR_A,MCPWM_DUTY_MODE_1);//Set PWM1A to duty mode one
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_OPR_B,MCPWM_DUTY_MODE_0);//Set PWM1B back to duty mode zero
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_1,MCPWM_BYPASS_FED,100,100);//Deadtime of 10uss
//MCPWMXA to duty mode 1 and MCPWMXB to duty mode 0 or vice versa will generate MCPWM compliment signal of each other, there are also other ways to do it
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_OPR_A,MCPWM_DUTY_MODE_1);//Set PWM0A to duty mode one
mcpwm_set_duty_type(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_OPR_B,MCPWM_DUTY_MODE_0);//Set PWM0B back to duty mode zero
mcpwm_deadtime_enable(MCPWM_UNIT_0,MCPWM_TIMER_0,MCPWM_BYPASS_FED,100,100);//Deadtime of 10us