Working PIC with Frequencies


Closed Thread
Results 1 to 7 of 7
  1. #1

    Thumbs up Working PIC with Frequencies

    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.
    Code:
    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!
    Last edited by Archangel; - 7th June 2011 at 07:25. Reason: added code tags

  2. #2
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default Re: Working PIC with Frequencies

    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.

  3. #3


    Did you find this post helpful? Yes | No

    Thumbs up Re: Working PIC with Frequencies

    Quote Originally Posted by dhouston View Post
    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!

  4. #4
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default Re: Working PIC with Frequencies

    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.
    Last edited by dhouston; - 30th March 2011 at 00:44. Reason: added link

  5. #5
    Join Date
    Oct 2005
    Location
    Loveland CO USA
    Posts
    83


    Did you find this post helpful? Yes | No

    Default Re: Working PIC with Frequencies

    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
    Last edited by Archangel; - 7th June 2011 at 07:21. Reason: fix quote tag

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Working PIC with Frequencies

    Quote Originally Posted by ronsimpson View Post
    [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?
    Last edited by Archangel; - 7th June 2011 at 07:23. Reason: fix quote tag

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Working PIC with Frequencies

    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?

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts