一、以TIMER2和TIMER3的输入捕获为例
Timer2.attachCompare1Interrupt(handler_channel_1);
Timer2.attachCompare2Interrupt(handler_channel_2);
Timer2.attachCompare3Interrupt(handler_channel_3);
Timer2.attachCompare4Interrupt(handler_channel_4);
TIMER2_BASE->CR1 = TIMER_CR1_CEN;
TIMER2_BASE->CR2 = 0;
TIMER2_BASE->SMCR = 0;
TIMER2_BASE->DIER = TIMER_DIER_CC1IE | TIMER_DIER_CC2IE | TIMER_DIER_CC3IE | TIMER_DIER_CC4IE;
TIMER2_BASE->EGR = 0;
TIMER2_BASE->CCMR1 = 0b100000001; //Register is set like this due to a bug in the define table (30-09-2017)
TIMER2_BASE->CCMR2 = 0b100000001; //Register is set like this due to a bug in the define table (30-09-2017)
TIMER2_BASE->CCER = TIMER_CCER_CC1E | TIMER_CCER_CC2E | TIMER_CCER_CC3E | TIMER_CCER_CC4E;
TIMER2_BASE->PSC = 71;
TIMER2_BASE->ARR = 0xFFFF;
TIMER2_BASE->DCR = 0;
Timer3.attachCompare1Interrupt(handler_channel_5);
Timer3.attachCompare2Interrupt(handler_channel_6);
TIMER3_BASE->CR1 = TIMER_CR1_CEN;
TIMER3_BASE->CR2 = 0;
TIMER3_BASE->SMCR = 0;
TIMER3_BASE->DIER = TIMER_DIER_CC1IE | TIMER_DIER_CC2IE;
TIMER3_BASE->EGR = 0;
TIMER3_BASE->CCMR1 = 0b100000001; //Register is set like this due to a bug in the define table (30-09-2017)
TIMER3_BASE->CCMR2 = 0;
TIMER3_BASE->CCER = TIMER_CCER_CC1E | TIMER_CCER_CC2E;
TIMER3_BASE->PSC = 71;
TIMER3_BASE->ARR = 0xFFFF;
TIMER3_BASE->DCR = 0;
以上内容摘自Brokking.net的YMFC-32,链接Brokking.net - My personal project page.
STM32 for Arduino - Connecting an RC receiver via input capture mode: https://youtu.be/JFSFbSg0l2M
STM32 for Arduino - Electronic Speed Controller (ESC) - STM32F103C8T6: https://youtu.be/Nju9rvZOjVQ
无法科学上网可以搜优酷或B站,有老邵翻译的视频
二、以TIMER4的PWM输出频率调整为例
#define Prescale 2
#define Overflow 7200
int highduty = 0;//PWM输出占空比,本例中最大值为7200
void setup() {
Serial.begin(57600);
Timer4.setPrescaleFactor(Prescale);
Timer4.setOverflow(Overflow);
pinMode(PB6, PWM);
pinMode(PB7, PWM);
}
void loop() {
pwmWrite(PB7, highduty);
pwmWrite(PB6, highduty);
}