你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32输入捕获和PWM输出的问题,请高手指点!

[复制链接]
asdy00000 提问时间:2012-9-3 19:31 /
我用的CPU是STM32F103RBT6,库的版本是3.4.0,我现在的程序能运行,但是我不知道为什么在我的PA1脚没有接输入频率的时候却一直在有数据,而且数据不是固定的,输入捕捉在没有频率输入的时候应该是0吧?!我只上传两组数据举例一下如下所示:
DutyCycle1     64
Frequency1     1651
capture2       43607
capture3       28322
capture4       56304
DutyCycle1     56
Frequency1     1661
capture2       43334
capture3       50192
capture4       24343
还有一个问题就是为什么我用的是定时器输出比较模式产生PWM,却不是我计算的值呢!?TIM_Period =65535,TIM_Prescaler=5,TIM_Pulse  = zhankongbi4=600;却产生的10k频率的方波(示波器看的),而且为什么改变了TIM_Pulse  的值,输出的频率就会改变呢?!正常不是应该改变TIM_Period 和TIM_Prescaler值嘛?!定时器输出比较模式的频率计算到底是怎么回事呢?我的systemclock选择的72M!
求高手指点一下,先贴上我的代码,main.c函数
#include "stm32f10x.h"
#include
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TimOCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
void RCC_cfg();
void GPIO_cfg();
void TIME3_PWM_cfg();
void TIME2_cfg();
void NVIC_cfg();
u16 Frequency1;
u16 DutyCycle1;
vu16 zhankongbi4=600;  //600->10k
int main()
{        RCC_cfg();
         GPIO_cfg();
         NVIC_cfg();
         TIME2_cfg();
         TIME3_PWM_cfg();        
         while(1)
         {    
         }         
}
void GPIO_cfg()
{
  GPIO_InitTypeDef GPIO_InitStructure;        
         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM2,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO,ENABLE);
  //CH2  TIM2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;        
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA,&GPIO_InitStructure);   
  // CH4 TIM3
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;        
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB,&GPIO_InitStructure);
  //LED
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;        
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC,&GPIO_InitStructure);        
}
void TIME3_PWM_cfg()
{
  TIM_TimeBaseStructure.TIM_Prescaler = 0; 
  TIM_TimeBaseStructure.TIM_ClockDivision =0; 
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
  TIM_PrescalerConfig(TIM3,5,TIM_PSCReloadMode_Immediate);
  TIM_OCStructInit(&TimOCInitStructure);
  TimOCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TimOCInitStructure.TIM_Pulse  = zhankongbi4;    
  TIM_OC4Init(TIM3, &TimOCInitStructure);    
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
         TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);          
         TIM_Cmd(TIM3, ENABLE);  
}
void TIME2_cfg()
{
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision =0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period =65535;
  TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);         
  TIM_PrescalerConfig(TIM2,5,TIM_PSCReloadMode_Immediate);
  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_ICInit(TIM2, &TIM_ICInitStructure);      
  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);  
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); 
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
  TIM_Cmd(TIM2, ENABLE);
}
void NVIC_cfg()
{
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
void RCC_cfg()
{  ErrorStatus HSEStartUpStatus;
   RCC_DeInit();
   RCC_HSEConfig(RCC_HSE_ON);  
   HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)      
  {
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
    RCC_PCLK2Config(RCC_HCLK_Div1);  
    RCC_PCLK1Config(RCC_HCLK_Div2); 
    FLASH_SetLatency(FLASH_Latency_2);  
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);     
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);    
    RCC_PLLCmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    while(RCC_GetSYSCLKSource() != 0x08) {}   
  }
}

stm32f10x_it.c中断函数

#include "stm32f10x_it.h"
extern u16 Frequency1;
extern u16 DutyCycle1;
extern vu16 zhankongbi4;
u16 capture2;
u16 capture3;
u16 capture4;
void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
  { TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
    GPIO_WriteBit(GPIOB,GPIO_Pin_1,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_1)));
    capture4=TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture4+zhankongbi4);
  }
}
void TIM2_IRQHandler(void)
{ if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {   TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
      capture2=TIM_GetCapture2(TIM2);
      capture3=TIM_GetCapture1(TIM2);   
    if(capture2 != 0)
      {
          DutyCycle1= (TIM_GetCapture1(TIM2) * 100) / capture2;
          Frequency1 = 72000000 / capture2;
      }
      else { DutyCycle1 = 0; Frequency1 = 0;}
  }
}
收藏 评论0 发布时间:2012-9-3 19:31

举报

0个回答

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版