-
Notifications
You must be signed in to change notification settings - Fork 0
/
DELAY.c
98 lines (94 loc) · 3.44 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
/*
* DELAY.c
*
* Created on: Oct 26, 2018
* Author: Itachi
*/
#include "include.h"
#include "DELAY.h"
unsigned long Tick=0;
unsigned long Tick_monitor=0;
unsigned long tick_Timeout[5]={0};
unsigned long time_Timeout[5]={0};
unsigned long active_Timeout[5]={0};
/* SYSTICK Initialization
* Function:- Initialization for Systick timer then load 80 ->(80 * 1/(80*10^6) = 1us)
- STAY IN LOOP un
+ Change the Load value to create different time delay interval
+ Change the interrupt handler by "SystickIntRegister" function
*/
void Systick_Init(){
SysTickIntRegister(Systick_ISR); //Define Systick ISR
HWREG(NVIC_ST_CTRL)&=~0x07; //Disable Systick Timer during initialization
HWREG(NVIC_ST_RELOAD)=80-1;
HWREG(NVIC_ST_CURRENT)=0; //Write any value to clear the counter
HWREG(NVIC_ST_CTRL)|=0x07; //Enable Systick Timer and ARM for Systick interrupt
}
/* SYSTICK Overflow Interrupt Handler
* The SYSTICK timer automatically load value from RELOAD_R so there no need to update new value
* Interrupt after each 1us
* After 100ms: Execute monitor to indicate system state
Increase Tick_timeout
* Input: No
* Output: No
* Global variable affect:
- "Tick"
- "Tick_monitor"
- "active_Timeout[]"
- "tick_Timeout[]"
*/
void Systick_ISR()
{
unsigned char count_Timeout=0;
Tick++; //Increase every 1 us corresponding to Reload value
Tick_monitor++;
if(Tick_monitor>=100000)
{
SysTickDisable();
for(count_Timeout=0;count_Timeout<5;count_Timeout++)
{
if(active_Timeout[count_Timeout]==1) tick_Timeout[count_Timeout]++;
}
Monitor();
SysTickEnable();
}
};
/* Delay us second by SYSTICK
* Function: Delay the amount of "microseconds" micro second
* Input: The amount of microseconds to dtil delay enough amount of time except when Systick interrupt execute
- Use "HWREG" to change register value (more info in HW_type.h)
- Enable SYSTICK Interrupt
* Input: No
* Output: No
* Change this function:elay
* Output: No
* Change: Use flag so the system is able to jump to another thread
rather than stuck in 1 line code
-------------------------------------------------------*/
void delay_us(unsigned int microseconds) {
Tick=0; //Reset count value
while(Tick < microseconds);
}
/* Timeout(unsigned long Timeout_100ms)
* Function: Create a loop after "Timeout_100ms" will break that loop and return 1
* Input: - unsigned long "Timeout_100ms": time in microseconds
- unsigned char "Number": the Timeout block use because more than one Timeout can be used at a time
* Output: 1 if enough time in loop
* Change: Change time in loop, change the Number to using more Timeout block, then must add more code in Systick_ISR
and the definition of 2 arrays using in code bellow because the code is using just 5 Timeout block
* Affect global variable:
1. "Timeout_active"
2. "Tick_timeout"
*/
bool Timeout(unsigned long Timeout_100ms, unsigned char Number)
{
time_Timeout[Number]=Timeout_100ms;
active_Timeout[Number]=1;
if(tick_Timeout[Number]>=time_Timeout[Number])
{
tick_Timeout[Number]=0;
active_Timeout[Number]=0;
return 1;
}
else return 0;
}