唐嫣2017上映的电视剧:STM32的PWM

来源:百度文库 编辑:八卦区 时间:2020/01/19 23:22:07
闲不下~看看STM32的PWM(2011-07-06 21:21:21) 
        STM32的TIMx 是 TIMx_ARR 寄存器确定频率(周期)、由TIMx_CCRx  寄存器确定占空比的信号。
        
        使用定时器3。而TIM2、3、4的时钟源是 APB1 即是 PCLK1  ( APB1 对应 PCLK1 )
 PCLK1 = APB1 = HCLK/2 = SYSCLK/2 = 36MHZ (36,000,000 HZ)
        但是注意: 倍频器会自动倍2, 即是 【72MHZ】!


万历版程序
功能:要求:在万利的开发板 EK-STM32F 上产生 周期为1秒,占空比分别为 50% 10%的 PWM       并且点亮 板上的 LD1,LD2 灯闪烁。

void 
STM32_PWM_GPIO_Configuration(void)
 
        // 11:完全映像
         STM32_Afio_Regs->mapr.bit.TIM3_REMAP=3;
         
        // LD1 =P7 LD2=PC6
        
 STM32_Gpioc_Regs->crl.bit.CNF6=Output_Af_push_pull;   // PC.06 复用功能推挽输出模式
 STM32_Gpioc_Regs->crl.bit.MODE6=Output_Mode_50mhz;    // PC.06 输出模式,最大速度50MHz

 STM32_Gpioc_Regs->crl.bit.CNF7=Output_Af_push_pull;   // PC.07 复用功能推挽输出模式
 STM32_Gpioc_Regs->crl.bit.MODE7=Output_Mode_50mhz;    // PC.07 输出模式,最大速度50MHz
 
}//end sub
 
        
        
void 
STM32_TIM3_Configuration(void)
       
       //  TIM_DeInit( TIM3);//复位TIM3定时器   
   STM32_Rcc_Regs->apb1rstr.all |=  RCC_TIM3RST;
   STM32_Rcc_Regs->apb1rstr.all &= ~RCC_TIM3RST;
        //时钟使能
        STM32_Rcc_Regs->apb1enr.all |=RCC_TIM3EN;  
         
   
   //TIM_TimeBaseStructure.TIM_Period = 9999;
   //TIM_TimeBaseStructure.TIM_Prescaler = 7200;
   //TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
   //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   //TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

 STM32_Tim3_Regs->arr.all=9999;  //   定时周期,PWM频率! 10毫秒*100=1秒 
 STM32_Tim3_Regs->psc.all=720; //  7200分频        72MHZ/72,00  72,000,000/72,00=10,000
 STM32_Tim3_Regs->cr1.bit.CKD=0; //  时钟分频因子
 STM32_Tim3_Regs->cr1.bit.DIR=0; //  0:计数器向上计数        
 
   
   //TIM_ClearFlag(TIM3, TIM_FLAG_Update);
 STM32_Tim3_Regs->sr.bit.UIF=0; //更新中断标记由软件清0 ,例如当上溢或下溢时,软件对CNT重新初始化
       

     
        //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        //TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        //TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
        //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        //TIM_OC1Init(TIM3, &TIM_OCInitStructure);
        
        // timer3 的  通道1 是 PC6 引脚, AFIO完全映射
 STM32_Tim3_Regs->ccer.bit.CC1P=0; // 输入/捕获1输出极性  0:OC1高电平有效   1:OC1低电平有效
 STM32_Tim3_Regs->ccer.bit.CC1E=1; // 输入/捕获1输出使能  1: 开启- OC1信号输出到对应的输出引脚。
        
 STM32_Tim3_Regs->ccer.bit.CC2P=0; // 输入/捕获2输出极性  0:OC1高电平有效   1:OC1低电平有效
 STM32_Tim3_Regs->ccer.bit.CC2E=1; // 输入/捕获2输出使能  1: 开启- OC2信号输出到对应的输出引脚。
        
        STM32_Tim3_Regs->ccmr1.ou_bit.CC1S=0;//00:输出比较1 通道1 被配置为输出
        STM32_Tim3_Regs->ccmr1.ou_bit.CC2S=0;//00:输出比较2 通道2 被配置为输出
        
        STM32_Tim3_Regs->ccmr1.ou_bit.OC1M=6;//0110:PWM模式1
        STM32_Tim3_Regs->ccmr1.ou_bit.OC2M=6;//0110:PWM模式1
        
        
        STM32_Tim3_Regs->ccr1.all=5000;// 占空比 50%
        STM32_Tim3_Regs->ccr2.all=1000;// 10%
        
       // TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
        STM32_Tim3_Regs->ccmr1.ou_bit.OC1PE=1; //OC1PE:输出比较1预装载使能1:开启TIMx_CCR1寄存器的预装载功能       
        STM32_Tim3_Regs->ccmr1.ou_bit.OC2PE=1; //OC1PE:输出比较2预装载使能1:开启TIMx_CCR2寄存器的预装载功能   

        
        //TIM_ARRPreloadConfig(TIM3, ENABLE);
        STM32_Tim3_Regs->cr1.bit.ARPE=1;  // 7 自动重装载预装载允许位 0:TIM1_ARR寄存器没有缓冲
        
        // TIM3 enable counter 
        //TIM_Cmd(TIM3, ENABLE);
   STM32_Tim3_Regs->cr1.bit.CEN=1;// 开启计数器
        
        STM32_Tim3_Regs->egr.bit.UG=1; //触发一次事件中断。
        STM32_Tim3_Regs->sr.bit.UIF=0;
        
   
}//end sub 




九九版:

     这个是STM32的PWM输出模式,STM32的TIM1模块是增强型的定时器模块,天生就是为电机控制而生,可以产生3组6路PWM,同时每组2路PWM为互补,并可以带有死区,可以用来驱动H桥。
  下面的代码,是利用TIM1模块的1、2通道产生一共4路PWM的代码例子,类似代码也可以参考ST的固件库中相应example
//Step1.开启TIM和相应端口时钟
//启动GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
                       RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,\
                       ENABLE);
//启动AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//启动TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

//Step2. GPIO做相应设置,为AF输出
//PA.8/9口设置为TIM1的OC1输出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//PB.13/14口设置为TIM1_CH1N和TIM1_CH2N输出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//Step3. TIM模块初始化
void TIM_Configuration(void)
{
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

    //TIM1基本计数器设置(设置PWM频率)
    //频率=TIM1_CLK/(ARR+1)
    TIM_BaseInitStructure.TIM_Period = 1000-1;
    TIM_BaseInitStructure.TIM_Prescaler = 72-1;
    TIM_BaseInitStructure.TIM_ClockDivision = 0;
    TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
    //启用ARR的影子寄存器(直到产生更新事件才更改设置)
    TIM_ARRPreloadConfig(TIM1, ENABLE);


    //TIM1_OC1模块设置(设置1通道占空比)
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_Pulse = 120;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

    //TIM2_OC2模块设置(设置2通道占空比)
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 680;
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
    //启用CCR2寄存器的影子寄存器(直到产生更新事件才更改设置)
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
    
    //死区设置
    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    TIM_BDTRInitStructure.TIM_DeadTime = 0x90;  //这里调整死区大小0-0xff
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
    TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
    
    //TIM1开启
    TIM_Cmd(TIM1, ENABLE);
    //TIM1_OC通道输出PWM(一定要加)
    TIM_CtrlPWMOutputs(TIM1, ENABLE);

}