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

STM32F030模拟比较器COMP1不能控制PWM1

[复制链接]
kinto 提问时间:2019-7-9 09:06 /
程序代码如下:
void TIM1Configurate(void)
{
    unsigned int uiChannelDuty;

  TIM_OCInitTypeDef       TIM_OCInitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  GPIO_InitTypeDef        GPIO_InitStructure;
  TIM_BDTRInitTypeDef     TIM_BDTRInitStructure;
  //GPIOA clock enable
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  //TIM1 clock enable
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  TIM_DeInit(TIM1);
  TIM_InternalClockConfig(TIM1);
       
  //GPIOA(PA8,PA9)Configuration: PA8->TIM1_CH1, PA9->TIM1_CH2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  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_PinSource8, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
       
  uiChannelDuty = 100;//(uint16_t) (((uint32_t) MOS_OPEN*600)/100);
                                       
  //Time Base configuration
        TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = PWMPeriod;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
       
  //Channel 1 and 2 Configuration in PWM mode
        TIM_OCStructInit(&TIM_OCInitStructure);
  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 = uiChannelDuty;//Channel1Pulse;
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

        TIM_OCStructInit(&TIM_OCInitStructure);
  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_OCPolarity = TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
  TIM_OCInitStructure.TIM_Pulse = 600 - uiChannelDuty;
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
       
  TIM_SelectOCREFClear(TIM1,TIM_OCReferenceClear_OCREFCLR);
  TIM_ClearOC1Ref(TIM1, TIM_OCClear_Enable);
  TIM_ClearOC2Ref(TIM1, TIM_OCClear_Enable);

  //TIM1 Main Output Enable
  TIM_CtrlPWMOutputs(TIM1, ENABLE);       
  //TIM1 counter enable
  TIM_Cmd(TIM1, ENABLE);
  //TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
}

PWMl输出正常
void ComparerConfigurate(void)
{
  //DAC_InitTypeDef    DAC_InitStructure;
  COMP_InitTypeDef  COMP_InitStructure;
        GPIO_InitTypeDef  GPIO_InitStructure;
  //RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC , ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
       
        GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;// | GPIO_Pin_0;
  //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  //COMP_DeInit();
       
        //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_VREFINT;
  COMP_InitStructure.COMP_Output = COMP_Output_TIM1OCREFCLR;
  COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;
  //COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed;
        COMP_InitStructure.COMP_Mode = COMP_Mode_UltraLowPower;
  COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No;
        //COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_High;
  COMP_Init(COMP_Selection_COMP2, &COMP_InitStructure);
       
        //COMP_GetOutputLevel(COMP_Selection_COMP1);
                                       
  //COMP_SwitchCmd(DISABLE);
  //COMP_WindowCmd(DISABLE);
  COMP_Cmd(COMP_Selection_COMP2,ENABLE);
}

我想用比较器COMP1控制PWM1的通断,当PA1大于1.2V时,PWM1关断,当PA1小于1.2V时,PWM1正常输出,但不管PA1怎么样,pwm1都有输出
收藏 评论2 发布时间:2019-7-9 09:06

举报

2个回答
疯de_恒 回答时间:2019-7-9 09:16:48
F030有比较器???

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

kinto 回答时间:2019-7-9 10:38:17
谢谢,我仔细看看

所属标签

相似问题

官网相关资源

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