pulse generatng in PIC

Discussion in 'Embedded Systems and Microcontrollers' started by fancy102, Dec 21, 2007.

  1. fancy102

    Thread Starter Member

    Dec 21, 2007
    26
    0
    hi,
    i m facing defficulties in writing programme to calculate phase shift of a pulse in pic.

    my input to the PIC16F877A is pulse form.how to write the PIC programme at time=0 and time=t1. i been reading a lot of websie but i still not understand. really thanks if somebody might help.

    best regards
     
  2. BlackBox

    Member

    Apr 22, 2007
    20
    0
    Sorry, if you could be a bit more specific in your request it would be easier to help you :)

    Are you trying to read the time between a High-to-low and a Low-to-high transition?

    In that case I would suggest using one of the internal timers of the PIC and poll the input port.
     
  3. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    The PIC16F877A has a capture/compare module using timer 1. This provides a way to use the hardware to time the changes on the input pin. Polling the input would not be as accurate. Without more specific info we can't help much.
     
  4. fancy102

    Thread Starter Member

    Dec 21, 2007
    26
    0
    yaya..i would like to write from high to low transition. pic can capture the module by using timer1? what relate with my hardware? and sorry to ask bout the pooling input..if i use polling is it really not accurate?if so,what method should be use?

    actually i am doing power factor measurement system. my input of the PIC is from the analogue switch, which is in square wave. and my problem is how to write high to low transition of the time by using PIC. Output of my PIC is connected to the LCD display.

    i would like to attach my schematic draawing here...
    thanks..
     
  5. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    Read Section 8.1 (pg65) of the datasheet:

    http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf

    This explains the CCP1 module. When an event is detected it captures the TMR1 value. TMR1 needs to be running.

    Polling the pin means that you'd have to use the software check the value of the input at set intervals. This would consume a lot of processor resources especially if you need that interval to be very small. Using the CCP1 module the hardware automagically takes care of monitoring the input pin (and capturing the timer 1 value at the event). It also triggers an interrupt.
     
  6. fancy102

    Thread Starter Member

    Dec 21, 2007
    26
    0
    hi,

    i had write the programme based on the timer. i would like to measure my input which is in pulse form. my problem is i still can get my output. my LCD show nothing at all.

    my input of pulse form is 50Hz, so the period is 1/50=0.02 second. my programme is witten as below. can any body help on y there is still no output at all.thanks.

    ' Hardware configuration
    ' ======================
    DEFINE OSC 20
    DEFINE LOADER_USED 1
    DEFINE LCD_DREG PORTD
    DEFINE LCD_DBIT 4

    DEFINE LCD_RSREG PORTE
    DEFINE LCD_RSBIT 0

    DEFINE LCD_EREG PORTE
    DEFINE LCD_EBIT 1

    TRISD = %00000000
    TRISA= %00000000
    TRISC = 255 ' PORTC As input
    adcon1=%00000100
    CMCON = 7 ' Disable analog comparator
    T1CON = 0 ' TIMER1:
    ' Clock Internal
    ' Pre-scaller 1:1

    ' Hardware connection
    ' ===================
    SignalInput VAR PORTC.2

    ' Variables definition
    ' ===================
    T1ON VAR T1CON.0
    PulseHigh VAR WORD
    PulseLow VAR WORD
    degree VAR BYTE
    totalpulse VAR BYTE

    ' Software/Hardware initialisation
    ' ================================
    Clear
    GoSub ClearTimer

    Start:
    ' Waiting for falling edge
    ' ========================
    While SIGNALINPUT=0 : Wend
    While SIGNALINPUT=1 : Wend

    ' Measure low pulse
    ' =================
    T1ON=1 ' start Timer
    While SIGNALINPUT=0 : Wend ' wait for next rising edge
    T1ON=0 ' stop timer
    PULSElow.HIGHBYTE=TMR1H ' store results
    PULSElow.LOWBYTE=TMR1L '
    pulselow=pulselow / 5 ' 1 tick = 0.2 Usec @ 20MHZ
    ' Divide by 5 to have 1 uSec
    ' Resolution

    GoSub Cleartimer ' reset Timer1 register (TMR1L, TMR1H)

    ' Waiting for rising edge
    ' =======================
    While Signalinput=1 : Wend
    While signalinput=0 : Wend


    ' Measure high pulse
    ' ==================
    T1ON=1 ' Start timer
    While SIGNALINPUT=1 : Wend ' wait for next falling edge
    T1ON=0 ' Stop Timer
    PULSEhigh.HIGHBYTE=TMR1H ' Store result
    PULSEhigh.LOWBYTE=TMR1L '
    pulsehigh=pulsehigh / 5 ' match results to 1 uSec resolution
    GoSub Cleartimer ' reset Timer1 register (TMR1L, TMR1H)


    ' Send results to LCD
    ' =============================
    LCDOut $fe, 1
    LCDOut "pulse0=",DEC pulselow

    LCDOut $fe,$c0
    LCDOut "pulse1=",DEC pulsehigh

    totalpulse=pulsehigh+pulselow
    degree=pulsehigh/totalpulse*360

    LCDOut $fe, 1
    LCDOut "degree=",DEC degree
    Pause 500
    GoTo start


    ClearTimer:
    TMR1L=0
    TMR1H=0
    Return


    Is the problem relate to the frequensy?or the timer?i really at a lost as what to do. i duno what the problem is.really appreciate your kindness for helping..
     
  7. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    I'm not familiar with PIC programming, just re-posting his code how it's supposed to look (his original formatting)

    Code ( (Unknown Language)):
    1.     '    Hardware configuration
    2.     '    ======================
    3.          DEFINE OSC 20
    4.          DEFINE LOADER_USED 1
    5.          DEFINE LCD_DREG PORTD
    6.          DEFINE LCD_DBIT 4
    7.  
    8.          DEFINE LCD_RSREG PORTE
    9.          DEFINE LCD_RSBIT 0
    10.  
    11.          DEFINE LCD_EREG PORTE
    12.          DEFINE LCD_EBIT 1
    13.        
    14.          TRISD = %00000000      
    15.          TRISA=  %00000000
    16.          TRISC = 255            ' PORTC As input
    17.          adcon1=%00000100
    18.          CMCON = 7              ' Disable analog comparator      
    19.          T1CON = 0              ' TIMER1:
    20.                                 ' Clock Internal
    21.                                 ' Pre-scaller 1:1
    22.                      
    23.     '    Hardware connection
    24.     '    ===================
    25.          SignalInput       VAR PORTC.2
    26.              
    27.     '    Variables definition
    28.     '    ===================
    29.           T1ON              VAR T1CON.0
    30.          PulseHigh          VAR WORD
    31.          PulseLow           VAR WORD
    32.          degree             VAR BYTE
    33.          totalpulse         VAR BYTE
    34.          
    35.     '    Software/Hardware initialisation
    36.     '    ================================
    37.          Clear
    38.          GoSub ClearTimer
    39.    
    40. Start:
    41.     '    Waiting for falling edge
    42.     '    ========================
    43.          While SIGNALINPUT=0 : Wend
    44.          While SIGNALINPUT=1 : Wend
    45.  
    46.     '    Measure low pulse
    47.     '    =================  
    48.          T1ON=1                     ' start Timer
    49.          While SIGNALINPUT=0 : Wend ' wait for next rising edge
    50.          T1ON=0                     ' stop timer
    51.          PULSElow.HIGHBYTE=TMR1H    ' store results
    52.          PULSElow.LOWBYTE=TMR1L     '
    53.          pulselow=pulselow / 5      ' 1 tick = 0.2 Usec @ 20MHZ
    54.                                     ' Divide by 5 to have 1 uSec
    55.                                     ' Resolution
    56.          
    57.          GoSub Cleartimer           ' reset Timer1 register (TMR1L, TMR1H)
    58.  
    59.     '    Waiting for rising edge
    60.     '    =======================
    61.          While Signalinput=1 : Wend
    62.          While signalinput=0 : Wend
    63.    
    64.    
    65.     '    Measure high pulse
    66.     '    ==================  
    67.          T1ON=1                     ' Start timer
    68.          While SIGNALINPUT=1 : Wend ' wait for next falling edge
    69.          T1ON=0                     ' Stop Timer
    70.          PULSEhigh.HIGHBYTE=TMR1H   ' Store result
    71.          PULSEhigh.LOWBYTE=TMR1L    '
    72.          pulsehigh=pulsehigh / 5    ' match results to 1 uSec resolution
    73.          GoSub Cleartimer           ' reset Timer1 register (TMR1L, TMR1H)
    74.          
    75.  
    76.     '    Send results to  LCD
    77.     '    =============================      
    78.          LCDOut $fe, 1
    79.          LCDOut "pulse0=",DEC pulselow
    80.        
    81.          LCDOut $fe,$c0
    82.          LCDOut "pulse1=",DEC pulsehigh
    83.  
    84.          totalpulse=pulsehigh+pulselow
    85.          degree=pulsehigh/totalpulse*360
    86.  
    87.          LCDOut $fe, 1
    88.          LCDOut "degree=",DEC degree
    89.          Pause 500
    90.          GoTo start
    91.                  
    92.  
    93. ClearTimer:
    94.     TMR1L=0
    95.     TMR1H=0
    96.     Return
    97.  
    98.  
     
  8. fancy102

    Thread Starter Member

    Dec 21, 2007
    26
    0
    my frequensy is 50Hz and my input of pic change every time depends on the

    loading...

    input of pic is the phase shift between the volage and current, i am

    going to develope a power factor measurement system.

    so i using the ratio of time (0 and 1) to get my on time and off time. then the

    phase(degree) is measured based on the ratio;

    degree=time when 1/(time0+time1) *360

    and my problem now is y my lcd show no outout at all though i had connect

    all my hardware together.

    is it the problem of time calculating?thanks!
     
Loading...