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

Internal Prescaler EX_FREQ.C
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mike Walne



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

View user's profile Send private message

PostPosted: Sun May 26, 2013 7:17 am     Reply with quote

Jhonny wrote:
Dear dyeatman!

Thanx comment, but:
1., Mike uses 16MHz, but I use 20MHz
2., As I write now, I'm just a "EX_FREQ.C" code and I want to modify. I do not want another (new) code.
The rest is all true what you wrote. (The code is hard even for me to understand because I am an absolute beginner)


I've even explained the maths in the pre-amble, including what each of the numbers 16, 250, 4 etc. are for.
It's not rocket science to modify for any crystal, gate period or PIC.
(And certainly simpler than the CCS version.)

Code:
/////////////////////////////////////////////////////////////////////////
////                            100ms Gate                           ////
/////////////////////////////////////////////////////////////////////////
// External source connected to T1 clock input
// T2 used as gate timer
// T2 timer set for /16 /250 /4 -> overflows every 4ms
// 25 overflows -> 100ms gate time
// T1 enabled when gating_count is 10 and disabled on 35 (25 later)
// Takes 3 extra cycles to reach turn off, so padded at turn on
// 16MHz clock -> 250ns instruction time
#define   START 10
#define STOP  35


Like dyeatman says,

What more do you want?

Mike
Jhonny



Joined: 30 Jan 2011
Posts: 16

View user's profile Send private message

PostPosted: Sun May 26, 2013 10:25 am     Reply with quote

Okay, thank you!
You have a few questions:
In your code, how do I spit out the frequency? The while () loop? What gives it its value? Could complement your code here for me? What is the maximum measurable frequency? The input is still the C0, - right?
Sorry to ask so much, but I really want to understand! Embarassed
dyeatman



Joined: 06 Sep 2003
Posts: 1914
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun May 26, 2013 12:22 pm     Reply with quote

I am glad you want to learn!

The Timer_1 external input is the input to the freq ctr. Use the datasheet
to determine which pin that is.

The count in Timer 1 will be the indicator of frequency. The commented
lines will give you some direction.

Define a global variable called display_freq. This variable being non-zero
is a "flag" to signal when a count has been captured and needs to be
converted/calculated and displayed.

Code:
void main()
{
      setup_timer_2(T2_DIV_BY_16,249,4);
      enable_interrupts(INT_TIMER2);
      enable_interrupts(GLOBAL);
      gating_count = 0;
      Display_Freq = 0; // **** set the display Freq flag to zero or false
      while (TRUE)
           {
             if (Display_freq > 0) // **** If the Display flag is non-zero then display the latest count
                   {
                     Get_Timer1 count and calculate the frequency
                     Use printf to display the calculated frequency.
                     set Display_Freq flag to zero
                    }
           output_high(PIN_B1);
           output_low(PIN_B1);   //Toggle B1 to show we made a pass through the routine
          }
}

#INT_TIMER2
void gating_isr()
{
   gating_count++;
   if (gating_count == START)
   {
      delay_cycles(3);         // pads out timing difference
       setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
      output_high(PIN_B0);      // something to see on a 'scope
   }
   if (gating_count == STOP)
   {   
      setup_timer_1(T1_DISABLED);   //turn off counter
      output_low(PIN_B0);         // can see on scope
      gating_count = 0;         // get ready to start again
      Display_freq=1; // **** signal the value is ready to be calculated and displayed.
   }
}

_________________
Google and Forum Search are some of your best tools!!!!
Mike Walne



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

View user's profile Send private message

PostPosted: Sun May 26, 2013 1:05 pm     Reply with quote

Quote:
In your code, how do I spit out the frequency? The while () loop? What gives it its value? Could complement your code here for me? What is the maximum measurable frequency? The input is still the C0, - right?
What we're telling you is:-

1) If you're going to learn, you have to work it out for yourself.
2) We (dyeatman & I) have done the donkey work for you.
3) I believe the maximum frequency is dependant on your chosen PIC.
4) The data sheets have all the answers.

I've outlined earlier in this post the limitations of the technique.
The PIC and crystal frequency will determine the ultimate performance.
This was never my project, so I'll leave the rest to you.
I earned my degree ~half a century ago, I don't need another.

Mike
Jhonny



Joined: 30 Jan 2011
Posts: 16

View user's profile Send private message

PostPosted: Tue May 28, 2013 9:41 am     Reply with quote

dyeatman, Mike!
Thank you very much!
I try to do my program. I hope you will succeed. Wish me luck! Wink
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
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