本帖最后由 ligongxiaobie 于 2014-4-22 11:00 编辑
最近在用
STM32做一个脉冲计数的任务,很简单,就是在一定时间内计算出有多少个脉冲,我是通过
tiM3进行输入捕获,然后把记到的数放到DMA里,然后用DMA产生中断,在中断里对10组数处理。用的是PB1口。
但是程序并不是像我想的一样, 在用信号发生器去给一个固定的脉冲的时候,DMA里的10个数并不一样,而且随着脉冲频率的加快,DMA进入中断的速度也加快了,自己感觉代码没有问题,这里附上代码,希望各位前辈批评指正,晚辈感激不尽!
- #include "stm32f4xx.h"
- #include "hardware_conf.h"
- #define TIM3_CCR4_ADDRESS 0x40000440
- #define TIM3_CNT_ADDRESS 0x40000424
- u16 TIM3_Buffer[10]={0}; //用于缓存计数值
-
-
- void TIM3_initial(void)//计数
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- /* TIM1 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* GPIOB clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- TIM_DeInit(TIM3);//初始化TIM3寄存器
- /* TIM3 channel 3 pin (PE.11) configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- 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_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM3);
- TIM_BaseInitStructure.TIM_Period = 65535; //周期
- TIM_BaseInitStructure.TIM_Prescaler = 10; //预分频
- TIM_BaseInitStructure.TIM_ClockDivision = 1;
- TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure);
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
- TIM_ICInitStructure.TIM_ICFilter = 0x0;
- TIM_ICInit(TIM3, &TIM_ICInitStructure);
- DMA_Cmd(DMA1_Stream2, ENABLE);
- TIM_DMACmd(TIM3, TIM_DMA_CC4, ENABLE);
- /* TIM enable counter */
- TIM_Cmd(TIM3, ENABLE);
- }
- void TIM3_DMA_Config(void) //计数的DMA函数
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);
- DMA_InitStructure.DMA_Channel = DMA_Channel_5;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM3_CCR4_ADDRESS ;
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)TIM3_Buffer; //存放计数数组
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
- DMA_InitStructure.DMA_BufferSize = 10;
- 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_Medium;
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_Init(DMA1_Stream2, &DMA_InitStructure);
- DMA_Cmd(DMA1_Stream2, ENABLE);
- DMA_ITConfig(DMA1_Stream2, DMA_IT_TC, ENABLE);//使能DMA中断,在接收完10个数据之后产生中断
- NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- DMA_Cmd(DMA1_Stream2, ENABLE); //使能DMA1_Steam2
- }
复制代码这里是DMA中断里的处理,我这里有个LCD屏,我是在屏上去观察计数的值。
- #include "stm32f4xx_it.h"
- #include "hardware_conf.h"
- #include "DMA_CESU.H"
- #include "CeSu_INIT.H"
- #include "ili932x_conf.h"
- extern u16 Dispaly_Buffer[10];
- extern u16 TIM3_Buffer[10];
- int c=0;
- #define LED0 PAout(0)
- void DMA1_Stream2_IRQHandler(void) //DMA接受完成中断
- {
- u8 i=0,a=0;
- LED0=~LED0;
- /* Test on DMA Stream Transfer Complete interrupt */
- if(DMA_GetITStatus(DMA1_Stream2, DMA_IT_TCIF2))
- {
- for(i=0;i<10;i++)
- {
- Dispaly_Buffer[i]=TIM3_Buffer[i];//将接受到的10个数缓存出来到另一个数组中
- }
- c++;
- if(c==100) //这里是防止发的数据太快,屏显示不过来
- {
- c=0;
- for(a=0;a<10;a++)
- {
- Display_4NUM(50,0+a*15,Dispaly_Buffer[a],BLUE);
-
- }
- }
-
- DMA_ClearITPendingBit(DMA1_Stream2, DMA_IT_TCIF2);
- }
复制代码
一周热门 更多>