Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 -
while(1)
      {
       
 
       sqwv1=1; //ON Time
       sqwv1=1; 
 
       sqwv1=0; //OFF Time
   
 
      }
Posted 7-Dec-12 21:28pm

1 solution

Rate this: bad
good
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 (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!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2014
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