Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C Programming: Clock Pulse Generation

Status
Not open for further replies.

km

New Member
How to generate clock pulse let's say 1MHz using C?

Eg:
high --> delay(?) --> low --> delay(?)

How much delay should i give?
 
km said:
How to generate clock pulse let's say 1MHz using C?

Eg:
high --> delay(?) --> low --> delay(?)

How much delay should i give?

500nS - but you're seriously going to struggle getting 1MHz from a software loop, particularly if using C.
 
km said:
Why you said so? Is there other better choice?

Assembler is the obvious choice, as it's the fastest possible coding, but 1MHz is still pushing a micro-controller - although an 18F PIC runing at 40MHz would be able to do it.

What processor are you using?, and why are you trying to generate 1MHz?.
 
Clock Pulse Generation

You don't mention which processor you are using. If it has (and most do) hardware timers on-chip, use those and do the timing loop in hardware instead. This will be far more precise and use only a fraction of your processors available cycles. You will have to develop an interrupt service routine to respond to the timer interrupt. Depending upon your vendor, they may have prepackaged routines already available for you. Also, check their web site and databook for guidance and app notes.
 
Mike Stanley said:
You don't mention which processor you are using. If it has (and most do) hardware timers on-chip, use those and do the timing loop in hardware instead. This will be far more precise and use only a fraction of your processors available cycles.

It certainly true that it will use less cycles, but no way is it more precise!, with software delay routines you can adjust the routine by a single cycle, with hardware timers you are limited to the fixed intervals the timer can provide.

As for using a hardware timer to generate the delay for 1MHz, the interrupt latency is likely to be far longer than the delay you're trying to produce - unless you're talking a seriously fast device?.

But as you rightly say, we've never been informed what processor he's thinking of using, nor what he's trying to do (which might be completely non-practical anyway?).
 
Nigel, You're right about the interrupt latency. As for "fixed intervals" provided by the timer, that depends on the timer. Those I've used can resolve down to a single clock cycle - far better than any software routine.
Depending upon the timer and whether or not he needs a single pulse or a regular waveform, he might just be able to program the timer and let it rip (with no ISR required). But again, all this depends on the hardware he has available. The best advice? Read the databook.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top