Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Software Delays, Hardcoded or Not?

Status
Not open for further replies.
Re: what all the difficulties in writing first c program in mplab ide for 16f877a ?

I am sure you would agree that hard-coding a parameter that is open for change is not a great idea.

No, I do not agree.

This is NOT a hardcoded delay:

Code:
#define _XTAL_FREQ 8000000

The above define of _XTAL_FREQ allows the generated assembly code to produce a precise and accurate delay when the following routine is called:

Code:
_delay_ms();


While the following routine is poorly designed and offers virtually no precision or accuracy:

Code:
void delay_ms ( int delay )
{
	int ms, i;
 
	for ( ms = 0; ms < delay; ms ++ )
		for ( i = 0; i < 5; i ++ );
}


Isn't there a better way?

No.

For a software delay to offer both precision and accuracy the actual system clock frequency must be taken into account during code generation.

Otherwise, any change in system clock frequency or compiler version could require painstaking manual tuning of routines like the following:

Code:
void delay_ms ( int delay )
{
	int ms, i;
 
	for ( ms = 0; ms < delay; ms ++ )
		for ( i = 0; i < 5; i ++ );
}


Of course, there is always the option to utilize the hardware timers to produce a precise and accurate delay.


BigDog
 

Re: what all the difficulties in writing first c program in mplab ide for 16f877a ?

No, I do not agree.

This is NOT a hardcoded delay:

Code:
#define _XTAL_FREQ 8000000

Surely if one has to change the source code in order to change a value then it is hard-coded.

If you run the program on another device with a different clock freq, or change the freq on the given device, then you have to change the source code - correct?

Then it is hard-coded. It is not a run-time parameter, is not derived from the system, it is not a value on a file.
 

The delay is propertional to the clock frequency so it has to be known beforehand for the duration of each instruction used in the delay to be used the the calculation. If you mean "can it work out it's own clock frequency?" the answer is no, unless some other known reference can be used to measure it against.

Although the calculations to generate the delays are hard coded, it should be possible to pass the clock frequency to the compiler as an external parameter if you don't want the source code itself to be changed but it would have to be recompiled for each different frequency you pass to it.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top