 |
 |
View previous topic :: View next topic |
Author |
Message |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun May 26, 2013 7:17 am |
|
|
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
|
|
Posted: Sun May 26, 2013 10:25 am |
|
|
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!  |
|
 |
dyeatman
Joined: 06 Sep 2003 Posts: 1950 Location: Norman, OK
|
|
Posted: Sun May 26, 2013 12:22 pm |
|
|
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
|
|
Posted: Sun May 26, 2013 1:05 pm |
|
|
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
|
|
Posted: Tue May 28, 2013 9:41 am |
|
|
dyeatman, Mike!
Thank you very much!
I try to do my program. I hope you will succeed. Wish me luck!  |
|
 |
|
|
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
|