计算机专业的,自己目前的经历对控制做的比较少,这周熟悉了STM32,至此目前自学了三星的S3C2440A 、NPC的LPC114、ST的STM32103FVET6
根据References Manual 和 datasheet
PWM input model is a kind of input caputre model
This mode is a particular case of input c apture mode. The procedure is the same except:
● Two ICx signals are mapped on the same TIx input.
● These 2 ICx signals are active on edges with opposite polarity.
● One of the two TIxFP signals is selected as trigger input and the slave mode controller
is configured in reset mode
1. 每个定时器有四个输入捕获通道IC1、IC2、IC3、IC4。且IC1 IC2一组,IC3 IC4一组。并且可是设置管脚和寄存器的对应关系。
2. 同一个TIx输入映射了两个ICx信号。
3. 这两个ICx信号分别在相反的极性边沿有效。
4. 两个边沿信号中的一个被选为触发信号,并且从模式控制器被设置成复位模式。
5. 当触发信号来临时,被设置成触发输入信号的捕获寄存器,捕获“一个PWM周期(即连续的两个上升沿或下降沿)。
根据通用定时器的diagram,可以看到 IC1和IC2可以映射到TI1或者TI2上面。我的问题就是在看整个逻辑图的时候,不仔细,加上好多简写不熟悉,不理解,但是花点时间后,就还是看懂了。
坚持就是胜利!!!!!!
在过程中面临的问题:
1、整个初始化相关功能,需要初始化相关的寄存器,而具体应该初始化哪些寄存器和初始化的顺序。
参考手册上面的例子,自己感觉不够仔细,但是安装上面说的,把寄存器按照上面说的顺序,把相关寄存器都初始化。但是还程序中,还是不行,最后还是在网上查了前人的例子,把初始化完成:
void pmw_input_ini(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//TIM_TimeBaseInitTypeDef TIM3_TimeBaseStructure;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //TIM3_CH1 < PC6 FULL REMAP
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //TIM3_CH2 < PC7 FULL REMAP
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
*/
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE);
//
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //NVIC配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM3->PSC = 0; // CK_CNT = CK_PSC/( PSC[15:0] +1 ) 一分频
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI ; /// 0x01; CC1S selected //
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; ///CC1P
TIM_ICInitStructure.TIM_ICFilter = 0x0;//TIM_TS_TI1FP1; ///0x101 TI1FP1 <SMR triggle selection
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; ////*!< Capture performed each time an edge is detected on the capture input. */
TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
//TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Trigger);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); //复位模式为从模式
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); //使能主从模式
TIM_CCxCmd(TIM3,TIM_Channel_1,TIM_CCx_Enable);
TIM_CCxCmd(TIM3,TIM_Channel_2,TIM_CCx_Enable);
TIM_Cmd(TIM3,ENABLE);
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); //打开中断
}
void TIM3_IRQHandler(void)
{
unsigned int IC2Value,DutyCycle,Frequency,cycle;
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); //清楚TIM的中断待处理位
IC2Value = TIM_GetCapture1(TIM3); //读取IC1捕获寄存器的值,即为PWM周期的计数值
if (IC2Value != 0)
{
cycle = TIM_GetCapture2(TIM3) +1 ;
//计算PWM频率。
DutyCycle = (cycle * 100) / IC2Value; //读取IC2捕获寄存器的值,并计算占空比
Frequency = 72000000 / IC2Value;
//Frequency = 24000000 / IC2Value;
}
else
{
DutyCycle = 0;
Frequency = 0;
}
USART_OUT(USART1,"\r\n");
USART_OUT(USART1,"period : %d duty:%d ",IC2Value,cycle);
USART_OUT(USART1,"\r\n");
USART_OUT(USART1,"DytyCycle: %d Frequency:%d ",DutyCycle,Frequency);
}
/////////////////////////////////////////
2.PWM波的单个波形周期为period,在寄存器TIMx_CCR1,该记录了单个波的周期,数据为在timer的CK_CNK=》timer clock 系统时钟,period中为一个周期波形所经过的时钟CK_CNK数目。
而占空比的表现形式,在duty cycle在寄存器TIMx_CCR2,duty cycle中记录一个周期波形下,高电平或者低电平所经过的CK_CNKI数目。
3.overflow 寄存器数据溢出问题。
当输入1K Hz PWM信号时,如果 pwm input model 所在timer,的预分频器为0,即是一分频,则信号72M Hz,则 捕获的period = 72M / 1K = 72K = 72000 CK_CNK
但是,period所在寄存器为16bit,MAX = 65535,会造成数据溢出6465
数据:
period : 6464 duty:6462
DytyCycle: 99 Frequency:11138
period : 6464 duty:6462
DytyCycle: 99 Frequency:11138
period : 6464 duty:6462
DytyCycle: 99 Frequency:11138
period : 6464 duty:6462
DytyCycle: 99 Frequency:11138
数据溢出问题
//////////////////////////////////////////////////////////////////////
4.数据频率,根据采集数据的频率要求,H>=2f,才可以准确采集,应用在此情况下,输入捕获的timer的频率不能小于输入信号