stm32F407(CORTEX-M4)之RCC配置與TIM2的PWM
(我的外部時(shí)鐘為25MHz)
本文引用地址:http://m.butianyuan.cn/article/201611/321571.htm#include
static __IO uint32_t TimingDelay;
int8_t LED_Flag = 1;
void RCC_Config(void);
void GPIO_Config(void);
void TIM_Config(void);
void NVIC_Config(void);
void Delay(__IO uint32_t nTime);
main ()
{
RCC_Config();
GPIO_Config();
TIM_Config();
NVIC_Config();
RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_1);
if (SysTick_Config(SystemCoreClock / 1000))
while (1)
}
void RCC_Config(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
if (RCC_WaitForHSEStartUp() == SUCCESS)
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);//復(fù)用GPIOA_Pin1為TIM2_Ch2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);
}
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);//初始化TIM2寄存器
TIM_TimeBaseStructure.TIM_Period = 10000-1; //查數(shù)據(jù)手冊(cè)可知,TIM2與TIM5為32位自動(dòng)裝載
TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上計(jì)數(shù)
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1為正常占空比模式,PWM2為反極性模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2000; //輸入CCR(占空比數(shù)值)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //High為占空比高極性,此時(shí)占空比為20%;Low則為反極性,占空比為80%
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//CCR自動(dòng)裝載默認(rèn)也是打開的
TIM_ARRPreloadConfig(TIM2, ENABLE); //ARR自動(dòng)裝載默認(rèn)是打開的,可以不設(shè)置
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2, ENABLE); //使能TIM2定時(shí)器
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中斷優(yōu)先級(jí)分組為1
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(__IO uint32_t nTime)
{
}
void TimingDelay_Decrement(void)
{
}
中斷服務(wù)函數(shù):
void TIM2_IRQHandler(void)
{
}
評(píng)論