Click here to Skip to main content
13,138,881 members (53,459 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
I want to generate 2MHz on C8051f005.
With internal 16MHz system clock, I able to generate 1.921MHz
now i am trying with external 25Mhz system clock, but frequency goes on decreasing
to 240khz.

Please help on this matter : If Possible with codes, snippets and guidelines for how to get the frequency range.


Code which i am using -
       sqwv1=1; //ON Time
       sqwv1=0; //OFF Time
Posted 7-Dec-12 20:28pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Two things: firstly, if you want to generate a true, absolute value frequency square wave in code, they don't do it in C: the compiler does not know what you are trying to achieve, and throws in code which will generally sabotage the effort. In addition, any future update to the compiler may produce different code, which could drastically change the output waveform. Use assembler instead.

Secondly, I've not used a C8051f005 so I don't know how it's clock speed relates to execution time, but pretty much unless the process executes all instructions in the same number of clock pulses, and that number is pretty small, you are unlikely to get a 2MHz clock out of a 16Mhz processor - even if it executes instructions in 1 code period, you still only have eight instructions to generate a good square wave - you might get away with a skewed wave, but even then it could be tight.

The frequency slowdown with a faster processor is unlikely unless there are problems: it could be a scope trace artefact (try changing the timebase and see if what you are measuring is actually a harmonic - digital scopes are prone to this) or it could be that you are overclocking the processor and it just can't cope - without knowing a lot more about your hardware I can't tell.

A quick look at the C8051f005 specs ([^]) shows that you have timer drives to your output ports - use one of them instead! They will generate a clean, square waveform with much better accuracy than you will in code, and require no processing overhead to achieve!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100