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.

HOW TO CREATE exact 1ms delay using High tech C (HTC) compiler?(PIC16F84A,4MHz)

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
751
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,054
Hi all, i just started PIC programming few days ago . I prefer HIGH TECH C compiler for programming (in MPLAB). For making delay, i use

void delay(unsigned int z)
{
int j;
for(j=0;j<=z;j++)
}

but i dont know how to make exact delay like 1ms,1 sec, 1 minute , etc...
pls help me.......

:|
 

actually am new in pic programming.......i downloaded the file posted above. BUT there are many files like
always.h
delay.c
delay.h
delay.pjt
main.c etc
.
.
Actually am confused , what to du with those files.....pls explain
 

depending on your definition of "exact", the right answer ranges from "impossible" to "too easy".
 

vinodstanur,

you can find "readme.txt" file there. It has an explanation.

Also, you will find "main.c" file. It is an example program that using these routines.
 

A new built-in inline function '_delay' (request a delay in instruction cycles)
had been offered by HI-TECH C for PIC10/12/16 v9.60+ PRO mode.
EX:
Code:
#include <htc.h>
#define _XTAL_FREQ 4e6 // 4MHz 
void main( void )
{
   _delay( 1 ); // 1 instruction cycle delay
   __delay_us( 2 ); // 2 us delay
   __delay_ms( 3 ); // 3 ms delay
}
To read the manual for details.
 
A new built-in inline function '_delay' had been offered by HI-TECH C for PIC10/12/16 v9.60+ PRO mode
Impressive, how they finally catch up with the competition :)
 

It has been in HiTech for a long long time, people just failed to find it or were too busy looking for a free delay routine. so has been the inline option.
 

you can easily defeat that delay routine, or pretty much any delay routine in a mcu.

the "appropriate" solution really depends on how "exact" you want it to be.
 

Code:

#include <htc.h>
#define _XTAL_FREQ 4e6 // 4MHz
void main( void )
{
_delay( 1 ); // 1 instruction cycle delay
__delay_us( 2 ); // 2 us delay
__delay_ms( 3 ); // 3 ms delay
}
These routines are not working ...please help!
 

Works fine for me!!!

Code:
// hi-tech c compiler for PIC12F629
#include <htc.h>	// Required to interface with delay routines
#ifndef _XTAL_FREQ
 // Unless already defined assume 4MHz system frequency
 // This definition is required to calibrate __delay_us() and __delay_ms()
 #define _XTAL_FREQ 4000000
#endif
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF);
void main(void){
	while(1)
	{
		GPIO=0;
		__delay_ms(500);
		GPIO=0xFF;
		__delay_ms(500);
	}
}
 

Code:
#include<htc.h>
#define _16F877
__CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT );

#define	XTAL_FREQ	12MHZ
void main()
{
TRISB=0x00;
TRISD=0x00;
while(1)
{
PORTB=0xff;
PORTD=0xff;
__delay_ms( 3 );
PORTB=0x00;
PORTD=0x00;
__delay_ms( 3 );
}
}

This my code and am using MPLAB IDE v8.60 with HI-TECH compiler 9.60.
I have type the delay routines as you said but the compiler gave the following errors
Error [192] E:\led.c; 14.29 undefined identifier "_XTAL_FREQ"

********** Build failed! **********

please help...
 

Try use like my example:

#include
#define xtal
Fuses...

Open the examples folder from your hitech compiler... Search for delay.c...
 

try this

Code:
#define    XTAL_FREQ 12000000  // 12 MHZ

or 
#define    XTAL_FREQ 12
or
#define PIC_CLK 12
 

Code:
#define	XTAL_FREQ	12MHZ

This my code and am using MPLAB IDE v8.60 with HI-TECH compiler 9.60.
I have type the delay routines as you said but the compiler gave the following errors

Error [192] E:\led.c; 14.29 undefined identifier "_XTAL_FREQ"

********** Build failed! **********

I think the problem is quite obvious.

The error mess says "_XTAL_FREQ" not defined.

But in your program you defined "XTAL_FREQ"

You left out "_" before XTAL.

And I don't think the compiler would accept 12MHZ too. Try 12000000 instead.

Allen
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top