新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > stm32F407(CORTEX-M4)之RCC配置與TIM2的PWM

stm32F407(CORTEX-M4)之RCC配置與TIM2的PWM

作者: 時間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
之前我已經(jīng)發(fā)過了關(guān)于TIM2的PWM,但是沒有自己配置RCC時鐘,用到的系統(tǒng)時鐘、HCLK、PCLK1與PCLK2都是系統(tǒng)啟動代碼里默認(rèn)設(shè)置的,但是由于應(yīng)用場合不同,默認(rèn)的設(shè)置可能不滿足用戶需要,這時便需要手動配置。還是同樣的問題,M4的資料太少,我只能參考函數(shù)的英文說明(我已經(jīng)習(xí)慣看英文了,呵呵)來自己配置,不敢獨(dú)享,現(xiàn)分享如下:

(我的外部時鐘為25MHz)

本文引用地址:http://butianyuan.cn/article/201611/321571.htm

#include

static __IO uint32_t TimingDelay;//__IO為volatile的宏定義
int8_t LED_Flag = 1;//LED燈翻轉(zhuǎn)標(biāo)志位

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);
}
while (1)
{

if (LED_Flag != 1)
{
GPIO_SetBits(GPIOG, GPIO_Pin_6); //setbits使能IO,當(dāng)前下指輸出(此時為滅)
}
else
{
GPIO_ResetBits(GPIOG, GPIO_Pin_6); //Resetbits屏蔽IO,當(dāng)前下指不輸出(此時為亮)
}
}
}


void RCC_Config(void)
{
RCC_DeInit(); //RCC寄存器初始化
RCC_HSEConfig(RCC_HSE_ON); //使用外部時鐘
if (RCC_WaitForHSEStartUp() == SUCCESS)//等待外部時鐘啟動
{
RCC_PLLCmd(DISABLE); //配置PLL前應(yīng)先關(guān)閉主PLL
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //選擇PLL時鐘為系統(tǒng)時鐘
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK(AHB)時鐘為系統(tǒng)時鐘1分頻
RCC_PCLK1Config(RCC_HCLK_Div4); //PCLK1(APB1)時鐘為HCLK時鐘8分頻,則TIM2時鐘為HCLK時鐘4分頻
RCC_PCLK2Config(RCC_HCLK_Div2); //PCLK2(APB2)時鐘為HCLK時鐘2分頻
RCC_PLLConfig(RCC_PLLSource_HSE, 25, 336, 2, 7); //PLL時鐘配置,公式詳見‘system_stm43f4xx.c’ Line149
RCC_PLLCmd(ENABLE);//PLL時鐘開啟
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } //等待PLL時鐘準(zhǔn)備好
}
}


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); //調(diào)用IO初始化函數(shù)


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //指定第一引腳
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //模式必須為復(fù)用!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //頻率為快速
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉與否對PWM產(chǎn)生無影響
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ù)手冊可知,TIM2與TIM5為32位自動裝載

TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上計數(shù)
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


TIM_OCStructInit(&TIM_OCInitStructure); //填入缺省值,其中后幾項只對TIM1和TIM8有效
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為占空比高極性,此時占空比為20%;Low則為反極性,占空比為80%
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//CCR自動裝載默認(rèn)也是打開的
TIM_ARRPreloadConfig(TIM2, ENABLE); //ARR自動裝載默認(rèn)是打開的,可以不設(shè)置

TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2, ENABLE); //使能TIM2定時器
}


void NVIC_Config(void)
{

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中斷優(yōu)先級分組為1
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//嵌套中斷通道為TIM2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優(yōu)先級為1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級為0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中斷
NVIC_Init(&NVIC_InitStructure);
}


void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0);
}


void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}

中斷服務(wù)函數(shù):

void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
LED_Flag= ~LED_Flag;


}
}




關(guān)鍵詞: stm32F407CORTEX-M4RCC配置PW

評論


技術(shù)專區(qū)

關(guān)閉