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

STM32F051比较器周期电流控制的问题

[复制链接]
njliweizhi-3202 提问时间:2016-4-29 14:45 /
本帖最后由 njliweizhi-320212 于 2016-4-29 18:29 编辑

1、TIM3配置

void TIM_Configuration( void )
{  
    uint16_t TimerPeriod = 0;
    uint16_t Channel1Pulse = 0, Channel2Pulse = 0;
   
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;     //245_EN
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* GPIOA (PA6,PA7)Configuration: PA6->TIM3_CH1, PA7->TIM3_CH2 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
        
    /* GPIOB (PB0,PB1)Configuration: PB0->TIM3_CH4, PB1->TIM3_CH3 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
   
        
  TimerPeriod = (SystemCoreClock / PWM_FREQ ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
  Channel1Pulse = (uint16_t) (((uint32_t) MOS_OPEN * (TimerPeriod - 1)) / 100);
  /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 */
  Channel2Pulse = (uint16_t) (((uint32_t)  LOW_TENSION * (TimerPeriod - 1)) / 100);
  
   
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2 ;
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


  /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; //Channel1Pulse;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //Channel1Pulse;
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);
   
  /* TIM1 counter enable */
  TIM_Cmd(TIM3, ENABLE);
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);   

  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM3, ENABLE);
   
   
    PWM_ENABLE();
}
PWM能正常工作。
2、比较器配置
void compdaconfig(void)
{
    DAC_InitTypeDef    DAC_InitStructure;
    COMP_InitTypeDef        COMP_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC , ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;   //DA輸出
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
      /* DAC channel1 Configuration */
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;

  /* DAC Channel1 Init */
  DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  
  /* Enable DAC Channel1 */
  DAC_Cmd(DAC_Channel_1, ENABLE);
   
      /* COMP1 Init: the higher threshold is set to VREFINT ~ 1.22V
     but can be changed to other available possibilities */
  COMP_StructInit(&COMP_InitStructure);
  COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_DAC1;
  COMP_InitStructure.COMP_Output = COMP_Output_TIM3OCREFCLR;
    COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;
  COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed;
  COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No;
  COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure);
    COMP_SwitchCmd(DISABLE);
    COMP_WindowCmd(DISABLE);
    COMP_Cmd(COMP_Selection_COMP1,ENABLE);

}
DA管脚输出正常  用COMP_GetOutputLevel(COMP_Selection_COMP1)检测比较器反转正常,但他对TIM3的PWM没有任何作用,监控COMP->CSR =0X4741   证明设定的输出连接  COMP_Output_TIM3OCREFCLR=111也是对的,但为什么就是不能实现逐周期控制,不知道有没有人用过。

收藏 评论3 发布时间:2016-4-29 14:45

举报

3个回答
njliweizhi-3202 回答时间:2016-4-29 18:31:43
比较器其他应用的例程都有,就没有这个的例程,我搜网上有一个人也用不起来,后来比较器输出管脚再到定时器ETR,到底这个功能不知道有没有用
njliweizhi-3202 回答时间:2016-5-6 17:50:16
感谢STM32的邵工,问题解决了。失败的原因:
1、中文版的资料 跟这个有关的部分翻译的时候阉割掉了,看英文版的就有这个介绍了。
2、解决的方法增加了3句话
     TIM_SelectOCREFClear(TIM3,TIM_OCReferenceClear_OCREFCLR);
     TIM_ClearOC1Ref(TIM3, TIM_OCClear_Enable);       
    TIM_ClearOC4Ref(TIM3, TIM_OCClear_Enable);       
shaonian 回答时间:2018-2-25 17:01:27
楼主还在么,能解释下原理吗

所属标签

相似问题

官网相关资源

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