Measuring change of frequency with PIC


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    With Timer1 configured for 16-bit read/write, you need to write to TRM1H
    first, then write to TMR1L.

    So change;
    TMR1L = 0 ' clear TMR1 low
    TMR1H = 0 ' clear TMR1 high

    To;
    TMR1H = 0 ' writes to TMR1H buffer
    TMR1L = 0 ' writes TMR1H 'buffer' to TMR1H register & writes to TMR1L
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    If RC2 is avilable you could also use the hardware capture module.
    Code:
    ' Measuring pulse width with capture module
    ' Input signal connected to RC2/CCP1 pin
    
        DEFINE OSC 4
       
        Symbol Capture = PIR1.2 ' CCP1 capture flag
        T1 VAR WORD          ' 1st capture value
        T2 VAR WORD          ' 2nd capture value
        T3 VAR WORD          ' 3rd capture value
        LowCycle VAR WORD    ' low pulse width rsult
        HighCycle VAR WORD   ' high pulse width result
        
        TRISC.2 = 1          ' CCP1 input pin (Capture input)
        INTCON = 0           ' Interrupts off
        CCP1CON = %00000101  ' Capture mode, capture on rising edge
        T1CON = %10000001    ' TMR1 prescale=1, clock=Fosc/4, TMR1=on
     
    Main:
        WHILE !Capture      ' wait for rising edge capture
        WEND
        ' Rising edge detected so record timer1 value
        T1.HighByte = CCPR1H : T1.LowByte = CCPR1L
        
        Capture = 0         ' Clear capture flag bit
        CCP1CON.0 = 0       ' Configure capture for falling edge now
        
        While !Capture      ' While here for capture on falling edge
        Wend
        ' Falling edge detected so record timer1 value again
        T2.HighByte = CCPR1H : T2.LowByte = CCPR1L
        
        Capture = 0         ' Clear capture flag bit
        CCP1CON.0 = 1       ' Configure capture for rising edge now
        
        While !Capture      ' While here for capture on falling edge
        Wend
            
        ' Falling edge detected so record timer1 value again
        T3.HighByte = CCPR1H : T3.LowByte = CCPR1L
        
        CCP1CON = 0         ' disable capture during data processing
        
        HighCycle = T2-T1   ' High pulse width = T2 - T1
        LowCycle = T3-T2    ' Low pulse width = T3 - T2
          
        HSEROUT ["High pulse width = ",DEC HighCycle,"uS",13,10]
        HSEROUT ["Low pulse width = ",DEC LowCycle,"uS",13,10]
        HSEROUT ["Period = ",DEC LowCycle+HighCycle,"uS",13,10]
        
        Capture = 0          ' Clear capture flag bit
        CCP1CON = %00000101  ' Capture mode, capture on rising edge
        
        GOTO Main
    
        END
    If the measured frequency is too slow you can use the timer prescaler. You
    want the frequency to be high enough so the timer doesn't overflow during
    the measurement period.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    May 2007
    Location
    Melbourne
    Posts
    3


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by Bruce View Post
    With Timer1 configured for 16-bit read/write, you need to write to TRM1H
    first, then write to TMR1L.

    So change;
    TMR1L = 0 ' clear TMR1 low
    TMR1H = 0 ' clear TMR1 high

    To;
    TMR1H = 0 ' writes to TMR1H buffer
    TMR1L = 0 ' writes TMR1H 'buffer' to TMR1H register & writes to TMR1L
    Thanks a lot Bruce,
    I changed only that thing in my program and it started working .
    Have never seen in the manual that such a sequence of work with the timers should be...

Similar Threads

  1. TSA5512/5511 code for PIC16F877/84
    By Marin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th August 2013, 06:16
  2. newbe looking for PIC advice
    By Mad Professor in forum General
    Replies: 3
    Last Post: - 27th May 2009, 07:56
  3. Automatic VB6 to pic serial connection
    By arniepj in forum Code Examples
    Replies: 13
    Last Post: - 10th January 2008, 07:57
  4. Replies: 2
    Last Post: - 10th February 2006, 01:04
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

Members who have read this thread : 0

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