新聞中心

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

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

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

(我的外部時(shí)鐘為25MHz)

本文引用地址:http://m.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)前下指輸出(此時(shí)為滅)
}
else
{
GPIO_ResetBits(GPIOG, GPIO_Pin_6); //Resetbits屏蔽IO,當(dāng)前下指不輸出(此時(shí)為亮)
}
}
}


void RCC_Config(void)
{
RCC_DeInit(); //RCC寄存器初始化
RCC_HSEConfig(RCC_HSE_ON); //使用外部時(shí)鐘
if (RCC_WaitForHSEStartUp() == SUCCESS)//等待外部時(shí)鐘啟動(dòng)
{
RCC_PLLCmd(DISABLE); //配置PLL前應(yīng)先關(guān)閉主PLL
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //選擇PLL時(shí)鐘為系統(tǒng)時(shí)鐘
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK(AHB)時(shí)鐘為系統(tǒng)時(shí)鐘1分頻
RCC_PCLK1Config(RCC_HCLK_Div4); //PCLK1(APB1)時(shí)鐘為HCLK時(shí)鐘8分頻,則TIM2時(shí)鐘為HCLK時(shí)鐘4分頻
RCC_PCLK2Config(RCC_HCLK_Div2); //PCLK2(APB2)時(shí)鐘為HCLK時(shí)鐘2分頻
RCC_PLLConfig(RCC_PLLSource_HSE, 25, 336, 2, 7); //PLL時(shí)鐘配置,公式詳見‘system_stm43f4xx.c’ Line149
RCC_PLLCmd(ENABLE);//PLL時(shí)鐘開啟
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } //等待PLL時(shí)鐘準(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; //上拉與否對(duì)PWM產(chǎn)生無(wú)影響
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); //填入缺省值,其中后幾項(xiàng)只對(duì)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為占空比高極性,此時(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;//嵌套中斷通道為TIM2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優(yōu)先級(jí)為1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級(jí)為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;


}
}




評(píng)論


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

關(guān)閉