#include "stm32f10x_rcc.h"
#include "stm32f10x_flash.h"
#include "stm32f10x_tim.h"
#include "misc.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Configuration(void);
int main(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);
}
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
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);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_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);
}
void TIM_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_TimeBaseInitTypeDef TIM_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
}
上一篇:stm32快速学习2——点亮LED
下一篇:stm32快速学习5——串口中断接收
推荐阅读最新更新时间:2024-03-16 15:12