本帖最后由 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也是对的,但为什么就是不能实现逐周期控制,不知道有没有人用过。 |
【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定时慢很多问题?
1、中文版的资料 跟这个有关的部分翻译的时候阉割掉了,看英文版的就有这个介绍了。
2、解决的方法增加了3句话
TIM_SelectOCREFClear(TIM3,TIM_OCReferenceClear_OCREFCLR);
TIM_ClearOC1Ref(TIM3, TIM_OCClear_Enable);
TIM_ClearOC4Ref(TIM3, TIM_OCClear_Enable);