-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelay.c
119 lines (98 loc) · 3.05 KB
/
delay.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
#include "delay.h"
#include "stm32f10x_it.h"
uint16_t TIM6_cnt;
uint16_t TIM7_cnt;
/*
TIM6 configurations.
Generate time delay of microseconds.
*/
void TIM6_RCC_Config(void);
void TIM6_NVIC_Config(void);
void TIM6_Config(void);
void TIM6_RCC_Config(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
}
void TIM6_NVIC_Config(void) {
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM6_Config(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
TIM_TimeBaseInitStructure.TIM_Period = 71; // 1 us between interruptions
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
}
/*
TIM7 configurations.
Generate time delay of milliseconds.
*/
void TIM7_RCC_Config(void);
void TIM7_NVIC_Config(void);
void TIM7_Config(void);
void TIM7_RCC_Config(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
}
void TIM7_NVIC_Config(void) {
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM7_Config(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
TIM_TimeBaseInitStructure.TIM_Period = 7199; // 0.1 ms between interruptions
TIM_TimeBaseInitStructure.TIM_Prescaler = 9;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
}
// Delay functions.
void DelayInit(void)
{
TIM6_RCC_Config();
TIM6_NVIC_Config();
TIM6_Config();
TIM7_RCC_Config();
TIM7_NVIC_Config();
TIM7_Config();
}
void Delay_us(uint16_t us)
{
TIM6_cnt = us + 1;
TIM_Cmd(TIM6, ENABLE);
while (TIM6_cnt > 0);
TIM_Cmd(TIM6, DISABLE);
}
// Interruption handler.
void TIM6_IRQHandler(void) {
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
if (TIM6_cnt > 0) {
TIM6_cnt--;
}
}
void Delay_ms(uint16_t ms)
{
TIM7_cnt = ms + 1;
TIM_Cmd(TIM7, ENABLE);
while (TIM7_cnt > 0);
TIM_Cmd(TIM7, DISABLE);
}
// Interruption handler.
void TIM7_IRQHandler(void) {
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
if (TIM7_cnt > 0) {
TIM7_cnt--;
}
}