kinto 发表于 2019-7-9 09:06:03

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

程序代码如下:
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_InitTypeDefCOMP_InitStructure;
        GPIO_InitTypeDefGPIO_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都有输出

す疯Ⅱ恒す 发表于 2019-7-9 09:16:48

F030有比较器???

kinto 发表于 2019-7-9 10:38:17

谢谢,我仔细看看
页: [1]
查看完整版本: STM32F030模拟比较器COMP1不能控制PWM1