-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxlcd_delays.c
99 lines (82 loc) · 2.46 KB
/
xlcd_delays.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
/** \file xlcd_delays.c
\brief Implements delay functions for the C18 library for the LCD screen
\author Daniel Tam
\version 1
\date September 2016
*/
/** Delay for 18 cycles. Uses 14 NOP assembly commands
since it takes 4 cycles to enter and exit the function. */
void DelayFor18TCY(void)
{
_asm
NOP NOP NOP NOP
NOP NOP NOP NOP
NOP NOP NOP NOP
NOP NOP
_endasm
}
#if defined(__18F452)
/** Delay for 15 milliseconds, which is 15000 cycles on a 4 MHz processor
* as the instruction rate is /4 i.e. 1 MHz */
void DelayPORXLCD(void)
{
int i = 1151;
// These NOP commands precisely tune the function to 15000 cycles and
// disable compiler optimization
_asm NOP NOP NOP NOP NOP NOP NOP _endasm
// This loop takes 13 cycles each iteration (checking, entering, exiting)
while(i)
{
--i;
}
}
/** Delay for 5 milliseconds, which is 5000 cycles on a 4 MHz processor
* as the instruction rate is /4 i.e. 1 MHz */
void DelayXLCD(void)
{
int i = 382;
// These NOP commands precisely tune the function to 5000 cycles and
// disable compiler optimization
_asm NOP NOP NOP NOP _endasm
// This loop takes 13 cycles each iteration (checking, entering, exiting)
while(i)
{
--i;
}
}
#elif defined(__18F4520)
/** Delay for 15 milliseconds, which is 37500 cycles on a 10 MHz processor
* as the instruction rate is /4 i.e. 2.5 MHz */
void DelayPORXLCD(void)
{
int i = 2882;
// These NOP commands precisely tune the function to 37500 cycles and
// disable compiler optimization
_asm
NOP NOP NOP NOP
_endasm
// This loop takes 13 cycles each iteration (checking, entering, exiting)
while(i)
{
--i;
}
}
/** Delay for 5 milliseconds, which is 12500 cycles on a 10 MHz processor
* as the instruction rate is /4 i.e. 2.5 MHz */
void DelayXLCD(void)
{
int i = 959;
// These NOP commands precisely tune the function to 12500 cycles and
// disable compiler optimization
_asm
NOP NOP NOP
_endasm
// This loop takes 13 cycles each iteration (checking, entering, exiting)
while(i)
{
--i;
}
}
#else
#error Unknown processor!
#endif