-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrv_time.c
150 lines (113 loc) · 3.48 KB
/
Drv_time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/******************** (C) COPYRIGHT 2017 ANO Tech ********************************
* 作者 :匿名科创
* 官网 :www.anotc.com
* 淘宝 :anotc.taobao.com
* 技术Q群 :190169595
* 描述 :定时器驱动和滴答配置
**********************************************************************************/
#include "Drv_time.h"
#include "include.h"
#include "Drv_led.h"
#define SYS_TIMx TIM2
#define SYS_RCC_TIMx RCC_APB1Periph_TIM2
void TIM_CONF() //APB1 84M
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* 使能时钟 */
RCC_APB1PeriphClockCmd ( SYS_RCC_TIMx, ENABLE );
TIM_DeInit ( SYS_TIMx );
/* 自动重装载寄存器周期的值(计数值) */
TIM_TimeBaseStructure.TIM_Period = 1000;
/* 累计 TIM_Period个频率后产生一个更新或者中断 */
/* 时钟预分频数为72 */
TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1;
/* 对外部时钟进行采样的时钟分频,这里没有用到 */
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit ( SYS_TIMx, &TIM_TimeBaseStructure );
TIM_ClearFlag ( SYS_TIMx, TIM_FLAG_Update );
TIM_ITConfig ( SYS_TIMx, TIM_IT_Update, ENABLE );
TIM_Cmd ( SYS_TIMx, ENABLE );
RCC_APB1PeriphClockCmd ( SYS_RCC_TIMx , DISABLE ); /*先关闭等待使用*/
}
void TIM_NVIC()
{
NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_TIME_P;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_TIME_S;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init ( &NVIC_InitStructure );
}
void TIM_INIT()
{
TIM_CONF();
TIM_NVIC();
/* TIM2 重新开时钟,开始计时 */
RCC_APB1PeriphClockCmd ( SYS_RCC_TIMx , ENABLE );
}
volatile uint32_t sysTickUptime = 0;
void SysTick_Configuration ( void )
{
RCC_ClocksTypeDef rcc_clocks;
uint32_t cnts;
RCC_GetClocksFreq ( &rcc_clocks );
cnts = ( uint32_t ) rcc_clocks.HCLK_Frequency / TICK_PER_SECOND;
cnts = cnts / 8;
SysTick_Config ( cnts );
SysTick_CLKSourceConfig ( SysTick_CLKSource_HCLK_Div8 );
}
uint32_t GetSysTime_us ( void )
{
register uint32_t ms;
u32 value;
do
{
ms = sysTickUptime;
value = ms * TICK_US + ( SysTick->LOAD - SysTick->VAL ) * TICK_US / SysTick->LOAD;
}
while(ms != sysTickUptime);
return value;
}
void Delay_us ( uint32_t us )
{
uint32_t now = GetSysTime_us();
while ( GetSysTime_us() - now < us );
}
void Delay_ms ( uint32_t ms )
{
while ( ms-- )
Delay_us ( 1000 );
}
u32 systime_ms;
void sys_time()
{
systime_ms++;
}
u32 SysTick_GetTick(void)
{
return systime_ms;
}
u32 Get_Delta_T(_get_dT_st *data)
{
data->old = data->now; //上一次的时间
data->now = GetSysTime_us(); //本次的时间
data->dT = ( ( data->now - data->old ) );//间隔的时间(周期)
if(data->init_flag == 0)
{
data->init_flag = 1;//第一次调用时输出 0 作为初始化,以后正常输出
return 0;
}
else
{
return data->dT;
}
}
void SysTick_Handler(void)
{
sysTickUptime++;
sys_time();
LED_1ms_DRV();
}
/******************* (C) COPYRIGHT 2014 ANO TECH *****END OF FILE************/