This week, I made a computer mouse's encoder module and spent a whole week.
This process makes us laugh and cry. We plan to use the CH3 and methane channels of tim3 to collect the PWM of the Left motor for speed measurement and ranging, and use the CH2 and cosine of tim4 to test the right motor.
WriteCodeFirst, we did the left motor first. It went to Friday, and we found that the CH3 and 4th channels of the general timer could not be used for PWM input. Dizzy !! Below is one of our configurations. Let's share it with you!
We use the two channels of gpio_ B _7 and timer4.
Void my_config (void)
{
Nvic_inittypedef nvic_initstructure;
Gpio_inittypedef gpio_initstructure;
Tim_timebaseinittypedef tim_timebasestructure;
/* Tim4 clock source enable */
Rcc_apb1periphclockcmd (rcc_apb1periph_tim4, enable );
/* Enable gpioa, clock */
Rcc_apb2periphclockcmd (rcc_apb2periph_gpiob, enable );
Gpio_initstructure.gpio_pin = gpio_pin_7;
Gpio_initstructure.gpio_mode = gpio_mode_in_floating;
Gpio_initstructure.gpio_speed = gpio_speed_2mhz;
Gpio_init (gpiob, & gpio_initstructure );
// Tim4 output Initialization
Tim_deinit (tim4 );
Tim_timebasestructure.tim_period = 100; // COUNT = 100-1
Tim_timebasestructure.tim_prescaler = 0; // pre-Division
Tim_timebasestructure.tim_clockdivision = tim_ckd_div1; // Time Division
Tim_timebasestructure.tim_countermode = tim_countermode_down; // Mode
Tim_timebaseinit (tim4, & tim_timebasestructure); // basic Initialization
Tim_setcounter (tim4, 100); // sets the value of the tim4 counter register.
// Set the value of timx auto-Reload register
Tim_arrpreloadconfig (tim4, enable );
Tim_selectinputtrigger (tim4, tim_ts_ti2fp2); // If channel 1 is used, tim_ts_ti1fp1
// Tim_selectinputtrigger (tim2, tim_ts_ti1f_ed );
Tim_selectslavemode (tim4, tim_slavemode_external1 );
// Tim_selectslavemode (tim4, tim_slavemode_reset );
Tim_setcompare2 (tim4, 100); // if one channel is used. Tim_setcomper1 (tim4, 100 );
Tim_cmd (tim4, enable); // start tim4
Tim_clearflag (tim4, tim_it_cc2); // If a channel is used, tim_it_cc2 and tim_it_cc2 are both changed to tim_it_cc _
Tim_itconfig (tim4, tim_it_cc2, enable)
// Interrupt Configuration
Nvic_initstructure.nvic_irqchannel = tim4_irqchannel ;;//
Nvic_initstructure.nvic_irqchannelpreemptionpriority = 3; // the preemption priority is 0.
Nvic_initstructure.nvic_irqchannelsubpriority = 0; // The response priority is 0.
Nvic_initstructure.nvic_irqchannelcmd = Enable; // interrupt allowed
Nvic_init (& nvic_initstructure); // write settings
}
Void tim4_irqhandler (void)
{
Tim_clearflag (tim4, tim_it_cc2 );
}
Sadly, I have to re-draw the canvas. However, we have learned a lot about the universal timer of stm32. We hope our team will laugh at the end !!!