GeekIMU/2.Firmware/STM32/Firmware/sensor/time.c

127 lines
4.9 KiB
C
Raw Permalink Normal View History

2024-11-09 21:39:20 +08:00
/******************** (C) COPYRIGHT 2020 GEEKIMU *******************************
* File Name : time.c
* Current Version : V2.0
* Author : zhanli 719901725@qq.com
* Date of Issued : 2015.10.3
* Comments : TrackVR <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD> <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
********************************************************************************/
#include "time.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
/**----------------------------------------------------------------------
* Function : TIM3_Int_Init(uint16_t arr,uint16_t psc)
* Description : 1ms<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1000hz<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2021/6/22 zhanli
*---------------------------------------------------------------------**/
void TIM3_Int_Init(uint16_t arr,uint16_t psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>3 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EEB6AF><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5000Ϊ500ms*/
TIM_TimeBaseStructure.TIM_Period = arr;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪTIMxʱ<78><CAB1>Ƶ<EFBFBD>ʳ<EFBFBD><CAB3><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>Ƶֵ 10Khz<68>ļ<EFBFBD><C4BC><EFBFBD>Ƶ<EFBFBD><C6B5> */
TIM_TimeBaseStructure.TIM_Prescaler = psc;
/* <20><><EFBFBD><EFBFBD>ʱ<EFBFBD>ӷָ<D3B7>:TDTS = Tck_tim */
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
/* TIM<49><4D><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>ģʽ */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* <20><><EFBFBD><EFBFBD>TIM_TimeBaseInitStruct<63><74>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>TIMx<4D><78>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ */
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3, TIM_IT_Update ,ENABLE);
/* <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD> */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
/* <20><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>0<EFBFBD><30> */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
/* <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3<EFBFBD><33> */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
/* IRQͨ<51><CDA8><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* <20><><EFBFBD><EFBFBD>NVIC_InitStruct<63><74>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>NVIC<49>Ĵ<EFBFBD><C4B4><EFBFBD> */
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE);
}
/**----------------------------------------------------------------------
* Function : SysTick_Init
* Description : <EFBFBD><EFBFBD>Ҫ<EFBFBD>dz<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>4
* Author : zhanli&719901725@qq.com
* Date : 2015/10/4 zhanli
*---------------------------------------------------------------------**/
void SysTick_Init(void)
{
/* ** <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> ***/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*ʱ<><CAB1>ʹ<EFBFBD><CAB9>*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EEB6AF><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5000Ϊ500ms*/
TIM_TimeBaseStructure.TIM_Period = 0xffff;
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪTIMxʱ<78><CAB1>Ƶ<EFBFBD>ʳ<EFBFBD><CAB3><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>Ƶֵ 10Khz<68>ļ<EFBFBD><C4BC><EFBFBD>Ƶ<EFBFBD><C6B5>*/
TIM_TimeBaseStructure.TIM_Prescaler =71;
/*<2A><><EFBFBD><EFBFBD>ʱ<EFBFBD>ӷָ<D3B7>:TDTS = Tck_tim*/
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
/*TIM<49><4D><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>ģʽ*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/*<2A><><EFBFBD><EFBFBD>TIM_TimeBaseInitStruct<63><74>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>TIMx<4D><78>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ*/
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_Cmd(TIM4, ENABLE);
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>2 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EEB6AF><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5000Ϊ500ms */
TIM_TimeBaseStructure.TIM_Period = 0xffff;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪTIMxʱ<78><CAB1>Ƶ<EFBFBD>ʳ<EFBFBD><CAB3><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>Ƶֵ 10Khz<68>ļ<EFBFBD><C4BC><EFBFBD>Ƶ<EFBFBD><C6B5> */
TIM_TimeBaseStructure.TIM_Prescaler =71;
/* <20><><EFBFBD><EFBFBD>ʱ<EFBFBD>ӷָ<D3B7>:TDTS = Tck_tim */
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
/* TIM<49><4D><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>ģʽ */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* <20><><EFBFBD><EFBFBD>TIM_TimeBaseInitStruct<63><74>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>TIMx<4D><78>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ */
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
/**----------------------------------------------------------------------
* Function : Get_NowTime(void)
* Description : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>AHRS<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0),<EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ǰsystick<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>ֵ,32λ
* Author : zhanli&719901725@qq.com
* Date : 2017/1/15 zhanli
*---------------------------------------------------------------------**/
float Get_NowTime(void)
{
float temp = 0.0f;
static uint32_t now = 0; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD> <20><>λ us */
now = TIM4->CNT; /* <20><><EFBFBD><EFBFBD>16λʱ<CEBB><CAB1> */
TIM4->CNT = 0; /* <20><><EFBFBD>¼<EFBFBD>ʱ */
temp = (float)now / 2000000.0f; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ms<6D><73><EFBFBD>ٳ<EFBFBD><D9B3><EFBFBD>2<EFBFBD><32>(1/2 *T) */
return temp;
}