我正在尝试在STM32中初始化计时器中断。
我正在使用以下计时器:
TIM2创建一个Second / 100ms / 10ms时间周期。
TIM3发出没有delay()
的声音信号(蜂鸣器)
TIM4为我的电动机(多轴直升机电动机)创建PWM信号
TIM10计数1us
想法是:
TIM4的PWM信号驱动电机>
电动螺旋桨中断光闸>
STM32的中断计数器将增加>
TIM10计数微秒>
TIM2计数(例如)1秒并每1秒获取一次timer-interrupt-IRQ>
1秒后:计数的微秒将除以中断计数器=螺旋桨旋转之间的平均时间>
平均时间将通过串行接口(UART)发送到PC并保存。
问题:然后我激活了TIM2 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
的计时器IRQ处理程序
我丢失了TIM4的PWM信号。为什么会这样呢? 是否正确初始化计时器中断? 我不需要投票。有更正吗?
// *************************************************************************
// Initialisation for motor PWM and timer 4
void Motor_PWM_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_12 | GPIO_Pin_13
| GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
TIM_TimeBaseInitTypeDef Timer_InitStructure;
TIM_OCInitTypeDef Output_ChannelInit;
uint16_t PrescalarValue = (uint16_t) 84 - 1;
Timer_InitStructure.TIM_Period = 20000 - 1;
Timer_InitStructure.TIM_Prescaler = PrescalarValue;
Timer_InitStructure.TIM_ClockDivision = 0;
Timer_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &Timer_InitStructure);
Output_ChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
Output_ChannelInit.TIM_OutputState = TIM_OutputState_Enable;
Output_ChannelInit.TIM_Pulse = 0;
Output_ChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &Output_ChannelInit);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4, &Output_ChannelInit);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3Init(TIM4, &Output_ChannelInit);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &Output_ChannelInit);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
// *************************************************************************
// Initialisation for buzzer PWM and timer 3
void Buzzer_PWM_Init(void) {
GPIO_InitTypeDef gpioStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
gpioStructure.GPIO_Pin = GPIO_Pin_6;
gpioStructure.GPIO_Mode = GPIO_Mode_AF;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
gpioStructure.GPIO_PuPd = GPIO_PuPd_UP;
gpioStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &gpioStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
TIM_TimeBaseInitTypeDef timerInitStructure;
TIM_OCInitTypeDef outputChannelInit;
uint16_t PrescalarValue = (uint16_t) 84-1;
timerInitStructure.TIM_Period = 1000 - 1;
timerInitStructure.TIM_Prescaler = PrescalarValue;
timerInitStructure.TIM_ClockDivision = 0;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &timerInitStructure);
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
outputChannelInit.TIM_Pulse = 0;
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &outputChannelInit);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
// *************************************************************************
// Initialisation for timer 10 (1 usecond timer)
void MicroSek_timer_Init() {
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM10); // PB8 -> TIM10 Channel 1
// TIM10 Clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
// Timer 10 Config PWM
TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF; //Change Orig 1 Sek: 10000-1; 1ms: 10-1; 10ms: 100-1; 100ms: 1000-1
TIM_TimeBaseInitStructure.TIM_Prescaler = 84-1; //Change Orig 1 Sek: 16800-1
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM10, &TIM_TimeBaseInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM10, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM10, ENABLE);
TIM_Cmd(TIM10, ENABLE);
TIM_CtrlPWMOutputs(TIM10, ENABLE);
TIM10->CCR1 = 1<<15;
}
// *************************************************************************
// Initialisation for timer 2 (1 second timer)
void FS_timer_Init() {
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2); // PC6 -> TIM8 Channel 1
// TIM2 Clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Timer 2 Config PWM
TIM_TimeBaseInitStructure.TIM_Period = period - 1; //Change Orig 1 Sek: 10000-1; 1ms: 10-1; 10ms: 100-1; 100ms: 1000-1
TIM_TimeBaseInitStructure.TIM_Prescaler = 8400-1; //Change Orig 1 Sek: 16800-1
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
// TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM2->CCR1 = period/2;
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
// *************************************************************************
// FS-Timer interrupt
void TIM2_IRQHandler(){
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
fs_cnt++;
ov_flag = TRUE;
fs_flag = TRUE;
uint32_t time = TIM10->CNT;
uint32_t t_mean_0 = time/interruptcounter0;
uint32_t t_mean_1 = time/interruptcounter1;
uint32_t t_mean_2 = time/interruptcounter2;
uint32_t t_mean_3 = time/interruptcounter3;
char uart_t_mean_0[6];
char uart_t_mean_1[6];
char uart_t_mean_2[6];
char uart_t_mean_3[6];
itoa(t_mean_0, uart_t_mean_0, 6);
itoa(t_mean_1, uart_t_mean_1, 6);
itoa(t_mean_2, uart_t_mean_2, 6);
itoa(t_mean_3, uart_t_mean_3, 6);
USART2_Send(USART2, uart_t_mean_0);
USART2_Send(USART2, "\n");
USART2_Send(USART2, uart_t_mean_1);
USART2_Send(USART2, "\n");
USART2_Send(USART2, uart_t_mean_2);
USART2_Send(USART2, "\n");
USART2_Send(USART2, uart_t_mean_3);
USART2_Send(USART2, "\n");
TIM10->CNT = 0;
interruptcounter0=0;
interruptcounter0=1;
interruptcounter0=2;
interruptcounter0=3;
}
else{
fs_flag = FALSE;
}
}