STM32学习笔记(5):通用定时器PWM输出
SEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
//设置AHB时钟(HCLK)为系统时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//设置高速AHB时钟(APB2)为HCLK时钟
RCC_PCLK2Config(RCC_HCLK_Div1);
//设置低速AHB时钟(APB1)为HCLK的2分频
RCC_PCLK1Config(RCC_HCLK_Div2);
//设置FLASH代码延时
FLASH_SetLatency(FLASH_Latency_2);
//使能预取指缓存
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//设置PLL时钟,为HSE的9倍频8MHz * 9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//使能PLL
RCC_PLLCmd(ENABLE);
//等待PLL准备就绪
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//设置PLL为系统时钟源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//判断PLL是否是系统时钟
while(RCC_GetSYSCLKSource() != 0x08);
}
//开启TIM3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//开启GPIOB的时钟和复用功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_cfg()
{
GPIO_InitTypeDef GPIO_InitStructure;
//部分映射,将TIM3_CH2映射到PB5
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
//选择引脚5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
//输出频率最大50MHz
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//复用推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void TIMER_cfg()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//重新将Timer设置为缺省值
TIM_DeInit(TIM3);
//采用内部时钟给TIM3提供时钟源
TIM_InternalClockConfig(TIM3);
//预分频系数为0,即不进行预分频,此时TIMER的频率为72MHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
//设置时钟分割
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
//设置计数器模式为向上计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//设置计数溢出大小,每计7200个数就产生一个更新事件,即PWM的输出频率为10kHz
TIM_TimeBaseStructure.TIM_Period = 7200 - 1;
//将配置应用到TIM3中
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
}
void PWM_cfg()
{
TIM_OCInitTypeDef TimOCInitStructure;
//设置缺省值
TIM_OCStructInit(&TimOCInitStructure);
//PWM模式1输出
TimOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//设置占空比,占空比=(CCRx/ARR)*100%或(TIM_Pulse/TIM_Period)*100%
TimOCInitStructure.TIM_Pulse = dutyfactor * 7200 / 100;
//TIM输出比较极性高
TimOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//使能输出状态
TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//TIM3的CH2输出
TIM_OC2Init(TIM3, &TimOCInitStructure);
//设置TIM3的PWM输出为使能
TIM_CtrlPWMOutputs(TIM3,ENABLE);
}
STM32学习笔记通用定时器PWM输 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)