PDA

View Full Version : Generic RF Receiver



dhouston
- 8th September 2009, 15:35
This receives 12-48 bits of PDM (Pulse Distance Modulation) or PWM (Pulse Width Modulation) codes with bit periods from 1.7-2.5mS and with start pulses of 1.9-9.6mS.

To use with IR, change all While GPIO.1 to While !GPIO.1 and vice versa.

It uses Timer1 to measure start pulses and bit periods and Timer0 to timeout if a bit period exceeds 2.5mS.

'=======================| RF RECEIVER |=========================

'PIC12F629 @ 4MHz 455 words uses MPASM

'Pin 1 - Vdd
'Pin 2 - GPIO.5
'Pin 3 - GPIO.4
'Pin 4 - GPIO.3
'Pin 5 - GPIO.2 RS232 output @ 9600bps
'Pin 6 - GPIO.1 Receives up to 48 bits of PDM/PWM RF with lead-in of 2-9mS
'Pin 7 - GPIO.0
'Pin 8 - Vss
'================================================= ==============

@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF

DEFINE OSCCAL_1K 1
DEFINE OSC 4
DEFINE DEBUG_REG GPIO
DEFINE DEBUG_BIT 2
DEFINE DEBUG_MODE 1
DEFINE DEBUG_BAUD 9600

@Timer1 = TMR1L
Timer1 VAR WORD EXT
T0IF VAR INTCON.2 'TMR0 overflow flag
TMR1ON VAR T1CON.0 'Timer1 ON/OFF control bit
RF VAR byte[6]
ID VAR byte
period VAR word
i VAR byte
bits VAR byte
bytes VAR byte
sof VAR word
cnt VAR byte

CMCON =%00000111 'disable comparators
INTCON =%00000000 'disable interrupts
T1CON =%00000000 'TMR1 no prescaler
OPTION_REG =%10000001 'TMR0 1:4 prescaler, no PU

DEBUG "RF Receiver",13,10

init: RF[0]=0:RF[1]=0:RF[2]=0:RF[3]=0:RF[4]=0:RF[5]=0
While !GPIO.1:Wend 'wait rising edge
TMR1ON=0:Timer1=0:TMR1ON=1 'stop, clear, start TMR1
While GPIO.1:Wend 'wait falling edge
TMR1ON=0:sof=Timer1 'stop, read TMR1
If (sof<1900) Then init
If (sof>9600) Then init
While !GPIO.1:Wend 'wait rising edge
TMR1ON=0:Timer1=0:TMR1ON=1 'stop, clear, start TMR1
TMR0=49:T0IF=0:cnt=0 'reset TMR0, cnt
i=0
Repeat
While GPIO.1 'wait falling edge
If T0IF Then
GoSub cycles
EndIf
Wend 'falling edge
While !GPIO.1 'wait rising edge
If T0IF Then
GoSub cycles
EndIf
Wend 'rising edge
TMR1ON=0:period=Timer1 'stop, read TMR1
Timer1=0:TMR1ON=1 'clear, start TMR1
TMR0=49:T0IF=0:cnt=0 'reset TMR0, cnt
If (period<850) Then init
If (period>1300) Then
RF.0(i)=1
EndIf
i=i+1
Until (i>47)
break: If (i<12) Then init
bytes=i/8
If i//8 Then
bytes=bytes+1
EndIf
Debug ihex2 i
For i = 0 to bytes-1
Debug ihex2 RF[i]
Next
DEBUG 32,dec sof,13,10
GoTo init

cycles: TMR0=49:T0IF=0:cnt=cnt+1
If (cnt>2) Then break 'timeout @2.5mS
Return

End