External Links

Creative Science Centre

 

Input Capture

// #include "http://www.byvac.com/mBlib/flb/Library/2016/lib_ic.bas"

This is a reasonably complex peripheral and so there are a number of parameters to set for initialisation.

icap.init(*ic(),*pin(),*timer(),pre,fedge,intr,mode)
Example: icap.int(*ICAP1(),*PRB6(),*TIMER2(),5,1,1,6)

Where:

*ic() Selects the channel

is a choice of *ICAP1(),*ICAP2(),*ICAP5(),*ICAP5(),*ICAP5(), These will need associating with a pin on the IC which is done next.

*pin() Selects the port an pin

The pin is specifies according to this table which is taken from the pps library

  Peripheral Pin
Inputs INT4,T2CK,IC4,SS1, PRA0  PRB3  PRB4  RB15  RB7  RRC7  RRC0  RRC5 
   
Inputs INT3,T3CK, IC3,U1CTS,U2RX,SDI1 PRA1  PRB5  PRB1 RRB11 RRB8 RRA8 RRC8 RRA9
   
Inputs INT2,T4CK,IC1,IC5,U1RX,U2CTS,SDI2,OCFB PRA2 PRB6 PRA4 RB13 RB2 PRC6 PRC1 PRC3
   
Inputs INT1,T5CK,IC2,SS2,OCFA PRA3 PRB14 PRB0 PRB10 PRB9 PRC9 PRC2 PRC4
   

Only the port/pins on the same row can be used so for example IC4 (ICAP4()) can only be associated with PRA0,PRB3,PRB4,PRB15,PRC7,PRC0 and PRC5.

*timer() Choose timer

This can only be TIMER2(), TIMER3() or TIMER23 (for 32 bit)

pre is the prescale for the timer values 0 to 7. For further information see the timer library.

fedge First edge to capture 0 or 1

0 will capture the first falling
1 will capture the first rising edge

intr If interrupts will be used 0 to 3

0 will set an interrupt flag on every capture event
1 will set an interrupt flag on every second capture event
2 will set an interrupt flag on every third capture event
3 will set an interrupt flag on every forth capture event

mode Sets capture events 0 to 7

0 input capture module is disabled
1 Edge detect mode, every rising and falling edge
2 Simple capture event mode - every falling edge
3 Simple capture event mode - every rising edge
4 Prescaled capture event - every forth rising edge
5 Prescaled capture event - every sixteenth rising edge
6 Simple capture event - every edge, specified by first edge and every edge thereafter
7 Interrupt only - onlys supported in sleep mode

  • icap_in(*ic()) // reads the input buffer
  • icap_in?(*ic()) // returns none zero if buffer is not empty, cleared by reading buffer
  • icap_enable(*ic()) // enables peripheral - also icap_init() does same unless mode set to 0
  • icap_disabe(*ic()) // disables peripheral
  • icap.intr(*ic(),v) // changes interrupt as intr above, v = 0 to 3

*ic() is one of *ICAP1() to ICAP5()

For a practical example of how to use input capture see the measuring distance with the HR04 in the library. Or a more complex example IR Remote.