原理图:
调PB5的亮度 头文件及完整例程下载:[size=13.3333px]http://www.51hei.com/f/stm32标准例程库函数.rar 程序分析:
int main(void)
{
unsigned char a=0;
TIM_OCInitTypeDef TIM3_OCInitStructure;
RCC_Configuration();
time_ini();
SysTick_Config(72000); //配置SYSTICK时钟节拍为1ms一次
while(1){
Delay(1); //延时1ms
TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2
TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出禁止
TIM3_OCInitStructure.TIM_Pulse = CCR2_Val; //确定占空比
TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM3_OCInitStructure);
/*调整CCR2_Val的值来改变占空比,逐步的控制LED1的亮度, 占空比大过一定值时,
亮度的变化就不明显了,所以CCR2_VAL最大设定到17000*/
if(a==0) CCR2_Val=CCR2_Val+10;
else CCR2_Val=CCR2_Val-10;
if(CCR2_Val>17000){ CCR2_Val=17000; a=1;}
else if(CCR2_Val<200){ CCR2_Val=200; a=0;}
}
}
时钟初始化打开RCC_APB2Periph_AFIO ,RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOCD| RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE void RCC_Configuration(void){
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
}
配置初始化,初始化PB5为复用输出,TIM3部分映射到PB5,设置PWM的时钟为72M/(3*2400)=10K,初始化TIM3时钟,
占空比为CCR2_Val /(TIM_Period +1)
void time_ini(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PB5复用为TIM3的通道2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*TIM3局部复用功能开启 在TIM3的局部复用开启时,PB5会被复用为TIM3_CH2*/
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE); //TIM3部分映射到PB5
/*-------------------------------------------------------------------
TIM3CLK=72MHz 预分频系数Prescaler=2 经过分频 定时器时钟为24MHz
根据公式 通道输出占空比=TIM3_CCR2/(TIM_Period+1),可以得到TIM_Pulse的计数值
捕获/比较寄存器2 TIM3_CCR2= CCR2_Val
-------------------------------------------------------------------*/
TIM3_TimeBaseStructure.TIM_Prescaler = 2; //预分频器TIM3_PSC=3 24M
TIM3_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式 TIM3_CR1[4]=0
TIM3_TimeBaseStructure.TIM_Period =2399; //自动重装载寄存器TIM3_APR 确定频率为10KHz
TIM3_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频因子 TIM3_CR1[9:8]=00
TIM3_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM3,&TIM3_TimeBaseStructure); //写TIM3各寄存器参数
TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2 TIM3_CCMR1[14:12]=111 在向上计数时,
//一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输入/捕获2输出允许 OC2信号输出到对应的输出引脚PB5
TIM3_OCInitStructure.TIM_Pulse = CCR2_Val; //确定占空比,这个值决定了有效电平的时间。
TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性 低电平有效 TIM3_CCER[5]=1;
TIM_OC2Init(TIM3, &TIM3_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3,ENABLE); //启动定时器3 TIM3_CR1[0]=1;
}
SysTick_Config(72000); //配置SYSTICK时钟节拍为1ms一次
while(1){
Delay(1); //延时1ms
TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2
TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出禁止
TIM3_OCInitStructure.TIM_Pulse = CCR2_Val; //确定占空比
TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM3_OCInitStructure);
/*调整CCR2_Val的值来改变占空比,逐步的控制LED1的亮度, 占空比大过一定值时,
亮度的变化就不明显了,所以CCR2_VAL最大设定到17000*/
if(a==0) CCR2_Val=CCR2_Val+10;
else CCR2_Val=CCR2_Val-10;
if(CCR2_Val>17000){ CCR2_Val=17000; a=1;}
else if(CCR2_Val<200){ CCR2_Val=200; a=0;}
}
|