情况是这样,我会给一个电机发一个周期pwm脉冲,然后电机给我回20个周期pwm。现在已经实现发pwm给电机了,电机也收到了,也回复20个pwm给我了(示波器量到有),但是我设置成pwm后不能捕获到
离线
呼叫晕哥大神
离线
这个是设置pwm输出的配置 TIM_SetCompare1(TIM3, 100); 可以正常输出pwm
Timer3_Init(19999,71);
void Timer3_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
/* GPIOB Configuration: TIM3 CH1 (PA6) and TIM3 CH2 (PA7) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIOB Configuration: TIM3 CH3 (PB0) and TIM3 CH4 (PB1) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_Cmd(TIM3, ENABLE);
}
离线
这是设置输入捕获的代码配置
TIM3_Cap_Init(0XFFFF,72-1);
void TIM3_Cap_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<1; //tim3 ʱÖÓʹÄÜ
RCC->APB2ENR|=1<<2; //PORTAʱÖÓʹÄÜ
RCC->APB2ENR|=1<<3; //PORTBʱÖÓʹÄÜ
RCC->APB2ENR|=1<<0; //AFIOʱÖÓʹÄÜ
// AFIO->MAPR|=2<<8; //TIM2????
GPIOA->CRL&=0X00FFFFFF; //PA6,PA7
GPIOA->CRL|=0X88000000; //PA6,PA7
GPIOA->ODR&=0xFFFFFF3F; //PA6 PA7 ÏÂÀ
GPIOB->CRL&=0xFFFFFF00; //PB0,PB1
GPIOB->CRL|=0x00000088; //PB0,PB1
GPIOB->ODR&=0xFFFFFFFC; //PB0,PB1 ÏÂÀ
TIM3->ARR=arr;
TIM3->PSC=psc;
//CH1
TIM3->CCMR1|=1<<0; //CC1S=01 Ñ¡ÔñÊäÈë¶Ë IC1Ó³Éäµ½TI1ÉÏ
TIM3->CCMR1|=1<<4; //IC1F=0001 ÅäÖÃÊäÈëÂ˲¨Æ÷ ÒÔFck_int²ÉÑù£¬2¸öʼþºóÓÐЧ
TIM3->CCMR1|=0<<2; //IC1PS=00 ÅäÖÃÊäÈë·ÖƵ£¬²»·ÖƵ
TIM3->CCER|=0<<1; //CC1P=0 ÉÏÉýÑز¶»ñ
TIM3->CCER|=1<<0; //CC1E=1 ÔÊÐí²¶»ñ¼ÆÊýÆ÷µÄÖµµ½²¶»ñ¼Ä´æÆ÷ÖÐ
//CH2
TIM3->CCMR1|=1<<8; //CC2S=01 ͬÉÏ
TIM3->CCMR1|=1<<12; //IC2F=0001 ..
TIM3->CCMR1|=0<<10; //IC2PS=00 ..
TIM3->CCER|=0<<5; //CC2P=0 ..
TIM3->CCER|=1<<4; //CC2E=1 ..
//CH3
TIM3->CCMR2|=1<<0; //CC3S=01 ..
TIM3->CCMR2|=1<<4; //IC3F=0001 ..
TIM3->CCMR2|=0<<2; //IC3PS=00 ..
TIM3->CCER|=0<<9; //CC3P=0 ..
TIM3->CCER|=1<<8; //CC3E=1 ..
//CH4
TIM3->CCMR2|=1<<8; //CC4S=01 ..
TIM3->CCMR2|=1<<12; //IC4F=0001 ..
TIM3->CCMR2|=0<<10; //IC4PS=00 ..
TIM3->CCER|=0<<13; //CC4P=0 ..
TIM3->CCER|=1<<12; //CC4E=1 ..
//ÖжÏʹÄÜ
TIM3->DIER|=1<<1; //ÔÊÐí²¶»ñ1ÖжÏ
TIM3->DIER|=1<<2; //ÔÊÐí²¶»ñ2ÖжÏ
TIM3->DIER|=1<<3; //ÔÊÐí²¶»ñ3ÖжÏ
TIM3->DIER|=1<<4; //ÔÊÐí²¶»ñ4ÖжÏ
TIM3->DIER|=1<<0; //ÔÊÐí¸üÐÂÖжÏ
TIM3->CR1|=0x01; //ʹÄܶ¨Ê±Æ÷3
}
void main()
{
while(1)
{
if(TIM3CH1_CAPTURE_STA&0X80) //???????????
{
temp1=TIM3CH1_CAPTURE_STA&0X3F;
temp1*=65536; //??????
temp1+=TIM3CH1_CAPTURE_Date2;
temp1-=TIM3CH1_CAPTURE_Date1; //?????????
//if (temp1 != 65536)
printf("TIM3_CH1_HIGH:%d us\r\n",temp1); //?????????
TIM3CH1_CAPTURE_Date1=0;
TIM3CH1_CAPTURE_STA=0; //???????
}
if(TIM3CH2_CAPTURE_STA&0X80) //???????????
{
temp2=TIM3CH2_CAPTURE_STA&0X3F;
temp2*=65536; //??????
temp2+=TIM3CH2_CAPTURE_Date2;
temp2-=TIM3CH2_CAPTURE_Date1; //?????????
//if (temp2 != 65536)
printf("TIM3_CH2_HIGH:%d us\r\n",temp2); //?????????
TIM3CH2_CAPTURE_Date1=0;
TIM3CH2_CAPTURE_STA=0; //???????
}
if(TIM3CH3_CAPTURE_STA&0X80) //???????????
{
temp3=TIM3CH3_CAPTURE_STA&0X3F;
temp3*=65536; //??????
temp3+=TIM3CH3_CAPTURE_Date2;
temp3-=TIM3CH3_CAPTURE_Date1; //?????????
//if (temp3 != 65536)
printf("TIM3_CH3_HIGH:%d us\r\n",temp3); //?????????
TIM3CH3_CAPTURE_Date1=0;
TIM3CH3_CAPTURE_STA=0; //???????
}
if(TIM3CH4_CAPTURE_STA&0X80) //???????????
{
temp4=TIM3CH4_CAPTURE_STA&0X3F;
temp4*=65536; //??????
temp4+=TIM3CH4_CAPTURE_Date2;
temp4-=TIM3CH4_CAPTURE_Date1; //?????????
//if (temp4 != 65536)
printf("TIM3_CH4_HIGH:%d us\r\n",temp4); //?????????
TIM3CH4_CAPTURE_Date1=0;
TIM3CH4_CAPTURE_STA=0; //???????
}
}
}
离线
如果只是开机设置成捕获就没问题。设置成输出再设置成捕获就不行了。这是重点!!!!
离线
看了下,感觉怎么这么优秀的一个用库函数一个用寄存器=。=
离线
如果上电就进入输入捕获就可以的话,那说明输入捕获初始化没问题咯,那么设置成输出再设置成输入是否还可以呢?就单纯自己给脉冲 不用电机的。自己用杜邦线怼几下咯。 有一种情况就是输出脉冲后,MCU进入输入捕获这个切换需要一点点时间。而电机响应太快。
离线
看了下,感觉怎么这么优秀的一个用库函数一个用寄存器=。=
库函数是我自己的。寄存器是网上找的
离线
如果上电就进入输入捕获就可以的话,那说明输入捕获初始化没问题咯,那么设置成输出再设置成输入是否还可以呢?就单纯自己给脉冲 不用电机的。自己用杜邦线怼几下咯。 有一种情况就是输出脉冲后,MCU进入输入捕获这个切换需要一点点时间。而电机响应太快。
我现在不用电机,就怼着一个持续的pwm用来测试捕获。发现上电就设置输入捕获没问题。但是设置成输出再设置输入捕获就不行了。不知道什么原因
离线
问题解决了,设置输出后,执行TIM_DeInit(TIM3),再设置输入捕获就行了
Timer3_Init(19999,71);
TIM_SetCompare1(TIM3, 100);
TIM_SetCompare2(TIM3, 100);
TIM_SetCompare3(TIM3, 100);
TIM_SetCompare4(TIM3, 100);
TIM_DeInit(TIM3);
TIM3_Cap_Init(0XFFFF,72-1);
离线
可能是disable再enable一下就可以。
离线