CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how to code 25ns delay in ccs

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
syamin0712



Joined: 23 Aug 2015
Posts: 14

View user's profile Send private message

how to code 25ns delay in ccs
PostPosted: Sat Jan 30, 2016 8:33 pm     Reply with quote

Hi,

Does anybody know how to code the delay of 25ns each in ccs? So that, I can get the 20MHz for 1 cycle.
I use the crystal 10MHz and use HSPLL so that I can get 40MHz clock.
I have tried to use the fraction, but when I burn the code into PIC18F4580 and test in real probe...not pulse appeared. But, when I change into low frequency for example 500Hz or 50kHz..it was successfully appeared in the probe.

This is my code:
Code:
/// 20MHz pulse

#include <18F4580.h>
#fuses H4,NOWDT,NOLVP,BROWNOUT,PUT,NOPBADEN
#use delay(clock=40000000)

void main ()
{

while (TRUE)
{

output_high(PIN_B1);

delay_us(1/40);   //set 25ns each

output_low(PIN_B1);

delay_us(1/40);  //set 25ns each
   
}

}



Is the code I used correct? Please help.
temtronic



Joined: 01 Jul 2010
Posts: 9171
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 30, 2016 9:52 pm     Reply with quote

re:

delay_us(1/40); //set 25ns each

You need to read the CCS C manual !
Press f11 when your project is open and read the section on
delay_us()

knowledge is power

jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19363

View user's profile Send private message

PostPosted: Sun Jan 31, 2016 2:10 am     Reply with quote

None of the delay instructions can accept fractional delays. Int16 values for delay_us.

The shortest delay you can code, is delay_cycles(1);
Note 'cycles', not 'us'.
This gives a single machine instruction delay.

The processor at 40MHz clock, is executing 10mips. 100nSec/instruction. You cannot do anything faster than this. delay_cycles(1) gives 100nSec.
Then the output's each take an instruction, and the loop, two.

The processor cannot do a delay of 25nSec.
Even if you code immediately output_high, followed by output_low, there will be one machine cycle between the operations.

However you need to be using 'fast_io', or there will be two extra instructions inserted to control the TRIS.

Then the branch instruction to loop back, will take two instruction clocks, so the fastest possible for the processor is:
Code:

/// 2MHz pulse

#include <18F4580.h>
#fuses H4,NOWDT,NOLVP,BROWNOUT,PUT,NOPBADEN
#use delay(clock=40000000)
#use fast_io(B)

void main ()
{
   output_drive(PIN_B1); //need to set the TRIS to output
   while (TRUE)
   {
       output_high(PIN_B1);
       delay_cycles(1); //single machine cycle
       output_low(PIN_B1);
   } //the branch back will take 2 machine cycles
}

This will give 5 instruction times per loop (one output, one cycle delay, one output, then the loop back which takes two instruction times) _2Mhz_.
You could get rid of the delay and have 2.5Mhz with just 100nSec high.

This is the fastest the processor can manage.

You are an order of magnitude below performance to output 20MHz.

The hardware PWM, can generate clocks up to 1 instructions per clock (so 10MHz).

Understand that this is not a CCS 'limitation', it's just how fast the processor actually is. Even if you were using a processor that was executing 40mips, you'd not get 20MHz output. Instructions all take time.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Jan 31, 2016 8:52 am     Reply with quote

You could use the PWM to generate either 25ns high or 25ns low pulses.
As Mr T. has already said the maximum frequency will be 10MHz.

To get your 20MHz, 50% duty ratio signal, you'll need an 80MHz clock.

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9171
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 01, 2016 6:47 am     Reply with quote

Thinking 'outside the PIC' you could use a 'crystal clock module'. Just buy one of the correct frequency. Simple, cheap and it works.
We don't know your application..continous pulses or clocked?
Using the module, frees up the PIC (even a lower-end one) to do other tasks.....

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group