This is a simple example for using the 18F2431 motion feedback modules capture hardware
to learn IR codes, and codes from RF encoders.
This capture module can capture the value of TMR5 on every state change, makingCode:'****************************************************************
'* Name : CODE_LEARN.BAS *
'* Author : B. Reynolds *
'* Notice : Copyright (c) 2008 B. Reynolds *
'* : All Rights Reserved *
'* Date : 8/22/2008 *
'* Version : 1.0 *
'* Notes : Using the 18F2431 Motion Feedback Module capture *
'* : to learn encoder or IR transmitter codes. *
'****************************************************************
' Connections;
' IR detector output to PORTA.2 CAP1 input
' PORTB.0 ------/\/\/\/----|>|----GND LED on RB0 (for press/release indicator)
' PORTC.6 --MAX232---PC serial port RX (to print results)
DEFINE NO_CLRWDT 1 ' it's disabled in config
DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE DEBUG_REG PORTC
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true
NumPulses CON 26 ' total signal states to learn (26 for Sony)
T1 VAR WORD[NumPulses]
INDEX VAR BYTE
KeyNum VAR BYTE
SYMBOL Capture = PIR3.1
CLEAR
ANSEL0 = 0 ' all digital
TRISA.2 = 1 ' cap1 input pin (Capture input)
INTCON = 0 ' interrupts off
TMR5H = 0 ' clear high byte of TMR5 count
TMR5L = 0 ' clear low byte
T5CON = %00000001 ' prescale=1:1, int clock, TMR5=on
Main:
CAP1CON = %01001000 ' auto time base reset, capture on every state change
Capture = 0
FOR KeyNum = 0 TO 14 ' learn 15 total button codes
CAP1CON = %01001000 '
HIGH 0 ' indicate start (user press button)
Capture = 0 ' clear capture flag before starting
' Wait for 1st capture on state-change. Note: We don't care what the 1st capture
' value is since it's the beginning high-to-low edge (for IR), and of no significance.
' We only want the length of each high & low signal after the falling edge.
WHILE Capture = 0 : WEND
Capture = 0
FOR INDEX = 0 TO NumPulses-1 ' captures IR signal in 1uS increments
WHILE Capture = 0 : WEND ' wait for capture event
Capture = 0 ' clear int flag after each capture
T1[INDEX] = ((CAP1BUFH<<8) + CAP1BUFL) ' store capture values
Next INDEX
CAP1CON = 0 ' disable capture immediately after to avoid unwanted captures
Capture = 0
' if user holds transmitter button down too long.
LOW 0 ' indicates user should release transmitter button
PAUSE 1000
' print button codes
FOR INDEX = 0 TO NumPulses-1 STEP 2
DEBUG "low = ", DEC T1[INDEX],"uS",13,10 ' print low signal period
DEBUG "high = ",DEC T1[INDEX+1],"uS",13,10 ' print high signal period
NEXT INDEX
DEBUG 10,13
NEXT KeyNum
Finished: ' indicate finished
TOGGLE 0
PAUSE 200
GOTO Finished
END
it really simple to capture & recreate timing signals for remote control encoders.
It's really handy if you don't have an O-scope, and want to create code for a custom IR or
RF encoder IC.