News:

;) This forum is the property of Proton software developers

Main Menu

rf433

Started by Pepe, Oct 07, 2025, 03:51 AM

Previous topic - Next topic

Pepe

look #5

Dolci

Can you please upload Proteus 8.15

Thanks,
Dolci

Teo

#2
Hi Pepe,
You did an excellent project!
Please be kind and give us the .bas files.
This way we can better understand how the project works.
Thanks in advance,
Teo


Teo

Hi Pepe,
Forgive my boldness, is it possible for you to also post the reception part?
Thank you in advance,
Teo

Pepe

new demo with sources

Pepe

#5
another proteus example with ht12e/ht12d


ht12d

'============================================================
' HT12E Receiver for PIC12F1840
'============================================================
Device = 12F1840

'----------------- Fuses -----------------
Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, _
        BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF, LVP_OFF

Declare Xtal = 32          ' Internal clock 32 MHz with PLL
Declare Optimiser_Level = 0
Declare Watchdog off
Declare Create_Coff On

Declare Hserial_Baud = 9600
Declare HRSOut_Pin = PORTA.0

'----------------- Initialize PLL -----------------
OSCCON = %11110000
Repeat : Until OSCSTATBits_PLLR = 1

'----------------- Pins -----------------
Symbol InPin    = PORTA.5       ' Input from HT12E
Symbol led      = PORTA.2       ' LED indicator
Symbol IOCflag  = IOCAF.5
Symbol IOCneg   = IOCAN.5
Symbol IOCpos   = IOCAP.5
Symbol T0IF     = INTCON.2
Symbol T0IE     = INTCON.5
Symbol TMR1_ON  = T1CON.0

'----------------- Variables -----------------
Dim bitBuf As Word            ' 12-bit address + 4-bit data
Dim sampleIdx As Byte
Dim SyncDetect As Bit
Dim pulseWidth As Word
Dim addr As Word
Dim dataByte As Byte
Dim wTMR1 As TMR1L.Word
Dim pos As Byte
Dim vector[4] As Word
Clear

'----------------- Initialize I/O -----------------
ANSELA = 0
led = 0
Input InPin
Output PORTA.2
Output PORTA.4

'----------------- Timer1 (Pulse Width Measurement) -----------------
T1CON = %00110000   ' Timer1 prescaler 1:8, initially OFF

'----------------- Timer0 (Bit Sampling) -----------------
OPTION_REG = %00000101      ' Timer0 prescaler 1:16

T0IF = 0
T0IE = 0
IOCflag = 0
IOCneg = 0
IOCpos = 1                 ' Detect rising edge initially

'----------------- Interrupts -----------------
INTCON = %11001000         ' GIE=1, PEIE=1, IOCIE=1
On_Hardware_Interrupt GoTo Isr

'=================== Main Loop ====================
Do
    If SyncDetect = 1 Then
        SyncDetect = 0

        ' --- Extract address and data ---
        addr = (bitBuf >> 4) & $FFF   ' 12-bit address
        dataByte = bitBuf & $F        ' 4-bit data

        ' --- Store in history vector to check for repeated transmission ---
        vector[pos] = bitBuf
        Inc pos

        If pos = 4 Then
            pos = 0
            ' Check if all captured packets match
            For i = 0 To 2
                If vector[i] <> bitBuf Then Break
            Next i
            If i = 3 Then
                ' Valid reception, print via UART
                HSerOutLn ["ADDR=", Hex addr, "  DATA=", Hex dataByte]
            Else
                Clear vector
            End If
        End If
        ' Re-enable edge detection for next capture
        IOCpos = 1
        IOCflag = 0
    Else
        ' Timeout to reset capture if Timer1 runs too long
        If wTMR1 > 15000 Then
            TMR1_ON = 0
            wTMR1 = 0
            pos = 0
        End If
    End If
Loop

'=================== ISR ====================
Isr:
Context Save

