News:

;) This forum is the property of Proton software developers

Main Menu

Nec iRx Simulation with remote control

Started by Pepe, Aug 26, 2025, 12:44 AM

Previous topic - Next topic

Pepe

Proteus nec ir simulation with remote control
Add IRX library in Proteus 8.15 Professional\Data\Library

Pepe

extended version

' =======================================
' NEC IR Remote Receiver - PIC16C72A
' Proton IDE / Positron BASIC
' Con NEC clásico, extendido y repeat automático
' =======================================

Device = 16C72A
Config FOSC_HS, WDTE_OFF, PWRTE_OFF, CP_OFF, BOREN_OFF

Xtal   = 20                     ' 20 MHz crystal
Declare Optimiser_Level = 3
Declare Create_Coff On
Declare Watchdog Off

' -------------------------------
' LCD Configuration (16x2)
' -------------------------------
Declare LCD_DTPin PORTC.0
Declare LCD_ENPin PORTC.5
Declare LCD_RSPin PORTC.4
Declare LCD_Interface 4
Declare LCD_Lines 2
Declare LCD_Type  0

' -------------------------------
' NEC Protocol Timings (µs)
' -------------------------------
Symbol NEC_START_PULSE  = 9000/2
Symbol NEC_START_SPACE  = 4500/2
Symbol NEC_REPEAT_SPACE = 2250/2

' -------------------------------
' IR Sensor Input
' -------------------------------
Symbol IR_Sensor = PORTA.0
Input IR_Sensor

' -------------------------------
' Variables
' -------------------------------
Dim necCode    As Dword
Dim bitCount   As Byte
Dim Result     As Word
Dim spaceWidth As Word
Dim timewait   As Word
Dim timeout    As Bit

Dim Addr    As Word
Dim Cmd     As Word
Dim necMode As Byte   ' 0=invalido,1=NEC clasico,2=NEC ext
Dim repeatFlag As Bit

Dim LastAddr As Word
Dim LastCmd  As Word

' ===============================
' Main Program
' ===============================
Cls
Print At 1,1, "16C72A IR Test"

Main_Loop:
    GoSub Read_NEC_Remote
    If timeout = 1 Then
        If repeatFlag = 1 Then
            ' Repetir último comando
            Addr = LastAddr
            Cmd  = LastCmd
        Else
            GoSub DecodeNEC
            ' Guardar último comando válido
            LastAddr = Addr
            LastCmd  = Cmd
        End If

        ' Mostrar en LCD
        If necMode = 1 Then
            Print At 2,1, "NEC ADD:", Hex2 Addr, " CMD:", Hex2 Cmd, "   "
        ElseIf necMode = 2 Then
            Print At 2,1, "EXT ADD:", Hex4 Addr, " CMD:", Hex4 Cmd
        Else
            Print At 2,1, "Codigo invalido   "
        EndIf
    End If
    GoTo Main_Loop
   

' ===============================
' Read NEC Remote Code
' ===============================
Read_NEC_Remote:
    GoSub Wait_For_Start
    If timeout = 0 Then Return
    GoSub Wait_For_Space
    If timeout = 0 Then Return
    If repeatFlag = 1 Then Return   ' repetir último comando
   
    necCode = 0
    For bitCount = 0 To 31
        GoSub Read_Bit
        If timeout = 0 Then Return
       
        If spaceWidth > 600 Then
            SetBit necCode, bitCount       ' logic 1
        Else
            ClearBit necCode, bitCount     ' logic 0
        End If
    Next bitCount
    Return

' ===============================
' Wait for Start Pulse (~9 ms)
' ===============================
Wait_For_Start:
    timewait = 10000
    While IR_Sensor = 1
        Dec timewait
        If timewait = 0 Then
            timeout = 0
            Return
        EndIf                     
    Wend
    timeout = 1
    Return

' ===============================
' Wait for Start Space (4.5 ms o 2.25 ms)
' ===============================
Wait_For_Space:
    Result = PulseIn IR_Sensor, 1
    repeatFlag = 0
    If Result > NEC_START_SPACE * 0.7 And Result < NEC_START_SPACE * 1.3 Then
        timeout = 1
    ElseIf Result > NEC_REPEAT_SPACE * 0.7 And Result < NEC_REPEAT_SPACE * 1.3 Then
        repeatFlag = 1
        timeout = 1
    Else
        timeout = 0
    End If
    Return

' ===============================
' Read Single Bit
' ===============================
Read_Bit:
    spaceWidth = PulseIn IR_Sensor, 1
    If spaceWidth < 200 Then
        timeout = 0
        Return
    End If
    timeout = 1
    Return

' ===============================
' Decode NEC (clásico o extendido)
' ===============================
DecodeNEC:
    necMode = 0
   
    ' --- Caso NEC clásico ---
    If necCode.Byte0 ^ necCode.Byte1 = $FF And necCode.Byte2 ^ necCode.Byte3 = $FF Then
       Addr = necCode.Byte0
       Cmd  = necCode.Byte2
       necMode = 1
       Return
    EndIf

    ' --- Caso NEC extendido ---
    Addr.HighByte = necCode.Byte1
    Addr.LowByte  = necCode.Byte0   ' Dirección de 16 bits

    Cmd.HighByte  = necCode.Byte3
    Cmd.LowByte   = necCode.Byte2   ' Comando de 16 bits

    necMode = 2
    Return

' =======================================
' End of Program
' =======================================


Abdullah

#2
HELLO SIR THANK YOU VERY MUCH FOR THIS CODE
I WILL ALWAYS REMEMBER YOU IN MY PRAYERS.MAY GOD PROLONG YOUR LIFE,REMOVE ALL YOUR WORRIES,AND GIVE YOU FURTHER PROGRESS.MAY YOU ALWAYS BE HAPPY

HOLA SEÑOR, MUCHAS GRACIAS POR ESTE CÓDIGO. SIEMPRE TE RECORDARÉ EN MIS ORACIONES. QUE DIOS TE PROLONGUE LA VIDA, TE ALEJE DE TODAS TUS PREOCUPACIONES Y TE CONCEDA UN MAYOR PROGRESO. QUE SIEMPRE SEAS

Pepe

Another example

Abdullah

sir how to check all code of this remote IRC CHINA REMOTE

Pepe

#5
look the picture and this


 Select Case Cmd.HighByte
                            
    Case $03
          PORTB = 0
    Case $07               <------ code for button 1
          Toggle PORTB.0