程序代码如下: 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都有输出 |
【STM32F0开发日志/评测/笔记】+互补PWM波的产生
STM32F030 PB14和PB15无法输出PWM求助
【STM32F030探索套件】序列之五 外部中断
【STM32F0开发日志---二】+ucosii.2.92移植在STM32F030
上传个STM32F0+5110+内部温度传感器的菜鸟实例
【STM32F030探索套件使用问题】STM32F030 SPI方式驱动ST7565LCD失败
求一份STM32F051 I2C驱动LCD 12864的例程
STM32F0 M0 向结构体赋值进入HardFault异常
STM32F0 ADC-DMA方式采集2路数据时出现问题
STM32F030C8T6,TIM16定时慢很多问题?
评分
查看全部评分