capturing signal and outputing it


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2009
    Posts
    63

    Default capturing signal and outputing it

    hi, im having a pretty stupid problem. using a 18f2431

    i currently have my pic capturing a signal, it measures the period of the signal. the signal im inputting is at 19.45/64 khz so it has a period of about 3280 microseconds. i then use this for a duty for the output (the period will affect my pulsewidth in this project). it gives me an output, and the pulse width is about 1/10 of the period. my output signal which is at about 20khz.

    my question is.. when i measure the period is it measuring in microseconds? or how is it measured because im quite confused and the data sheet doesnt seem to enlighten me...

    this is my code:

    Code:
    define osc 20
    
    T1 VAR word
    
    Capture VAR PIR3.1
    False CON 0
    True  CON 1
    
    ' auto time base reset, frequency mode, capture on every rising edge
        Frequency  CON %01000101
      
        ANSEL0=0             ' All digital
        TRISA.2 = 1          ' this is your (CAP1) input pin measuring the frequency
        INTCON = 0           ' Interrupts off
        TMR5H = 0            ' Clear high byte of TMR5 counter
        TMR5L = 0            ' Clear low byte
        T5CON = %00000001    ' prescale=1:1, int clock, TMR5=on
        CAP1CON = Frequency  ' we're measuring a frequency
      
    
            Duty Var word
                                      'byte being the one that is answer of algorithm when thats created
       
        PORTB = 0               ' clear port latch
        TRISB = %11000000       ' PWM0,1,2,3,4,5 outputs
        
        TRISC = 2               ' RC1 = FLTA input (ground RC1 to halt PWM)
                                ' RC1 should be pulled high for normal PWM operation
                                ' when fault A is enabled.
        ' PCPWM init
        DTCON = %00000101       ' ~500nS dead-time (for complementary outputs only)
        PTCON0 = %00000000      ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
                                ' PTCON0 = %00000100 would give 19.45kHz/4
        PTPERL = 0              ' 
        PTPERH = 1              ' PTPER = $0100 or 256d for ~19.45kHz
    	
        ' PWM4,5 independent, PWM0,1,2,3 complementary
        PWMCON0 = %01010100     ' PWM[5:0] outputs enabled
        PWMCON1 = 1             ' updates enabled, overrides sync w/timebase
        PTCON1 = %10000000      ' PWM time base is ON, counts up
        FLTCONFIG = %00000011   ' enable fault A, cycle-by-cycle mode
    
    Main: 
        Capture = False       ' Reset capture flag
        GOSUB Freq            ' get frequency
        
    
    
    
        Duty = T1          ' 
        PDC2L = Duty.LowByte    ' maintain a fixed 50% duty cycle on PWM4,5
        PDC2H = Duty.HighByte   ' independent PWM outputs.
        
        GOTO Main
    
    Freq:    
        ' Frequency measurement mode, capture Timer5 count every rising edge
        ' with auto reset of Timer5 on each event
        WHILE Capture = False
        WEND
        T1.HighByte = CAP1BUFH
        T1.LowByte = CAP1BUFL
        RETURN
    
        END

  2. #2
    Join Date
    Aug 2009
    Posts
    63


    Did you find this post helpful? Yes | No

    Default

    just managed to get it to work with 3phases and to average them and output them... but im still unsure on the actual values. when a period is measured what does it get measured in? and how do i relate that to pwm?

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Cool

    Hi,

    A closer look at your PIC Datasheet will show you HOW the CCP modules work ...

    and from that ... What's the counting unit ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Well the timer measuring the period is being clocked by something, it apperars you're using the main oscillator as the source for that. Then there's a prescaler that can divide the main clock but it appears you have that set to 1:1. So if the PIC runs at 20MHz one timer "tick" is 200nS.

    If the timer value is captured (and then reset) on the rising edge and you get a timer value of 1234 then the persiod is 1234*200nS = 246.8uS.

    /Henrik.

  5. #5
    Join Date
    Aug 2009
    Posts
    63


    Did you find this post helpful? Yes | No

    Default

    hey thanks!

    i had just worked that all out before i got the reply! but its just confirmed my calculations which is good. 0.2us is one timer tick so i know how to scale it all now.

    i am having another problem if anyone can shed some light on it? im getting the right pulsewidth and the right frequency output (inrespect to my input) however its glitching every now and then and isnt giving me a perfectly smooth pwm output.

    its glitching is not coinciding with tiny fluctuations in the input signal either...

    could it be due to over measuring?

    thanks!

  6. #6
    Join Date
    Oct 2004
    Location
    North Norfolk UK
    Posts
    146


    Did you find this post helpful? Yes | No

    Default

    Have you considered using the HWPM facilty?

    Duncan


    ......

  7. #7
    Join Date
    Aug 2009
    Posts
    63


    Did you find this post helpful? Yes | No

    Default

    duncan303 Have you considered using the HWPM facilty?

    Duncan


    ......
    i tried implementing the hpwm facility but it didnt seem to work for me very well... i cant remember what the problem was now but there was a reason i didnt use it. my pwm code i know works fine... ive got it working beautifully smooth from another pic... but when i add the code to capture and then output a signal that has nothing to do with the input signal it has random flickerings on the output...

  8. #8
    Join Date
    Aug 2009
    Posts
    63


    Did you find this post helpful? Yes | No

    Default

    update:

    i have got it working pretty nicely now, although i have added a 5 second pause to slow down updates.

    there is very rarely a flickering now, which leads me to believe the highly frequent flickering before was caused by capturing and updating to quickly... why would this be tho?

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