采用stm32f407 使用TIM8通道3初始化成输入捕获功能,捕获PB15高电平和低电平的脉宽。是使用两种方式实现:中断和DMA,有波形,但是都采集不到数据,不知道是那的问题。 1.DMA方式 void TIM8_Config(void) { TIM_ICInitTypeDef TIM_ICInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_Cmd(TIM8, DISABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); /* GPIO clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM8); DMA_DeInit(DMA2_Stream2); while (DMA_GetCmdStatus(DMA2_Stream2) != DISABLE) { } /* Configure DMA Stream */ DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&TIM8->CCR3);; DMA_InitStructure.DMA_Memory0BaseAddr = (u32)cap_buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1024;//1 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream2, &DMA_InitStructure); DMA_Cmd(DMA2_Stream2, ENABLE); TIM_DeInit(TIM8); TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;//TIM_ICPolarity_Falling;//TIM_ICPolarity_BothEdge;////TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM8, &TIM_ICInitStructure); // // /* Select the TIM3 Input Trigger: TI2FP2 */ // TIM_SelectInputTrigger(TIM8, TIM_TS_TI2FP2);//TIM_TS_ETRF); // // /* Select the slave Mode: Reset Mode */ // TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Reset); // // /* Enable the Master/Slave Mode */ // TIM_SelectMasterSlaveMode(TIM8, TIM_MasterSlaveMode_Enable); TIM_DMAConfig(TIM8,TIM_DMABase_CCR3,TIM_DMABurstLength_2Bytes); /* TIM1 CC2 DMA Request enable */ TIM_DMACmd(TIM8,TIM_DMA_CC3, ENABLE); /* TIM1 CC2 DMA Request enable */ TIM_DMACmd(TIM8,TIM_DMA_Update, ENABLE); /* TIM enable counter */ TIM_Cmd(TIM8, ENABLE); } 2.中断方式 void TIM8_Config(void) { TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_DeInit(TIM8); /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); /* GPIO clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM8); TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;//TIM_ICPolarity_BothEdge;//TIM_ICPolarity_Falling;//TIM_ICPolarity_BothEdge;////TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM8, &TIM_ICInitStructure); // TIM_ITConfig(TIM8, TIM_IT_Update | TIM_IT_CC3, ENABLE); TIM_ITConfig(TIM8, TIM_IT_CC3, ENABLE); TIM_Cmd(TIM8, ENABLE); // TIM_SelectInputTrigger(TIM8, TIM_TS_TI1F_ED ); NVIC_InitStructure.NVIC_IRQChannel = TIM8_BRK_TIM12_IRQn;//TIM8_BRK_TIM12_IRQn; TIM8_CC_IRQn NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } unsigned int fff=0; void TIM8_BRK_TIM12_IRQHandler(void) { // if(TIM_GetITStatus(TIM8,TIM_IT_Update)==SET) // { // // SetTextValueInt32(2,3,sssssss++); // TIM_ClearITPendingBit(TIM8,TIM_IT_Update); // } if(TIM_GetITStatus(TIM8,TIM_IT_CC3)==SET) { // SetTextValueInt32(2,1,sss++); cap_buffer[fff]=TIM_GetCapture3(TIM8);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ. fff++; TIM_ClearITPendingBit(TIM8,TIM_IT_CC3); } } 两种方式都采集不到数据。 |
STM32F407 定时器触发DMA 求助大神
【MCU实战经验】基于STM32F407的音频播放器设计
盘古UE-STM32F407工控板原理图
【STM32F429心得\疑问】+STM32F4之FSMC和FMC
STM32F429读取IO口传输的数据速率
STM32F407ZGT6 手摸芯片背部重启
STM32F4 SPI 动作时,软件片选信号被拉高,IO口程序逻辑失控
STM32F401RE NUCLEO求助,串口一直不能进中断
读取STM32F407内部温度传感器值错误
STM32F429多路内部ADC独立采集的办法
void TIM8_CC_IRQHandler(void)
{
if(TIM_GetITStatus(TIM8,TIM_IT_CC3)==SET)
{
cap_buffer[fff]=TIM_GetCapture3(TIM8);//»ñÈ¡µ±Ç°µÄ²¶»ñÖµ.
// = TIM8->CCR3;
fff++;
}
TIM_ClearITPendingBit(TIM8,TIM_IT_CC3);
}
评分
查看全部评分
然后全速运行采集过程中暂停看看 CCR3寄存器有没有值。
一般配置没问题就不会出问题。
评分
查看全部评分