Hitech C bietet eine Delay-Routine mit
ihren Samples an.. Falls du sie
nicht finden kannst :
Der Source-Code :
1 | #include "delay.h"
|
2 |
|
3 | void
|
4 | DelayMs(unsigned char cnt)
|
5 | {
|
6 | #if XTAL_FREQ <= 2MHZ
|
7 | do {
|
8 | DelayUs(996);
|
9 | } while(--cnt);
|
10 | #endif
|
11 |
|
12 | #if XTAL_FREQ > 2MHZ
|
13 | unsigned char i;
|
14 | do {
|
15 | i = 4;
|
16 | do {
|
17 | DelayUs(250);
|
18 | } while(--i);
|
19 | } while(--cnt);
|
20 | #endif
|
21 | }
|
Die Header-Datei :
1 | /*
|
2 | * Delay functions for HI-TECH C on the PIC
|
3 | *
|
4 | * Functions available:
|
5 | * DelayUs(x) Delay specified number of microseconds
|
6 | * DelayMs(x) Delay specified number of milliseconds
|
7 | *
|
8 | * Note that there are range limits: x must not exceed 255 - for xtal
|
9 | * frequencies > 12MHz the range for DelayUs is even smaller.
|
10 | * To use DelayUs it is only necessary to include this file; to use
|
11 | * DelayMs you must include delay.c in your project.
|
12 | *
|
13 | */
|
14 |
|
15 | /* Set the crystal frequency in the CPP predefined symbols list in
|
16 | HPDPIC, or on the PICC commmand line, e.g.
|
17 | picc -DXTAL_FREQ=4MHZ
|
18 |
|
19 | or
|
20 | picc -DXTAL_FREQ=100KHZ
|
21 |
|
22 | Note that this is the crystal frequency, the CPU clock is
|
23 | divided by 4.
|
24 |
|
25 | * MAKE SURE this code is compiled with full optimization!!!
|
26 |
|
27 | */
|
28 |
|
29 | #ifndef XTAL_FREQ
|
30 | #define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
|
31 | #endif
|
32 |
|
33 | #define MHZ *1000L /* number of kHz in a MHz */
|
34 | #define KHZ *1 /* number of kHz in a kHz */
|
35 |
|
36 | #if XTAL_FREQ >= 12MHZ
|
37 |
|
38 | #define DelayUs(x) { unsigned char _dcnt; \
|
39 | _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
|
40 | while(--_dcnt != 0) \
|
41 | continue; }
|
42 | #else
|
43 |
|
44 | #define DelayUs(x) { unsigned char _dcnt; \
|
45 | _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
|
46 | while(--_dcnt != 0) \
|
47 | continue; }
|
48 | #endif
|
49 |
|
50 | extern void DelayMs(unsigned char);
|
Wenn du das alles sauber eingebunden hast,
kannst du mit
DelayMs(100); // 100ms Delay
den Hamster halten lassen..
Schöner wärs natürlich mit Timers, aber first things first.. :-)
Fiel schbass