'----------------- Falling/Rising Edge Detection -----------------
If IOCflag = 1 Then
    IOCflag = 0

    If IOCneg = 1 Then
        ' Capture pulse width from Timer1
        pulseWidth = wTMR1
        ' Check for valid sync pulse (approx. 1200–1700us)
        If pulseWidth >= 1200 And pulseWidth <= 1700 Then
            TMR0 = 70          ' First Timer0 sample adjusted
            T0IE = 1           ' Enable Timer0 to sample bits
            SyncDetect = 0
            sampleIdx = 0
            IOCpos = 0         ' Disable IOC during capture
            IOCneg = 0
            bitBuf = 0
            wTMR1 = 0
            led = 1
        Else
            IOCpos = 1
            IOCneg = 0
        End If
    Else
        ' Timer1 running to measure next pulse
        wTMR1 = 0
        IOCpos = 0
        IOCneg = 1
        TMR1_ON = 1
    End If
    T0IF = 0
End If

'----------------- Timer0 Overflow: Sample Bits -----------------
If T0IF = 1 Then
    T0IF = 0
    TMR0 = 173         ' Reload for normal intervals

    ' Shift in the new bit
    bitBuf = (bitBuf << 1) + InPin
    Inc sampleIdx

    ' After capturing 12 bits, stop Timer0
    If sampleIdx >= 12 Then
        T0IE = 0
        SyncDetect = 1
        wTMR1 = 0
        sampleIdx = 0
        led = 0
    End If
End If

Context Restore



ht12e

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 12F675

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, BOREN_OFF, CP_OFF, CPD_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal 20
Declare Optimiser_Level = 3

Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Reminders Off
Declare Hints Off
Declare Create_Coff On

TRISIO = %11111110   ' Set GPIO.0 as output, others as input

'----------------- Transmission Pin -----------------
Symbol RF_TX = GPIO.0

Symbol T_Half = 330         ' Half bit time in microseconds
Symbol Repe = 4             ' Repeat 4 times like HT12E

Dim Dire As Byte            ' Address (8 bits)
Dim Dat As Byte             ' Data (4 bits)
Dim i As Byte
Dim j As Byte

'------------------------------------------------------------
' Main program
'------------------------------------------------------------
Dire = $AA    ' Example address
Dat = $0      ' Example data

Do
    For j = 1 To Repe
        Call SendWord()      ' Transmit word multiple times
    Next
   
    Inc Dat
   
    If Dat > 15 Then Dat = 0
   
    DelayMS 100              ' Pause between transmissions
Loop

'------------------------------------------------------------
' Generate a Manchester encoded bit
'------------------------------------------------------------
Proc SendBit(b As Bit)
    If b = 1 Then
        High RF_TX
        DelayUS T_Half
        Low RF_TX
        DelayUS T_Half
    Else
        Low RF_TX
        DelayUS T_Half
        High RF_TX
        DelayUS T_Half
    EndIf
EndProc

'------------------------------------------------------------
' Transmit a word (8-bit address + 4-bit data)
'------------------------------------------------------------
Proc SendWord()
    Dim di As Byte
    Dim dato As Byte
    Dim bb As Bit

    ' Sync pulse (long pulse)
    High RF_TX
    DelayUS 1320 
    Low RF_TX
    DelayUS 1320

    ' Send address (8 bits MSB first)
    di = Dire
    For i = 0 To 7
        bb = di.7
        SendBit(bb)
        di = di << 1
    Next

    ' Send data (4 bits MSB first)
    dato = Dat
    For i = 0 To 3
        bb = dato.3
        SendBit(bb)
        dato = dato << 1
    Next

    Low RF_TX
    DelayMS 10
EndProc


Fanie

#6
May I ask Pepe, why not use the Keeloq code hopping ?

It is very secure, and you use only one pic, and of course the RF circuit.
It was developed by two guys here in Centurion where I live, and I had my own Rx Tx before it was sold to Microchip.
Back in the day we did all this with assembler.  It seems it was since made more secure too.
The remotes (I buy lately) use the 16F628's.
https://ww1.microchip.com/downloads/aemDocuments/documents/WSG/ApplicationNotes/ApplicationNotes/AN5322-Sub-GHz-Keyfob-Using-KeeLoq-Application-Note-DS00005322.pdf