[mw_shl_code=c,true]//定时器2通道2输入捕获配置
//arr:自动重装值(TIM2,TIM5是32位的!!)
//psc:时钟预分频数
TIM_ICInitTypeDef TIM2_ICInitStructure;
void TIM2_CH2_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE); //使能PORTA时钟
TIM_DeInit(TIM2);
//数据总线使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //GPIOB
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输入
GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化PA1
//产生捕获源
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIOA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA0
//时钟脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //GPIOA1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2); //PA1复用位定时器2
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
//初始化TIM2输入捕获参数
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC2S=01 选择输入端 IC1映射到TI1上
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
//产生DMA请求信号
TIM_DMACmd(TIM2, TIM_DMA_CC2, ENABLE);
//打开定时器
TIM_Cmd(TIM2, ENABLE);
}
unsigned short SendBuff[1024];
void MYDMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1时钟使能
DMA_DeInit(DMA1_Stream6);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE){}//等待DMA可配置
/* 配置 DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_3; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&GPIOB->IDR;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)SendBuff;//DMA 存储器0地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//外设模式到存储器
DMA_InitStructure.DMA_BufferSize = 1;//数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据长度:16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//存储器数据长度:16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// 使用循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//中等优先级
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(DMA1_Stream6, &DMA_InitStructure);//初始化DMA Stream
DMA_Cmd(DMA1_Stream6,ENABLE);
}
int main(void)
{
delay_init(168); //初始化延时函数
uart_init(115200);//初始化串口波特率为115200
TIM2_CH2_Cap_Init(256,0); //以xMhz的频率计数
MYDMA_Config();//DMA1,STEAM6,CH3,外设为TIM2,存储器为 SendBuff,长度为:1
while(1)
{
//捕获源
delay_us(100);
GPIO_ResetBits(GPIOA, GPIO_Pin_3);
delay_us(100);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
}
}
[/mw_shl_code]
请问大家这段代码哪还有问题,我用KEIL调试,watch窗口监视SendBuff,没数据搬用进来
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你好,请问问题解决了吗?可以分享一下思路吗
一周热门 更多>