22 TIMER_CH0CV(TIMER1) = (uint32_t) ((
float)
PWM_PERIOD * ch0);
23 TIMER_CH1CV(TIMER1) = (uint32_t) ((
float)
PWM_PERIOD * ch1);
24 TIMER_CH2CV(TIMER1) = (uint32_t) ((
float)
PWM_PERIOD * ch2);
32 timer_disable(TIMER13);
33 timer_deinit(TIMER13);
36 timer_interrupt_disable(TIMER13, TIMER_INT_UP);
37 nvic_irq_disable(TIMER13_IRQn);
38 rcu_periph_clock_disable(RCU_TIMER13);
46 timer_disable(TIMER2);
50 timer_interrupt_disable(TIMER2, TIMER_INT_UP);
51 nvic_irq_disable(TIMER2_IRQn);
52 rcu_periph_clock_disable(RCU_TIMER2);
62 timer_parameter_struct timer_initpara;
65 rcu_periph_clock_enable(RCU_TIMER2);
69 timer_initpara.prescaler = 71;
72 timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
73 timer_initpara.counterdirection = TIMER_COUNTER_UP;
77 timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
78 timer_init(TIMER2, &timer_initpara);
81 timer_interrupt_enable(TIMER2, TIMER_INT_UP);
90 timer_parameter_struct timer_initpara;
93 rcu_periph_clock_enable(RCU_TIMER13);
94 timer_deinit(TIMER13);
97 timer_initpara.prescaler = 71;
100 timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
101 timer_initpara.counterdirection = TIMER_COUNTER_UP;
105 timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
106 timer_init(TIMER13, &timer_initpara);
109 timer_interrupt_enable(TIMER13, TIMER_INT_UP);
110 timer_enable(TIMER13);
118 timer_oc_parameter_struct timer_ocintpara;
119 timer_parameter_struct timer_initpara;
122 rcu_periph_clock_enable(RCU_GPIOA);
123 rcu_periph_clock_enable(RCU_TIMER1);
124 timer_deinit(TIMER1);
127 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
128 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
131 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
132 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
135 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
136 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
139 gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_0);
140 gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_1);
141 gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_2);
144 timer_initpara.prescaler = 1;
147 timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
148 timer_initpara.counterdirection = TIMER_COUNTER_UP;
152 timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
153 timer_initpara.repetitioncounter = 0;
154 timer_init(TIMER1, &timer_initpara);
157 timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
158 timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
159 timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);
160 timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
161 timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocintpara);
164 timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0,
PWM_PERIOD / 2);
165 timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
166 timer_channel_output_shadow_config(TIMER1, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
169 timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1,
PWM_PERIOD / 2);
170 timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
171 timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
174 timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2,
PWM_PERIOD / 2);
175 timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM0);
176 timer_channel_output_shadow_config(TIMER1, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
179 timer_auto_reload_shadow_enable(TIMER1);
180 timer_enable(TIMER1);
used to place important parameter configurations for users
#define TIM13_PRIORITY
TIMER13 preemption priority set to 3.
#define TIM2_FREQUENCY
set FOC loop frequency to 8kHz
#define TIM13_FREQUENCY
set speed/angle PID loop frequency to 100Hz
#define TIM2_PRIORITY
TIMER2 preemption priority set to 2.
void timer2_disable(void)
disable timer2 periph and timer2 interrupt
void pwm_config(void)
configure timer1 periph and its gpios
void timer13_disable(void)
disable timer13 periph and timer2 interrupt
void timer13_config(void)
configure timer13 periph for timing interrupt
void update_pwm_dutycycle(float ch0, float ch1, float ch2)
update timer1 ch0 1 2 duty-cycle
void timer2_config(void)
configure timer2 periph for timing interrupt
this is the header file of timer.c.
#define PWM_PERIOD
PWM period calculate from PWM_FREQUENCY.