Delay function help

Thread Starter

nevermine

Joined Nov 30, 2009
7
hi,im doing the PIC_BASE_C_4 example which is driving 7segment,but have problems with the DelayS().
i dont have the stdmacros-HTC.h in my hi-tech C include folder but i found it in this forum. then i add it into my header file,it still cant work...
besides,i tried Delay_MS(100); or delay_ms(100); both of the cant work,is there any other file that i need to include?
currently i just #include <htc.h>

is there anyway to write a delay function myself? probably delayms/delays...
thanks in advance
 

t06afre

Joined May 11, 2009
5,934
I think this example is somewhat old. I can not find any delay.h in my HI-Tech version(9.71). You could try to comment out the #include
"delay.h" line in the stdmacros-HTC.h All time delays are now defined in the htc.h r paste in the and replace
Rich (BB code):
#define DelayS(T) {unsigned char i; for (i=0; i<T*10; i++) DelayMs(100);}
with
Rich (BB code):
#define DelayS(T) {unsigned char i; for (i=0; i<T*10; i++) __delay_ms(100);}
 

BMorse

Joined Sep 26, 2009
2,675
in Hi-Tech PICC Lite the syntax for the delay is:

__delay_ms(100)

or try this depending on what version you are using....

_delay_ms(100)


B. Morse
 

Thread Starter

nevermine

Joined Nov 30, 2009
7
in Hi-Tech PICC Lite the syntax for the delay is:

__delay_ms(100)

or try this depending on what version you are using....

_delay_ms(100)


B. Morse
yea thats what im doing in my code, but it doesnt work,with the error "undefined object "__delay_ms()". im did include the htx.h file and defined the _XTAL_FREQ 4000000...
Rich (BB code):
	for (;;)
	{
		for (digit=0; digit<10;digit++)
		{
			PORTC= pat7seg[digit] >> 1;
			RB2= pat7seg[digit] & 0b0000001;
		
			__delay_ms(100);
		}
	}
 

t06afre

Joined May 11, 2009
5,934
This is something that is fixed in the 9.71a version of the compiler. You could download it from Microchip, for eval and Lite mode usage. You application do not need high speed. May I suggest you run your CPU on a lower frequency. Insert this early in the code. If your OSC is 4MHz it will be the same as your is running on 500KHz osc. So you must use #define _XTAL_FREQ 500000 in your code
Rich (BB code):
OSCCON=0b00110000;//500KHz osc
You can also download the 9.71a version here for eval/Lite mode. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en542849
 
Last edited:

BMorse

Joined Sep 26, 2009
2,675
I use version 9.65 and 9.70 (different PC's) and I can use the functions just fine..... I will have to post some code here when I get home tonight...... I have another delay routine I use that is based on Timer0 and its interrupt, which I find very useful to be able to do other tasks while the delay is running.....

B. Morse
 

t06afre

Joined May 11, 2009
5,934
Then _delay(x) exceeds maximum limit of 197120 cycles. You will get this error. The __delay_ms() is defined as shown
Rich (BB code):
#define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
If you use 4MHz as clock and want a 100ms delay. The input to the _delay function will be 100000 cycles. So it should be OK. But due to a some sort of bug it will not work. But _delay(100000) will work. This do not depend on which mode you use for the compiler (PRO/Lite).
 
Last edited:

Thread Starter

nevermine

Joined Nov 30, 2009
7
Then _delay(x) exceeds maximum limit of 197120 cycles. You will get this error. The __delay_ms() is defined as shown
Rich (BB code):
#define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
If you use 4MHz as clock and want a 100ms delay. The input to the _delay function will be 100000 cycles. So it should be OK. But due to a some sort of bug it will not work. But _delay(100000) will work. This do not depend on which mode you use for the compiler (PRO/Lite).
Hi all,thanks for reply.
first of all, let say if im using 4Mhz crystal (internal) and i want to delay 100ms, so by applying the formula above, (100X4M/4000)=100K?then how can i use it?to write a delay function myself
btw, i found a delay function from google which look like below
Rich (BB code):
//**************************************
// Name: Delay functions for HI-TECH C on the PIC
// Description:Delay functions for HI-TECH C on the PIC
// By: SouthOfHeaven
//
//
// Inputs:None
//
// Returns:None
//
//Assumes:None
//
//Side Effects:None
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.7421/lngWId.3/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************

/*
*	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.*/
#ifndef	XTAL_FREQ
#define	XTAL_FREQ	4MHZ		/* Crystal frequency in MHz */
#endif
#define	MHZ	*1000			/* 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);
but it mention that "to use DelayMs you must include delay.c in your project."
but i couldnt found a delay.c i n my compiler...where can i get it?
the code is working for me,but if i want to delay like 100ms,den i need to repeat the delayUs for thousands of time...or use a for loop?
is there any way to derive the delayMs?

got my code,i just #include <htc.h> is it sufficient for my code?
and i look into the code there is no __delay_ms()...

sorry for my stupidness...
thanks in advance
 

t06afre

Joined May 11, 2009
5,934
My fault it is in the pic.h include file
Until you can download the 9.71a version from the htsoft site. you can put this in your code
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 4000000 //teling the compiler that clock speed is 4MHz
#define _100_ms (100)*(_XTAL_FREQ/4000.0)
 
void main(void)
{
_delay(_100_ms);
}
 

Thread Starter

nevermine

Joined Nov 30, 2009
7
By the way. The maximum number for the _delay() is now 50660096 cycles
hey , now the delay is working!!thanks so much for helping
btw, what if i wan to make a delay of 1 second? beside doing __dealy_ms(1000)...
in this code>> "#define _100_ms (100)*(_XTAL_FREQ/4000.0)" , "_100_ms" why 100? so that mean i can do a second my changing this value?
 

t06afre

Joined May 11, 2009
5,934
hey , now the delay is working!!thanks so much for helping
btw, what if i wan to make a delay of 1 second? beside doing __dealy_ms(1000)...
in this code>> "#define _100_ms (100)*(_XTAL_FREQ/4000.0)" , "_100_ms" why 100? so that mean i can do a second my changing this value?
That was a work-around to your problem If you are using version 9.71a you can do like this. It will work. Both in Lite and PRO mode. You will have 45 days to use PRO mode after that it is only Lite mode. But for hobbyist Lite mode will still do the job. The Link I gave is public and legal link so share as you want
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 4000000
//teling the compiler that clock speed is 4MHz
__CONFIG(/*Set your config bit correct or...:eek:*/);
void main(void)
{
__delay_ms(10000);//10 seconds wait no probem
}
 

Thread Starter

nevermine

Joined Nov 30, 2009
7
hi t06afre, i'd change my PIC16F505 to PIC16F887, then the delay function cannot work, with the error
"Error [499] ; 0. undefined symbol: _delayms(16F887.obj) "
but previously i tried on 16F505 it is not neccesary to define the _delay.
does it concern with the __CONFIG()?
thanks
 
Top