Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Thanks


Code which i am using -
C#
while(1)
      {
       

       sqwv1=1; //ON Time
       sqwv1=1; 

       sqwv1=0; //OFF Time
   

      }
Posted

1 solution

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 (http://www.silabs.com/Support%20Documents/TechnicalDocs/C8051F005-Short.pdf[^]) 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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900