本帖最后由 wang12zhe-291294 于 2015-4-24 09:35 编辑 我的程序如下, 个人思路是TIM1的CH3输出PWM波形, 然后使用TIM3的CH2捕获PWM波形 系统时钟96MHz //TIM1 PWM部分初始化 //PWM输出初始化 //arr:自动重装值 //psc:时钟预分频数 void TIM1_PWM_Init(u32 arr,u32 psc) { //此部分需手动修改IO口设置 GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM1时钟使能 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟 GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_TIM1); //GPIOF11复用为定时器1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //GPIOA11 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_UP; //上拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA11 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(TIM1,&TIM_TimeBaseStructure);//初始化定时器14 //初始化TIM1 Channel4 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低 // TIM_OCInitStructure.TIM_Pulse =1000; //根据使用的函数 选择不同的通道 TIM_OC4Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1OC1 TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器 TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPE使能 TIM_Cmd(TIM1, ENABLE); //使能TIM1 TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能TIM1的PWM输出,TIM1与TIM8有效 } //定时器3通道2输入捕获配置 //arr:自动重装值(TIM2,TIM5是32位的!!) (TIM3 和 TIM4 16位 //psc:时钟预分频数 void TIM3_CH2_Cap_Init() { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* GPIOB clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* TIM3 chennel2 configuration : PC.07 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 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_UP ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect TIM pin to AF2 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3); /* Enable the TIM3 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); /* Select the TIM3 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); } /** * @brief This function handles TIM4 global interrupt request. * @param None * @retval None */ void TIM3_IRQHandler(void) { RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获1发生捕获事件 { /* Clear TIM3 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); /* Get the Input Capture value */ PWMCCP.Period= TIM_GetCapture2(TIM3); //周期 if (PWMCCP.Period != 0) { PWMCCP.PulseWidth =TIM_GetCapture1(TIM3); //脉冲宽度 /* Duty cycle computation */ PWMCCP.DutyCycle = (TIM_GetCapture1(TIM3) * 100) / PWMCCP.Period; //占空比 /* Frequency computation TIM3 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */ PWMCCP.Frequency = (RCC_Clocks.HCLK_Frequency)/2 / PWMCCP.Period; } else { PWMCCP.DutyCycle = 0; PWMCCP.Frequency = 0; } } } 在主函数里我的PWM的占空是50%,使用示波器看了,PWM波形没问题 但是捕获得到的数据不正常 占空比和周期都不正确 |
最近又没有stm32开发板申请了,什么时候安排?
STM32f407+TOUCHGFX,用CubeMX生成后编译不通过
STM32 Bootloader提问
目前新项目,MCU采用STM32F407VGT6,文件存在TF卡(SDIO),请问能否通过F4的USB把TF卡里面的txt和excel文件拷贝到U盘?哪个USB可以实现?
STM32F407ZGT6假死现象,看门狗不起作用
STM32F407VGT6使用PLL倍频后芯片会反复重启
CAN接收丢包
使用Cubeprogrammer无法连接、连接G431和一块H750的板子可以连上但是连另外一块H750的板子和F407的板子就直接报错,这个应该如何解决。
SPI 通信,既有单字节数据传输,又有多字节数据传输,该如正确使用 DMA?
SD卡log存储
我使用官方的库例程,使用一个IO口模拟PWM输出, 捕获的出的占空比和周期也不对 而且会不停变化