-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysTick.c
99 lines (90 loc) · 2.06 KB
/
SysTick.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
#include "SysTick.h"
// Initialize the SysTick with busy wait running at bus clock
void SysTick_Init(void)
{
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_RELOAD_R = 0x00FFFFFF; // maximum reload value
NVIC_ST_CURRENT_R = 0; // any write to current clears it
NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock
}
// Time dela using busy wait.
// The delay parameter is in units of core clock.
void SysTick_wait(unsigned long delay)
{
volatile unsigned long elapsedTime;
unsigned long startTime = NVIC_ST_CURRENT_R;
do
{
elapsedTime = (startTime - NVIC_ST_CURRENT_R) & 0x00FFFFFF;
}
while(elapsedTime <= delay);
}
// Time delay using busy wait.
// This assumes 80 MHz sysytem clock
void SysTick_wait1microS(unsigned long delay)
{
unsigned long i;
for (i = 0; i < delay; i++)
{
SysTick_wait(80); // wait 1 micro seconds (assumes 80 MHz clock)
}
}
// Time delay using busy wait.
// This assumes 80 MHz sysytem clock
void SysTick_wait1miliS(unsigned long delay)
{
unsigned long i;
for (i = 0; i < delay; i++)
{
SysTick_wait(80000); // wait 1 mili seconds (assumes 80 MHz clock)
}
}
// Time delay using busy wait.
// This assumes 80 MHz sysytem clock
void SysTick_wait10miliS(unsigned long delay)
{
unsigned long i;
for (i = 0; i < delay; i++)
{
SysTick_wait(800000); // wait 10 mili seconds (assumes 80 MHz clock)
}
}
void Delay1micros ( unsigned long microsec )
{
unsigned long i;
while ( microsec > 0 )
{
i = 16; // this number means 1 micro s
while ( i > 0 )
{
i = i - 1;
}
microsec = microsec - 1; // decrements every 1 micro s
}
}
void Delay1ms ( unsigned long msec )
{
unsigned long i;
while ( msec > 0 )
{
i = 16000; // this number means 1 mili s
while ( i > 0 )
{
i = i - 1;
}
msec = msec - 1; // decrements every 1 mili s
}
}
void Delay1s ( unsigned long sec )
{
unsigned long i;
while ( sec > 0 )
{
i = 16000000; // this number means 1 s
while ( i > 0 )
{
i = i - 1;
}
sec = sec - 1; // decrements every 1 s
}
}