打印

关于PWM的输入捕获问题

[复制链接]
7364|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mandide|  楼主 | 2011-10-31 21:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
PWM, TI, ic, ni, ST
今天弄了大半天,想用PWM输入比较模式测频率,我是根据库3.5的例子编写的,但是调试的时候发现程序一直没能进入中断,导致频率一直没出,程序如下,还望解答~~感激不尽
void  RCC_CFG(void)
{
//APB2  GPIOA,AFIO,
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|
RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOD,ENABLE);
//APB1 TIM3      
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void GPIO_CFG(void)
{
//输入捕获的测试中所用端口为TIM3-CHANNEL2--PA.7,浮空输入
GPIO_InitTypeDef GPIO_InitStructrue;
   
GPIO_InitStructrue.GPIO_Pin = GPIO_Pin_7;
   GPIO_InitStructrue.GPIO_Mode = GPIO_Mode_IN_FLOATING;
   GPIO_InitStructrue.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOA, &GPIO_InitStructrue);
}
void NVIC_CFG(void)//设置串口,用来发送测得的频率
{  
NVIC_InitTypeDef  NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);            //设置为组2
//SET AND ENABLE THE WWDG INTERRUPT
NVIC_InitStructure.NVIC_IRQChannel  = TIM3_IRQn;           //设置中断通道13
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //抢占优先级为0
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     //响应优先级为0
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      //使能该通道中断
   NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel  = USART1_IRQn;           //设置中断通道13
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //抢占优先级为0
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;     //响应优先级为0
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      //使能该通道中断
   NVIC_Init(&NVIC_InitStructure);   
}
void USART1_CFG(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure. USART_BaudRate   = 9600;           //波特率设置为9600
USART_InitStructure. USART_WordLength = USART_WordLength_8b;        //数据位
USART_InitStructure.USART_StopBits    = USART_StopBits_1;      //停止位
USART_InitStructure.USART_Parity     = USART_Parity_No;      //校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //流量控制
USART_InitStructure.USART_Mode     = USART_Mode_Rx|USART_Mode_Tx;   //串口模式
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
//开串口中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);   //使能中断允许接收              //初始化              
   
}
void TIM_Input_Capture_CFG(void)
{
TIM_ICInitTypeDef  TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//TIM的输入端2.3.4与ICx的对应关系
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);
/* Enable the Master/Slave Mode */
   TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM3,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE);
}

然后是中断的程序
void TIM3_IRQHandler(void)
{
/* Clear TIM3 Capture compare interrupt pending bit */
   TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

   /* Get the Input Capture value */
   IC2Value = TIM_GetCapture2(TIM3);

   if (IC2Value != 0)
   {
     /* Duty cycle computation */
     DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;

     /* Frequency computation */
     Frequency = SystemCoreClock / IC2Value;
   }
   else
   {
     DutyCycle = 0;
     Frequency = 0;
   }
}

然后是主函数
int main(void)
{
SysInit();
RCC_CFG();
NVIC_CFG();
GPIO_CFG();
USART1_CFG();
TIM_Input_Capture_CFG();                                 
while(1) ;
     
}
沙发
mandide|  楼主 | 2011-11-1 21:45 | 只看该作者
还望有高手路过~~

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

11

帖子

1

粉丝