诸位好!
我们公司使用AT32F403A开发一款产品,参考https://bbs.21ic.com/icview-3036522-1-4.html
使用TMR DMA模式捕获外部 PWM信号,上升沿有效,捕获的信号的频率和脉宽是没有问题的,但是有个奇怪的问题!!!一个有效的信号,DMA搬运CCR2的参数值,会变动到下个CCR2的地址里面,也就是两个数
组CCR1[10]和CCR2[11]是一个完整信号的周期和脉宽记录。而不是正确的CCR1[10]和CCR2[10]记录一个信号。重新多次复位定时器后又能正常。
/**
* @brief Main program
* @param None
* @retval None
*/
void pwm_input_dma_init(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
/* DMA1 Channel5 configuration ----------------------------------------------*/
DMA_Reset(DMA1_Channel5);
DMA_DefaultInitParaConfig(&DMA_InitStructure1);
DMA_InitStructure1.DMA_PeripheralBaseAddr = (uint32_t)TMR2_CCR1_Address;//ַ
DMA_InitStructure1.DMA_MemoryBaseAddr = (uint32_t)buffer_cc1; //ڴַ
DMA_InitStructure1.DMA_Direction = DMA_DIR_PERIPHERALSRC; //Դ
DMA_InitStructure1.DMA_BufferSize = DMA_Buffer_Count;
DMA_InitStructure1.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE; //ַ
DMA_InitStructure1.DMA_MemoryInc = DMA_MEMORYINC_ENABLE; //ڴַ
DMA_InitStructure1.DMA_PeripheralDataWidth = DMA_PERIPHERALDATAWIDTH_WORD; //Ϊ32bit
DMA_InitStructure1.DMA_MemoryDataWidth = DMA_MEMORYDATAWIDTH_WORD; //ڴΪ32bit
DMA_InitStructure1.DMA_Mode = DMA_MODE_CIRCULAR; //ѭģʽ
DMA_InitStructure1.DMA_Priority = DMA_PRIORITY_HIGH; //DMA ȼΪ
DMA_InitStructure1.DMA_MTOM = DMA_MEMTOMEM_DISABLE; //ڴ浽ڴ
DMA_Init(DMA1_Channel5, &DMA_InitStructure1);
/* DMA1 Channel7 configuration ----------------------------------------------*/
DMA_Reset(DMA1_Channel7);
DMA_DefaultInitParaConfig(&DMA_InitStructure2);
DMA_InitStructure2.DMA_PeripheralBaseAddr = (uint32_t)TMR2_CCR2_Address;//ַ
DMA_InitStructure2.DMA_MemoryBaseAddr = (uint32_t)buffer_cc2; //ڴַ
DMA_InitStructure2.DMA_Direction = DMA_DIR_PERIPHERALSRC; //Դ
DMA_InitStructure2.DMA_BufferSize = DMA_Buffer_Count;
DMA_InitStructure2.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE; //ַ
DMA_InitStructure2.DMA_MemoryInc = DMA_MEMORYINC_ENABLE; //ڴַ
DMA_InitStructure2.DMA_PeripheralDataWidth = DMA_PERIPHERALDATAWIDTH_WORD; //Ϊ32bit
DMA_InitStructure2.DMA_MemoryDataWidth = DMA_MEMORYDATAWIDTH_WORD; //ڴΪ32bit
DMA_InitStructure2.DMA_Mode = DMA_MODE_CIRCULAR; //ѭģʽ//ģʽ
DMA_InitStructure2.DMA_Priority = DMA_PRIORITY_HIGH; //DMA ȼΪ
DMA_InitStructure2.DMA_MTOM = DMA_MEMTOMEM_DISABLE; //ڴ浽ڴ
DMA_Init(DMA1_Channel7, &DMA_InitStructure2);
DMA_ClearITPendingBit(DMA1_INT_HT5|DMA1_INT_TC5|DMA1_INT_GL5|DMA1_INT_ERR5); //жϱ־
DMA_INTConfig(DMA1_Channel5,DMA_INT_HT|DMA_INT_TC,ENABLE); //ʹܴһ롢ж
/* Enable the DMA1_Channel5 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ȼ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // ͬ ȼΪ1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹж DMA1 Channel5
NVIC_Init(&NVIC_InitStructure);
DMA_ChannelEnable(DMA1_Channel5,ENABLE); //ʹDMA1 Channel5
DMA_ChannelEnable(DMA1_Channel7,ENABLE);
/* TMRe base configuration */
TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
TMR_TMReBaseStructure.TMR_Period = 0xFFFFFFFF;
TMR_TMReBaseStructure.TMR_DIV = 24 - 1; //+1ʱΪ100ns
TMR_TMReBaseStructure.TMR_ClockDivision = 0;
TMR_TMReBaseStructure.TMR_CounterMode = TMR_CounterDIR_Up|TMR_CTRL1_PMEN; //ʹ32BIT
TMR_TimeBaseInit(TMR2, &TMR_TMReBaseStructure);
TMR_ICStructInit(&TMR_ICInitStructure);
TMR_ICInitStructure.TMR_Channel = TMR_Channel_1;
TMR_ICInitStructure.TMR_ICPolarity = TMR_ICPolarity_Rising; //ش
TMR_ICInitStructure.TMR_ICSelection = TMR_ICSelection_DirectTI; //
TMR_ICInitStructure.TMR_ICDIV = TMR_ICDIV_DIV1; //ÿڲϼʱִв
TMR_ICInitStructure.TMR_ICFilter = 0x0F; //ָ벶 Ϊ0
TMR_PWMIConfig(TMR2, &TMR_ICInitStructure);
/* Select the TMR2 Input Trigger: TI1FP1 */ /*ѡ TMR2 TMR_TRGSEL_TI1FP1 ӵPA0.0*/
TMR_SelectInputTrigger(TMR2, TMR_TRGSEL_TI1FP1);
/* Select the slave Mode: Reset Mode */ //ѡӻģʽλģʽ
TMR_SelectSlaveMode(TMR2, TMR_SlaveMode_Reset);
/* Enable the Master/Slave Mode */ ///ģʽ
TMR_SelectMasterSlaveMode(TMR2, TMR_MasterSlaveMode_Enable);
/* Enable the CC1/CC2 DMA */
TMR_DMACmd(TMR2,TMR_DMA_CC1,ENABLE);
TMR_DMACmd(TMR2,TMR_DMA_CC2,ENABLE);
/* TMR enable counter */
TMR_Cmd(TMR2, ENABLE);
}
|