1 |
/*********************************************************************
|
2 |
*
|
3 |
* General Delay rouines
|
4 |
*
|
5 |
*********************************************************************
|
6 |
* FileName: delay.c
|
7 |
* Dependencies: compiler.h
|
8 |
* Processor: PIC18
|
9 |
* Complier: MCC18 v1.00.50 or higher
|
10 |
* HITECH PICC-18 V8.10PL1 or higher
|
11 |
* Company: Microchip Technology, Inc.
|
12 |
*
|
13 |
* Software License Agreement
|
14 |
*
|
15 |
* The software supplied herewith by Microchip Technology Incorporated
|
16 |
* (the “Company”) for its PICmicro® Microcontroller is intended and
|
17 |
* supplied to you, the Company’s customer, for use solely and
|
18 |
* exclusively on Microchip PICmicro Microcontroller products. The
|
19 |
* software is owned by the Company and/or its supplier, and is
|
20 |
* protected under applicable copyright laws. All rights are reserved.
|
21 |
* Any use in violation of the foregoing restrictions may subject the
|
22 |
* user to criminal sanctions under applicable laws, as well as to
|
23 |
* civil liability for the breach of the terms and conditions of this
|
24 |
* license.
|
25 |
*
|
26 |
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
|
27 |
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
|
28 |
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
29 |
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
|
30 |
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
|
31 |
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
32 |
*
|
33 |
* HiTech PICC18 Compiler Options excluding device selection:
|
34 |
* -FAKELOCAL -G -Zg -E -C
|
35 |
*
|
36 |
* Note: HITECH portions is adopted from PICC18 sample "delay.c".
|
37 |
*
|
38 |
* Author Date Comment
|
39 |
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
40 |
* Nilesh Rajbharti 5/9/02 Original (Rev 1.0)
|
41 |
* Nilesh Rajbharti 6/10/02 Fixed C18 ms and us routines
|
42 |
********************************************************************/
|
43 |
#ifndef DELAY_H
|
44 |
#define DELAY_H
|
45 |
|
46 |
#include "compiler.h"
|
47 |
|
48 |
#if defined(MCHP_C18)
|
49 |
#include <delays.h>
|
50 |
#endif
|
51 |
|
52 |
/*
|
53 |
* Delay functions for HI-TECH C on the PIC18
|
54 |
*
|
55 |
* Functions available:
|
56 |
* DelayUs(x) Delay specified number of microseconds
|
57 |
* DelayMs(x) Delay specified number of milliseconds
|
58 |
*
|
59 |
* Note that there are range limits:
|
60 |
* - on small values of x (i.e. x<10), the delay becomes less
|
61 |
* accurate. DelayUs is accurate with xtal frequencies in the
|
62 |
* range of 4-16MHZ, where x must not exceed 255.
|
63 |
* For xtal frequencies > 16MHz the valid range for DelayUs
|
64 |
* is even smaller - hence affecting DelayMs.
|
65 |
* To use DelayUs it is only necessary to include this file.
|
66 |
* To use DelayMs you must include delay.c in your project.
|
67 |
*
|
68 |
* Note that this is the crystal frequency, the CPU clock is
|
69 |
* divided by 4.
|
70 |
*
|
71 |
* MAKE SURE this code is compiled with full optimization!!!
|
72 |
*/
|
73 |
#define MHZ *1
|
74 |
|
75 |
|
76 |
#ifndef CLOCK_FREQ
|
77 |
#error CLOCK_FREQ must be defined.
|
78 |
#endif
|
79 |
|
80 |
#if CLOCK_FREQ < 8MHZ
|
81 |
#define uS_CNT 24 // 238 /* 4x to make 1 mSec */
|
82 |
#endif
|
83 |
#if CLOCK_FREQ == 8MHZ
|
84 |
#define uS_CNT 25 // 244
|
85 |
#else
|
86 |
#define uS_CNT 25 // 246
|
87 |
#endif
|
88 |
|
89 |
#define FREQ_MULT (CLOCK_FREQ)/(4MHZ)
|
90 |
|
91 |
#if defined(HITECH_C18)
|
92 |
#define Delay10us(x) \
|
93 |
{ \
|
94 |
unsigned char _dcnt; \
|
95 |
if(x>=4) \
|
96 |
_dcnt=(x*(FREQ_MULT))/4; \
|
97 |
else \
|
98 |
_dcnt=1; \
|
99 |
while(--_dcnt > 0) \
|
100 |
continue; \
|
101 |
}
|
102 |
|
103 |
|
104 |
void DelayMs(unsigned char ms);
|
105 |
|
106 |
#elif defined(MCHP_C18)
|
107 |
#define Delay10us(us) Delay10TCYx(((CLOCK_FREQ/4000000)*us))
|
108 |
#define DelayMs(ms) Delay1KTCYx((((CLOCK_FREQ/1000)*ms)/4000))
|
109 |
|
110 |
#endif
|
111 |
|
112 |
|
113 |
|
114 |
#endif
|