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.

Problem generating a 1us delay in 18F4550 program

Status
Not open for further replies.

wcgan

Member level 2
Joined
Nov 1, 2006
Messages
43
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,611
delay_us

Hi all,

I having a problem to generate a 1us delay in my program.

Details:
IC - 18F4550
Crystal - 4MHz
Compiler - C18

I am able to generate 1us by calling function "Delay1TCY()" 12 times. But I convert it to for..next to run 12 times, it will generate longer than 1us.

Is there any better way to generate microsec delay in C18?

Below is my configuration setting:


Regards,
Wee Chong
 

daviddlc

Full Member level 5
Joined
Feb 11, 2007
Messages
260
Helped
35
Reputation
70
Reaction score
9
Trophy points
1,298
Location
USA
Activity points
2,709
c18 delay_us

Of course the "for" function is also consuming time, and it's added to the final delay.

Get the information on how much time will the "for" function consume and the make your new calculation on the number of loops.

Other method is to use the Timer to generate the delay.

DLC.
 

wcgan

Member level 2
Joined
Nov 1, 2006
Messages
43
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,611
how to generate delay in 18f4620

I have changed my Crystal to 20MHz.

C Compiler: MCC 18

below is my configuration settings:
Address Value
300000 - 0x24
==============
96MHz PLL Prescaler : Divide by 5 (20Mhz Input)
CPU System Clock Postscaler : [OSC1/OSC2 Src: /1][96MHz PLL Src: /2]
Full-speed USB Clock source selection : Clock src from 96MHz PLL/2

Address Value
300001 - 0x0E //HS: HS+PLL, USB-HS
==============

I am try to use the delay sample code below, but it cannot get 1us.

Code:
#define d_mhz 20000000
void delay_us(int usec);
int d_tmp;              //delay calculation temp
int lt,lt2;             //loop temp

// Initialize variables & Hardware

void delay_us(int usec)
{
     d_tmp = (d_mhz / 4) / 1000000;

     for(lt=0;lt<usec;lt++) {
          for(lt2=0;lt2<d_tmp;lt2++)
              Delay1TCY();
     }
}

Anyone can provide me a sample of delay us by using 20MHz?
Thanks a lot.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top