新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > stm32快速學(xué)習(xí)7——LED閃爍(TIM2查詢)

stm32快速學(xué)習(xí)7——LED閃爍(TIM2查詢)

作者: 時(shí)間:2016-11-17 來(lái)源:網(wǎng)絡(luò) 收藏
#include"stm32f10x_gpio.h"

#include"stm32f10x_rcc.h"

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

#include"stm32f10x_flash.h"

#include"stm32f10x_tim.h"

#include"misc.h"

voidRCC_Configuration(void);

voidGPIO_Configuration(void);

voidTIM_Configuration(void);

intmain(void)

{

RCC_Configuration();

GPIO_Configuration();

TIM_Configuration();

while(1)

{

if(TIM_GetFlagStatus(TIM2,TIM_FLAG_Update)!=RESET)

{

TIM_ClearFlag(TIM2,TIM_FLAG_Update);

if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0)==0)

GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);

else

GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);

}

}

}

voidRCC_Configuration(void)

{

ErrorStatusHSEStartUpStatus;

RCC_DeInit();

RCC_HSEConfig(RCC_HSE_ON);

HSEStartUpStatus=RCC_WaitForHSEStartUp();

if(HSEStartUpStatus==SUCCESS)

{

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

FLASH_SetLatency(FLASH_Latency_2);

RCC_HCLKConfig(RCC_SYSCLK_Div1);

RCC_PCLK2Config(RCC_HCLK_Div1);

RCC_PCLK1Config(RCC_HCLK_Div2);

RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)

{

}

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource()!=0x08)

{

}

}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

}

voidGPIO_Configuration(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);

}

voidTIM_Configuration(void)

{

//假設(shè)TIM2的時(shí)鐘源APB1(PCLK1)是36M

//CK_CNT=36M/(TIM_Prescaler+1)預(yù)分頻后的頻率=定時(shí)器的時(shí)鐘源/預(yù)分頻數(shù)

//f=CK_CNT/(1+TIM_Period)定時(shí)器實(shí)際頻率=預(yù)分頻后的頻率/溢出計(jì)數(shù)值

//f=36M/[(TIM_Prescaler+1)×(1+TIM_Period)]

//T=1/f

//T=[(TIM_Prescaler+1)×(1+TIM_Period)]/3600000

//1s=(3600×10000)/36M

//1s=[(3599+1)×(9999+1)]/36M

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period=9999;//定時(shí)器計(jì)數(shù)值0-65535(0xFFFF)

TIM_TimeBaseStructure.TIM_Prescaler=3599;//分頻計(jì)數(shù)0-65535(0xFFFF)

TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//時(shí)間分割值,APB1(PCLK1)分頻(頻率降低),這個(gè)值固定了定時(shí)器最小周期時(shí)間,第一級(jí)的分頻

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//選擇向上計(jì)數(shù)

TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//初始化定時(shí)器基本設(shè)定

TIM_ClearFlag(TIM2,TIM_FLAG_Update);

TIM_Cmd(TIM2,ENABLE);//使能TIM2

}



關(guān)鍵詞: stm32LED閃爍TIM2查

評(píng)論


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

關(guān)閉