/**
* @file Timer.c
* @author WB R&D Team - openmcu666
*
@version V0.1
*
@date 2015.7.9
*
@brief Timer
*/
#include "Timer.h"
__IO uint32_t TimingDelay = 0;
__IO uint32_t CaptureNumber = 0;
__IO uint32_t PeriodValue = 0;
/**
* @brief Configures TIM5 to measure the LSI oscillator frequency.
* @param None
* @retval LSI Frequency
*/
uint32_t GetLSIFrequency(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_ClocksTypeDef RCC_ClockFreq;
RCC_LSICmd(ENABLE); //使能LSI
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) //等待LSI稳定
{
}
/* TIM5 configuration *******************************************************/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能TIM5时钟
TIM_RemapConfig(TIM5, TIM5_LSI); //内部TIM5_CH4输入捕获连接到LSI时钟输出
TIM_PrescalerConfig(TIM5, 0, TIM_PSCReloadMode_Immediate); //设置预分频
/* TIM5 configuration: Input Capture mode ---------------------
The LSI oscillator is connected to TIM5 CH4
The Rising edge is used as active edge,
The TIM5 CCR4 is used to compute the frequency value
------------------------------------------------------------ */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM5, &TIM_ICInitStructure);
/* Enable TIM5 Interrupt channel */
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM5, ENABLE); //启动计数器
TIM5->SR = 0; //Reset the flags
TIM_ITConfig(TIM5, TIM_IT_CC4, ENABLE); //启动CC4中断请求
/* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler() in
stm32f4xx_it.c file) ******************************************************/
while(CaptureNumber != 2)
{
}
TIM_DeInit(TIM5); //设置为复位后的默认值
RCC_GetClocksFreq(&RCC_ClockFreq); //获取系统时钟,HCLK 和 PCLKx 频率
if ((RCC->CFGR & RCC_CFGR_PPRE1) == 0) //获取PCLK1预分频器
{
return ((RCC_ClockFreq.PCLK1_Frequency / PeriodValue) * 8);//PCLK1预分频器
}
else
{
return (((2 * RCC_ClockFreq.PCLK1_Frequency) / PeriodValue) * 8) ;
}
}
** 实验名:旺宝-红龙429-RNG实验
** KEIL MDK-ARM Standard Version:V5.10
** 软件库(keil) Version: unused
** 固件库(ST) Version: 1.5.0
** 实验说明:
实验原理:
硬件连接:
实验现象:程序运行后,串口打印RNG生成的32Bit随机数.
**********************************/