stm32快速学习7——LED闪烁(TIM2查询)
#include"stm32f10x_rcc.h"
#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)
{
//假设TIM2的时钟源APB1(PCLK1)是36M
//CK_CNT=36M/(TIM_Prescaler+1)预分频后的频率=定时器的时钟源/预分频数
//f=CK_CNT/(1+TIM_Period)定时器实际频率=预分频后的频率/溢出计数值
//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;//定时器计数值0-65535(0xFFFF)
TIM_TimeBaseStructure.TIM_Prescaler=3599;//分频计数0-65535(0xFFFF)
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_div1;//时间分割值,APB1(PCLK1)分频(频率降低),这个值固定了定时器最小周期时间,第一级的分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//选择向上计数
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//初始化定时器基本设定
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_Cmd(TIM2,ENABLE);//使能TIM2
}
stm32LED闪烁TIM2查 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)