Here's an example of measuring a 32kHz signal on RA2 with the Motion Feedback Capture. It's pretty simple to use.
Code:
DEFINE OSC 4
DEFINE DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true
T1 VAR WORD
T2 VAR WORD
T3 VAR WORD
Capture VAR PIR3.1
False CON 0
True CON 1
' auto time base reset, capture on rising to falling edge
RiseToFall CON %01000111
' auto time base reset, capture on falling to rising edge
FallToRise CON %01000110
' auto time base reset, frequency mode, capture on every rising edge
Frequency CON %01000101
ANSEL0 = 0 ' All digital
TRISA.2 = 1 ' Cap1 input pin (Capture input)
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
GOTO Main
Trash: ' Trash 1st capture after capture mode changes
Capture = False ' Reset capture flag
WHILE Capture = False ' Wait for capture
WEND '
Capture = False ' Reset capture flag
RETURN ' Return
Main:
GOSUB LtoH
GOSUB HtoL
GOSUB Freq
PAUSE 500
GOTO Main
LtoH:
' Capture on falling to rising edge
CAP1CON = FallToRise
GOSUB Trash
WHILE Capture = False
WEND
T1.HighByte = CAP1BUFH ' high byte
T1.LowByte = CAP1BUFL ' low byte
DEBUG "Low PW = ",DEC T1,"uS",13,10
RETURN
HtoL:
' Capture on rising to falling edge
CAP1CON = RiseToFall
GOSUB Trash
WHILE Capture = False
WEND
T2.HighByte = CAP1BUFH
T2.LowByte = CAP1BUFL
DEBUG "High PW = ",DEC T2,"uS",13,10
RETURN
Freq:
' Frequency measurement mode, capture every rising edge
' to measure period
CAP1CON = Frequency
GOSUB Trash
WHILE Capture = False
WEND
T3.HighByte = CAP1BUFH
T3.LowByte = CAP1BUFL
DEBUG "Freq = 1/",DEC T3,"uS",13,10
RETURN
END
Returns;
High PW = 17uS
Low PW = 14uS
Freq = 1/31uS
This 18F series has a boat-load of nifty features. Check out how fast the A/D
is. Should be a killer PIC for robotics applications.
Bookmarks