ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - CCPX2.pbp

PICBASIC PRO program to demonstrate the use of the hardware capture module to measure the period of a frequency input. Written for the PIC16F877 and LAB-X2 Experimenter Board. Input signal should be connected to RC2/CCP1.
' Name        : CCPX2.pbp
' Compiler    : PICBASIC PRO Compiler 2.6
' Assembler   : PM or MPASM
' Target PIC  : PIC16F877
' Hardware    : LAB-X2 Experimenter Board
' Oscillator  : 4MHz external crystal
' Keywords    : CAPTURE, SEROUT, TIMER1
' Description : PICBASIC PRO program to demonstrate the use of the 
' hardware capture module to measure the period of a frequency input.
' Written for the PIC16F877 and LAB-X2 Experimenter Board.
' Input signal should be connected to RC2/CCP1.
'

Define LOADER_USED 1   ' Optional, allows use of boot-loader

Include "modedefs.bas" ' Modes for SerOut

So       VAR PORTC.6   ' Serial output pin
capture  VAR PIR1.2    ' CCP1 capture flag
overflow VAR PIR1.0    ' Timer1 overflow flag
period   VAR WORD      ' Word variable that stores the value

   CCP1CON = %00000100 ' Enable the CCP1 capture, falling edge
   T1CON = %00000001   ' TMR1 prescale=1, and turn it on (1uS per count)

mainloop:
   IF (capture = 0) Then mainloop ' Wait here until captured 

   period.lowbyte = CCPR1L   ' Store the captured value in
   period.highbyte = CCPR1H  ' period variable
        
   IF overflow = 0 Then      ' Skip the output if the timer overflowed
      SerOut So, T2400, [ "Input Period: ",#period, "uS", 10,13] ' Output
   EndIF
   capture = 0   ' Clear the capture flag
        
reset:
   IF (capture = 0) Then reset ' Wait for beginning of next period
   TMR1L = 0     ' Clear Timer1 low register
   TMR1H = 0     ' Clear Timer1 high register
   capture = 0   ' Clear capture flag
   overflow = 0  ' Clear overflow flag
   GoTo mainloop ' Do it forever
        
   End

           

Download the program file.