楼上说得很对,我手上有一个市场上成熟产品的板子,我想把它的程序充掉,我重新写程序功能与它一样,下下去,它的板子没有外加与非门载波。
这几天我研究了很多STM32定时器的示例程序,感觉这个基础上改可行
void RCC_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); } vu16 CCR1_Val=8000; vu16 CCR2_Val=4000; vu16 CCR3_Val=2000; vu16 CCR4_Val=1000; void TIM_Configuration(void) { //结构体 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //写成员 TIM_TimeBaseStructure.TIM_Period=65535; //ARR值 TIM_TimeBaseStructure.TIM_Prescaler=0; //预分频先设0 TIM_TimeBaseStructure.TIM_ClockDivision=0; //捕获专用参数 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向 上TIM_CounterMode //写完初始化 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//设置Base TIM_PrescalerConfig(TIM2,7199,TIM_PSCReloadMode_Immediate);//预分频再设为7199且立即装入 //72M/7200=0.01M=10khz -> 0.1ms //比较捕获寄存器写成员 //TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Timing; ////TIM输出比较时间模式 TIM_OCInitStructure.TIM_OCMode= TIM_OCMode_Toggle; //TIM输出比较触发模式 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //out使能 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //TIM输出比较极性高 TIM_OCInitStructure.TIM_Pulse=CCR1_Val; //待装入捕获比较寄存器的脉冲值。 TIM_OC1Init(TIM2,&TIM_OCInitStructure);//OC1 0.1ms*8000=0.8s TIM_OCInitStructure.TIM_Pulse=CCR2_Val; //待装入捕获比较寄存器的脉冲值。 TIM_OC2Init(TIM2,&TIM_OCInitStructure);//OC2 0.1ms*4000=0.4s TIM_OCInitStructure.TIM_Pulse=CCR3_Val; //待装入捕获比较寄存器的脉冲值。 TIM_OC3Init(TIM2,&TIM_OCInitStructure);//OC3 0.1ms*2000=0.2s TIM_OCInitStructure.TIM_Pulse=CCR4_Val; //待装入捕获比较寄存器的脉冲值。 TIM_OC4Init(TIM2,&TIM_OCInitStructure);//OC4 0.1ms*1000=0.1s //禁止预装载寄存器 TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Disable);//TIM_OCPreload_Disable TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Disable);//TIM_OCPreload_Disable TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Disable);//TIM_OCPreload_Disable TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Disable);//TIM_OCPreload_Disable //使能中断 TIM_ITConfig(TIM2,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE); TIM_Cmd(TIM2,ENABLE); }
这是仿真结果, 最下面可以档当于载波,倒数第二相当于移相,中断中处理PWM输出
|