Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | File Name: TimeDelay.c |
||
4 | Dependencies: None |
||
5 | Processor: PIC18/PIC24/dsPIC30/dsPIC33/PIC32 |
||
6 | Compiler: C30 v3.12 |
||
7 | Company: Microchip Technology, Inc. |
||
8 | |||
9 | Copyright (C) 2010 Microchip Technology Inc. All rights reserved. |
||
10 | |||
11 | Microchip licenses to you the right to use, modify, copy and distribute |
||
12 | Software only when embedded on a Microchip microcontroller or digital signal |
||
13 | controller that is integrated into your product or third party product |
||
14 | (pursuant to the sublicense terms in the accompanying license agreement). |
||
15 | |||
16 | You should refer to the license agreement accompanying this Software for |
||
17 | additional information regarding your rights and obligations. |
||
18 | |||
19 | SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
||
20 | EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF |
||
21 | MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. |
||
22 | IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER |
||
23 | CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR |
||
24 | OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES |
||
25 | INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR |
||
26 | CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF |
||
27 | SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES |
||
28 | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. |
||
29 | |||
30 | Author Date Comments |
||
31 | -------------------------------------------------------------------------------- |
||
32 | AKN 2009.10.14 FILE CREATED |
||
33 | AKN 2009.10.15 CHANGED C18 DELAY ROUTINE TO DECREMENT ENTIRE NUMBER OF CYCLES |
||
34 | AKN 2009.10.19 CHANGED C30 DELAY ROUTINE TO MATCH C18 IMPLEMENTATION |
||
35 | AKN 2009.10.26 ADDED C32 DELAY ROUTINE TO MATCH C18 IMPLEMENTATION |
||
36 | AKN 2009.10.27 CONSOLIDATED C30 AND C32 IMPLEMENTATIONS, ADDED PADDING TO |
||
37 | MAKE C30 DELAYS MORE ACCURATE |
||
38 | PAT 2010.01.26 CONVERTED LOCALS TO VOLATILE |
||
39 | PAT 2010.03.07 ADDED include "Compiler.h" |
||
40 | *******************************************************************************/ |
||
41 | #if defined(__PIC32MX__) |
||
42 | #include <plib.h> |
||
43 | #endif |
||
44 | #include "Compiler.h" |
||
45 | #include "HardwareProfile.h" |
||
46 | #include "TimeDelay.h" |
||
47 | |||
48 | /**************************************************************************** |
||
49 | Function: |
||
50 | void Delay10us( UINT32 tenMicroSecondCounter ) |
||
51 | |||
52 | Description: |
||
53 | This routine performs a software delay in intervals of 10 microseconds. |
||
54 | |||
55 | Precondition: |
||
56 | None |
||
57 | |||
58 | Parameters: |
||
59 | UINT32 tenMicroSecondCounter - number of ten microsecond delays |
||
60 | to perform at once. |
||
61 | |||
62 | Returns: |
||
63 | None |
||
64 | |||
65 | Remarks: |
||
66 | None |
||
67 | ***************************************************************************/ |
||
68 | void Delay10us( UINT32 tenMicroSecondCounter ) |
||
69 | { |
||
70 | volatile INT32 cyclesRequiredForEntireDelay; |
||
71 | |||
72 | #if defined(__18CXX) |
||
73 | |||
74 | if (GetInstructionClock() <= 2500000) //for all FCY speeds under 2MHz (FOSC <= 10MHz) |
||
75 | { |
||
76 | //26 cycles burned through this path (includes return to caller). |
||
77 | //For FOSC == 1MHZ, it takes 104us. |
||
78 | //For FOSC == 4MHZ, it takes 26us |
||
79 | //For FOSC == 8MHZ, it takes 13us. |
||
80 | //For FOSC == 10MHZ, it takes 10.5us. |
||
81 | } |
||
82 | else |
||
83 | { |
||
84 | //14 cycles burned to this point. |
||
85 | |||
86 | //We want to pre-calculate number of cycles required to delay 10us * tenMicroSecondCounter using a 1 cycle granule. |
||
87 | cyclesRequiredForEntireDelay = (INT32)(GetInstructionClock()/100000) * tenMicroSecondCounter; |
||
88 | |||
89 | //We subtract all the cycles used up until we reach the while loop below, where each loop cycle count is subtracted. |
||
90 | //Also we subtract the 22 cycle function return. |
||
91 | cyclesRequiredForEntireDelay -= (153 + 22); |
||
92 | |||
93 | if (cyclesRequiredForEntireDelay <= 45) |
||
94 | { |
||
95 | // If we have exceeded the cycle count already, bail! Best compromise between FOSC == 12MHz and FOSC == 24MHz. |
||
96 | } |
||
97 | else |
||
98 | { |
||
99 | //Try as you may, you can't get out of this heavier-duty case under 30us. ;] |
||
100 | |||
101 | while (cyclesRequiredForEntireDelay>0) //153 cycles used to this point. |
||
102 | { |
||
103 | Nop(); //Delay one instruction cycle at a time, not absolutely necessary. |
||
104 | cyclesRequiredForEntireDelay -= 42; //Subtract cycles burned while doing each delay stage, 42 in this case. |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | #elif defined(__C30__) || defined(__PIC32MX__) |
||
110 | |||
111 | if(GetInstructionClock() <= 500000) //for all FCY speeds under 500KHz (FOSC <= 1MHz) |
||
112 | { |
||
113 | //10 cycles burned through this path (includes return to caller). |
||
114 | //For FOSC == 1MHZ, it takes 5us. |
||
115 | //For FOSC == 4MHZ, it takes 0.5us |
||
116 | //For FOSC == 8MHZ, it takes 0.25us. |
||
117 | //For FOSC == 10MHZ, it takes 0.2us. |
||
118 | } |
||
119 | else |
||
120 | { |
||
121 | //7 cycles burned to this point. |
||
122 | |||
123 | //We want to pre-calculate number of cycles required to delay 10us * tenMicroSecondCounter using a 1 cycle granule. |
||
124 | cyclesRequiredForEntireDelay = (INT32)(GetInstructionClock()/100000)*tenMicroSecondCounter; |
||
125 | |||
126 | #if defined(__C30__) |
||
127 | //We subtract all the cycles used up until we reach the while loop below, where each loop cycle count is subtracted. |
||
128 | //Also we subtract the 5 cycle function return. |
||
129 | cyclesRequiredForEntireDelay -= 44; //(29 + 5) + 10 cycles padding |
||
130 | #elif defined(__PIC32MX__) |
||
131 | //We subtract all the cycles used up until we reach the while loop below, where each loop cycle count is subtracted. |
||
132 | //Also we subtract the 5 cycle function return. |
||
133 | cyclesRequiredForEntireDelay -= 24; //(19 + 5) |
||
134 | #endif |
||
135 | |||
136 | if(cyclesRequiredForEntireDelay <= 0) |
||
137 | { |
||
138 | // If we have exceeded the cycle count already, bail! |
||
139 | } |
||
140 | else |
||
141 | { |
||
142 | while(cyclesRequiredForEntireDelay>0) //19 cycles used to this point. |
||
143 | { |
||
144 | #if defined(__C30__) |
||
145 | cyclesRequiredForEntireDelay -= 11; //Subtract cycles burned while doing each delay stage, 12 in this case. Add one cycle as padding. |
||
146 | #elif defined(__PIC32MX__) |
||
147 | cyclesRequiredForEntireDelay -= 8; //Subtract cycles burned while doing each delay stage, 8 in this case. |
||
148 | #endif |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | #endif |
||
153 | } |
||
154 | |||
155 | /**************************************************************************** |
||
156 | Function: |
||
157 | void DelayMs( UINT16 ms ) |
||
158 | |||
159 | Description: |
||
160 | This routine performs a software delay in intervals of 1 millisecond. |
||
161 | |||
162 | Precondition: |
||
163 | None |
||
164 | |||
165 | Parameters: |
||
166 | UINT16 ms - number of one millisecond delays to perform at once. |
||
167 | |||
168 | Returns: |
||
169 | None |
||
170 | |||
171 | Remarks: |
||
172 | None |
||
173 | ***************************************************************************/ |
||
174 | void DelayMs( UINT16 ms ) |
||
175 | { |
||
176 | #if defined(__18CXX) |
||
177 | |||
178 | INT32 cyclesRequiredForEntireDelay; |
||
179 | |||
180 | // We want to pre-calculate number of cycles required to delay 1ms, using a 1 cycle granule. |
||
181 | cyclesRequiredForEntireDelay = (signed long)(GetInstructionClock()/1000) * ms; |
||
182 | |||
183 | // We subtract all the cycles used up until we reach the while loop below, where each loop cycle count is subtracted. |
||
184 | // Also we subtract the 22 cycle function return. |
||
185 | cyclesRequiredForEntireDelay -= (148 + 22); |
||
186 | |||
187 | if (cyclesRequiredForEntireDelay <= (170+25)) |
||
188 | { |
||
189 | return; // If we have exceeded the cycle count already, bail! |
||
190 | } |
||
191 | else |
||
192 | { |
||
193 | while (cyclesRequiredForEntireDelay > 0) //148 cycles used to this point. |
||
194 | { |
||
195 | Nop(); // Delay one instruction cycle at a time, not absolutely necessary. |
||
196 | cyclesRequiredForEntireDelay -= 39; // Subtract cycles burned while doing each delay stage, 39 in this case. |
||
197 | } |
||
198 | } |
||
199 | |||
200 | #elif defined(__C30__) || defined(__PIC32MX__) |
||
201 | |||
202 | volatile UINT8 i; |
||
203 | |||
204 | while (ms--) |
||
205 | { |
||
206 | i = 4; |
||
207 | while (i--) |
||
208 | { |
||
209 | Delay10us( 25 ); |
||
210 | } |
||
211 | } |
||
212 | #endif |
||
213 | } |
||
214 |
Powered by WebSVN v2.8.3