Professional Documents
Culture Documents
-----------------------------------------
/* Filename: Delay.h
* Delay functions for HI-TECH C on the PIC
*
* Functions available:
* DelayUs(x) Delay specified number of microseconds
* DelayMs(x) Delay specified number of milliseconds
*
* Note that there are range limits: x must not exceed 255 - for xtal
* frequencies > 12MHz the range for DelayUs is even smaller.
* To use DelayUs it is only necessary to include this file; to use
* DelayMs you must include delay.c in your project.
*
*/
/* Set the crystal frequency in the CPP predefined symbols list in
HPDPIC, or on the PICC commmand line, e.g.
picc -DXTAL_FREQ=4MHZ
or
picc -DXTAL_FREQ=100KHZ
Note that this is the crystal frequency, the CPU clock is
divided by 4.
* MAKE SURE this code is compiled with full optimization!!!
*/
#ifndef XTAL_FREQ
#define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
#endif
#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */
#if XTAL_FREQ >= 12MHZ
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
while(--_dcnt != 0) \
continue; }
#else
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
while(--_dcnt != 0) \
continue; }
#endif
extern void DelayMs(unsigned char);
---------------------------------------------------
;===============================
delay.c
/*
--- Delay functions
--- This is delay.c ---
See delay.h for details
Make sure this code is compiled with full optimization!!!
*/
#include "delay.h"
#define MHZ *1
#ifndef XTAL_FREQ
#define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
#endif
#define DelayUs(x)
{ unsigned char _dcnt; \
if(x>=4) _dcnt=(x*(FREQ_MULT)/2); \
else _dcnt=1; \
while(--_dcnt > 0) \
{\
asm("nop");\
asm("nop");\
continue; }\
}
#ifndef DELAY_H
#define DELAY_H
#define FOSC 29490L /* Internal clock freq in KHZ*/
#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */
//===============================
// //_dcnt = ((x* FOSC)/(12MHZ)); \
#define DelayMicroS(x) { unsigned char _dcnt; \
_dcnt = ((x* FOSC)/(20MHZ)); \ //-- change 20MHz to your frequ
ency --
while(--_dcnt != 0) \
continue; }
//===============================
// --- cho them Delay_uS (x) ---
void DelayuS(unsigned int cnt)
{
unsigned char i;
do {
i = 2;
do {
DelayMicroS(2);
} while(--i);
} while(--cnt);
}
//===============================
void DelayMs(unsigned int cnt)
{
unsigned char i;
do {
i = 20;
do {
DelayMicroS(50);
} while(--i);
} while(--cnt);
}
//===============================
// use this function when need a delay loop from an ISR
// should not usually delay within an ISR but if you have too.....
void ISR_DelayMs(unsigned char cnt)
{
unsigned char i;
do {
i = 20;
do {
DelayMicroS(50);
} while(--i);
} while(--cnt);
}
//===============================
char kill_delay;
void DelayMsKill(unsigned char cnt)
{
unsigned char i;
do {
i = 20;
do {
DelayMicroS(50);
} while(--i && !kill_delay);
} while(--cnt && !kill_delay);
}
//
// Noted: I use only two functions
extern void DelayMs(unsigned int cnt);
extern void DelayuS(unsigned int cnt);
#endif
;===============================
You should look deep into the routine above and try to modify to feed your need.
The "delay_Mcc18.h" is only header need for using Microchip MCC18. This header s
hould work with Hi-Tech also with little syntech change. I only work with Hi-Tec
h compiler for about a month or so, so I don't know much about this compiler. C
is new for me; I am an assembly men. I use MCC18 on only one project so my knowl
edge is only limited.
I have noted that you say for 30 sec delay you get 6 extra sec of de lay. I sugg
est you to change the following in your delay.h
#if XTAL_FREQ <= 2MHZ
do {
DelayUs(996); // --- change this number to lower value and experiment w
ith it
} while(--cnt); // --- until you get exact 30 seconds ----------------
#endif