PDA

View Full Version : Working PIC with Frequencies



camilollanom
- 29th March 2011, 00:52
Hi there.

I know there's a way to work frequencies with a PIC 16f877a, my teacher told me It could work using the internal timer that the PIC has. It doesn't work with frequencies as such, but with pulses. The problem is that I don't know how to get a pulse value from a random frequency, but I'm going to share a code where someone put some pulse values for a frequency.


void main()
{

// VARIABLES

//int16 pulse;
int16 pulse;
int n = 0;
int x = 1;

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// setup_oscillator(False);

x=1;
output_b(0);
while(n < 14){delay_ms(150);output_toggle(PIN_B0);n++;}

while(TRUE)
{
// Detection of pulse length

while(input_state(input));
while(!input_state(input));
output_b(0);

if(input_state(input))
{
set_timer1(0);
while(input_state(input));
pulso = get_timer1();
printf("%Lu \n\r",pulse);
}

if ( pulse > 1450 && pulse < 1562 ) {
output_high(PIN_B0); //This works when I'm giving a frequency of 82.64 Hz
}
else if ( pulse > 1094 && pulse < 1194 ) {
output_high(PIN_B1); //This works when I'm giving a frequency of 109.2 Hz
}
else if ( pulse > 800 && pulse < 900 ) {
output_high(PIN_B2); //This works when I'm giving a frequency of 147.1 Hz
}
}
}

So if there's someone who could help me how to find a way to become frequency values into pulse values that I could use.

Thank you!

dhouston
- 29th March 2011, 01:55
period = time for one cycle
f = frequency in hertz (i.e. cycles per second)
period = 1/f

Assuming a 50% duty cycle, the time for a single pulse = 1/2 period

What you need to do is convert the pulse counts into times and then apply the above formula. The counts will vary with clock frequency and any prescaler used.

camilollanom
- 29th March 2011, 23:51
period = time for one cycle
f = frequency in hertz (i.e. cycles per second)
period = 1/f

Assuming a 50% duty cycle, the time for a single pulse = 1/2 period

What you need to do is convert the pulse counts into times and then apply the above formula. The counts will vary with clock frequency and any prescaler used.

Thank you for your reply.

But I can't understand it very well, for example what happens if I have a PIC 16F877A working with a clock of 20MHz. Do I have to see the relationship between my frequency, for example 82.64Hz, and the clock of 20MHz and then work with your formula?
It would be really great if you could help me with an example, I'll be really thankful!

dhouston
- 30th March 2011, 00:32
If you search the forums you'll find code from Bruce Reynolds that uses the Capture/Compare register to measure the period between adjacent rising edges (i.e. one cycle and independent of the duty cycle).

The code you posted is C while this forum is dedicated to PicBasic and PicBasiPro. If your class works in C, then you should be asking this in some forum dealing with using C to program PICs.

Here's a link to Bruce's code to capture a pulse (not one complete cycle) which is closest to your example. It is well commented.http://www.picbasic.co.uk/forum/showthread.php?p=23401

ronsimpson
- 30th March 2011, 00:38
But I can't understand it very well, for example what happens if I have a PIC 16F877A working with a clock of 20MHz. Do I have to see the relationship between my frequency, for example 82.64Hz, and the clock of 20MHz and then work with your formula?
It would be really great if you could help me with an example, I'll be really thankful!

If clock=20mhz and you counter is running at 5mhz then the counter is counting in 0.2uS steps. If the counter=1 then 0.2uS. If the counter=10 then 2uS. 100=20uS 1/20uS=(50khz)

If the frequency you want to look at is 82.64hz then the counter will read 60503. There are 60503 * 0.2uS in 82.64hz.

5,000,000hz/82.64hz=60503

Time=0.2uS X timer-count
F=1/Time

Your counter can not count frequency. It can count time! and F=1/T

camilollanom
- 30th March 2011, 20:32
[But I can't understand it very well, for example what happens if I have a PIC 16F877A working with a clock of 20MHz. Do I have to see the relationship between my frequency, for example 82.64Hz, and the clock of 20MHz and then work with your formula?
It would be really great if you could help me with an example, I'll be really thankful!

If clock=20mhz and you counter is running at 5mhz then the counter is counting in 0.2uS steps. If the counter=1 then 0.2uS. If the counter=10 then 2uS. 100=20uS 1/20uS=(50khz)

If the frequency you want to look at is 82.64hz then the counter will read 60503. There are 60503 * 0.2uS in 82.64hz.

5,000,000hz/82.64hz=60503

Time=0.2uS X timer-count
F=1/Time


Your counter can not count frequency. It can count time! and F=1/T

Thank you so much! that's easier. So now the question will be, What value should I use on my code? Timer-count values or just time values? My guess is Timer-Count values, Am I right?

camilollanom
- 5th April 2011, 17:04
In order to work frequencies with PICs, should I use as an Input of my source the OSC2/CLKOUT? There is where I put the external ocsilator of 20MHz, does it work as an input in order to work with pulses?