News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

CODIGO NEC FOR CONTROL TX

Started by superpro, Jul 22, 2022, 01:59 AM

Previous topic - Next topic

superpro

HELLO FRIENDS, WE ARE WORKING ON A CODE TO MANUFACTURE A REMOTE CONTROL, WITH NEC CODE, THERE WILL BE A SIMILAR PROJECT IN PROTON.

ON_OFF 0xFF50AF
PAUSE_PLAY 0xFFF807
MUTE 0xFFB04F
CH_UP 0xFF00FF
BRILLO_DWN 0xFFC03F
BRILLO_UP 0xFF40BF
CH_DWN 0xFF807F
NO_REPEAT 0xFF7887
REFRESH 0xFF02FD
BUTT_1 0xFF8877
BUTT_2 0xFF48B7
BUTT_3 0xFFC837
BUTT_4 0xFF28D7
BUTT_5 0xFFA857
BUTT_6 0xFF6897
BUTT_7 0xFFE817
BUTT_8 0xFF18E7
BUTT_9 0xFF9867
BUTT_0 0xFF08F7
BUTT_S2 0xFFEA15
BUTT_S3 0xFF1AE5

keytapper

I wrote a decoder for such protocol. It uses a 12F675.
The decoded buttons are left to the user for definitions.
Device 12F675
Declare Xtal 4
Config INTRC_OSC_NOCLKOUT, WDT_OFF, MCLRE_OFF, BODEN_OFF, PWRTE_ON


Symbol TMR1IE = PIE1.0       ' TMR1 Overflow Interrupt Enable
Symbol TMR1IF = PIR1.0       ' TMR1 Overflow Interrupt Flag
Symbol TMR1ON = T1CON.0         ' Timer1 ON
Symbol GPIF = INTCON.0       ' Port Change Interrupt Flag
Symbol T0IF = INTCON.2       ' TMR0 Overflow Interrupt Flag
Symbol T0IE = INTCON.5       ' TMR0 Overflow Interrupt Enable
Symbol PEIE = INTCON.6
Symbol GIE = INTCON.7        ' Global Interrupt Enable
Symbol GPIE = INTCON.3       ' GP Interrupt Enable
Symbol TXDPIN 0              ' Trasmitting pin
Symbol RXDPIN 1              ' Receiving pin
Symbol TRUE = 1              ' Boolean value
Symbol FALSE = 0             ' Boolean value
Symbol _ON = 1               ' Boolean value
Symbol _OFF = 0              ' Boolean value
Symbol TMRSET = 99           ' Setting for internal clock
Symbol BUFLEN = 4            ' The buffer where to store the received command
Symbol BUFSPACE BUFLEN -1
Symbol TXD GPIO.TXDPIN       ' pin assigned for trasmission
Symbol RXD GPIO.RXDPIN       ' pin assigned for reception
Symbol BITMASK (1<<RXDPIN) | (1<<TXDPIN)

Dim buffer[BUFLEN] As Byte
Dim address As Word
Dim command As Word
Dim flags As Byte
Dim x As Byte
Dim i As Byte
Dim y As Word
Dim z As Byte
Dim ticks As Byte
Dim RXerror As flags.0
Dim codeOK As flags.1
Dim RXstart As flags.2
Dim zeroX As flags.6
Dim bitcnt As Byte
Dim GPIOcopy As Byte
Dim GPIOtmp As Byte

Declare Serial_Data 9
Declare Serial_Baud 9600
Declare Rsout_Pin TXD
Declare Rsout_Mode 0
Declare Rsin_Mode 0

On_Hardware_Interrupt GoTo Isr

'============================Main program=======================================
Set_OSCCAL                   ' recover the calibration
Set IOC.RXDPIN
Input RXD
Set WPU.RXDPIN
OPTION_REG = 5               ' The options to set timer0 with 64 prescaler
Clear ANSEL                  ' no analog inputs
Clear flags                  ' all flags cleared
CMCON = 7                    ' no comparators
INTCON = %01101000
Clear ticks                  ' 1/100 sec counter, reset
Clear buffer
Clear bitcnt
T1CON = 1
GPIOtmp = GPIO & BITMASK
Clear TMR1H
Clear TMR1L
Clear GPIF
RSOut "\rReady"
Set GIE
While 1 = 1
    If codeOK = TRUE Then
        address.LowByte = buffer[0]
        address.HighByte = buffer[1]
        command.LowByte = buffer[2]
        command.HighByte = buffer[3]
        RSOut "\rReceived address code = ", Hex address
        RSOut "\rReceived command code = ", Hex command
        GoSub tidy
    End If
    If RXerror = TRUE Then
        GoSub tidy
    End If
Wend
End

tidy:
    flags = flags & 248
    Clear buffer
    Clear bitcnt
Isr:                         ' Interrupt Service Routine
    Context Save
    If T0IF = TRUE Then      ' the timer0 overflowed ?
        TMR0 = TMRSET        ' then reload its count
        Clear T0IF           ' remove the flag
        If ticks < 101 Then  ' when the count reach 100, it's reset to zero
            Inc ticks        ' else it'll increase
        Else
            Clear ticks      ' reset
            Set zeroX        ' it records that one second is past
        End If
    End If

    If GPIF = TRUE Then       ' Verifying if the status of the are changed
        GPIOtmp = GPIO & BITMASK   ' mask out unneeded signals
        GPIF = FALSE
        y.LowByte = TMR1L
        y.HighByte  = TMR1H
        Clear TMR1H
        Clear TMR1L
        GPIOcopy = GPIOcopy ^ GPIOtmp
        If GPIOcopy.RXDPIN = TRUE Then
            If RXerror = FALSE Then
                If RXstart = FALSE Then
                    GoSub ckstart
                Else
                    GoSub ckbits
                End If
            End If
        End If
        GPIOcopy = GPIOtmp
    End If
    Context Restore

ckstart:
    If RXD = FALSE Then                ' falling edge
        If y < 9499 And y > 8500 Then
            Set  bitcnt.7
        Else
            RXerror = TRUE
            RXstart = FALSE
        End If
    Else                                ' raising edge
        If y < 4950 And y > 4050 Then
            If bitcnt.7 = 1 Then
                RXstart = TRUE
                Clear bitcnt
            Else
                RXstart = FALSE
            End If
        End If
    End If
Return

ckbits:
    If RXD = FALSE Then                 ' falling edge
       If y < 620 And y > 506 Then
            If bitcnt > 30 Then
                Set codeOK
                Return
            End If
            Set  bitcnt.7
       Else
            RXerror = TRUE
       End If
    Else                                ' raising edge
        If bitcnt.7 = TRUE Then
            Clear bitcnt.7
              If y < 1855 And y > 1675 Then
                @Setc
                @Rrf    _X,F
            Else If y < 620 And y > 506 Then
                @Clrc
                @Rrf    _X,F
            Else If y > 1855 Then
                RXerror = TRUE
            End If
        End If
        If RXerror = FALSE Then
            i = bitcnt / 8
            buffer[i] = x
        End If
        Inc bitcnt
        If bitcnt // 8 = 0 Then
            Clear x
        End If
    End If
Return

End
This code is interrupt driven and uses Timer1 to count the pulses duration. Similar conditions may be applied for other protocols as well, just to modify slightly the periods of the timing.
Ignorance comes with a cost

superpro

hello friend, the code is not clear to me, where we place the IR led, to transmit.

keytapper

As the rxdpin is not used by the serial transmission, that's used to receive the IR. You may notice that the interrupt on change is set on that.
Ignorance comes with a cost

superpro


hello friend, can you post the circuit. I'm a bit stuck, hehehehe.

superpro

#5
CONTROL SERIAL.jpg

superpro

#6
Device 16F877A
XTAL 20

'program RX
Include "modedefs.bas"     ;incluyen los modos de comunicación

Symbol ledr  PORTB.1             ;nombre ledr al puerto b.1
Symbol leda  PORTB.2               
Symbol ledv  PORTB.3
Symbol ledIR  PORTB.4     'TX IR CODE NEC           
Dim datos As Byte              ;variable para almacenar el dato serial

High ledr                   ;led para saber si ya arrancó el PIC
DelayMS 500
Low ledr

recibir:
SerIn PORTB.0 ,N9600,[datos]    ;recibir dato serial y guardar en datos
If datos="A" Then High ledr :DelayMS 1000     ;si es A encender ledr
If datos="B" Then High leda :DelayMS 1000
If datos="C" Then High ledv :DelayMS 1000
If datos="D" Then High ledIR :DelayMS 1000;; CODE NEC

Low ledr :Low leda : Low ledv

GoTo recibir

keytapper

Quote from: superpro on Jul 24, 2022, 03:00 PMhello friend, can you post the circuit. I'm a bit stuck, hehehehe.
I may spend a little while to produce a functioning project, just to mention which hardware you want to implement. For MCU like 16F628A, 16F887A, 12F675 and 12F1840 I have the hardware to test.
So you mention to use the 16F877A, then I will try something usable. Forgive me if I will take a bit long, I have to manage with several tasks.
Probably each transmitter has a different code for their key press. So the first step is to decode each key received and assign them an alias.
If you plan to use another MCU for the transmitter, then it's a bit much of work, usually I took those cheap Chinese transmitter. For transmission I found some assembly source, but it works on a 12F675.
Ignorance comes with a cost

superpro

IN THIS CODE I WANT TO ADD THE TX IR FOR NEC.

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Device 16F877A
XTAL 20

'program RX
Include "modedefs.bas"            ;incluyen los modos de comunicación

Symbol ledr  PORTB.1            ;nombre ledr al puerto b.1
Symbol leda  PORTB.2             
Symbol ledv  PORTB.3
Symbol ledIR  PORTB.4    'TX IR CODE NEC           
Dim datos As Byte              ;variable para almacenar el dato serial

High ledr                  ;led para saber si ya arrancó el PIC
DelayMS 500
Low ledr

recibir:
SerIn PORTB.0 ,N9600,[datos]    ;recibir dato serial y guardar en datos
If datos="A" Then High ledr :DelayMS 1000    ;si es A encender ledr
If datos="B" Then High leda :DelayMS 1000
If datos="C" Then High ledv :DelayMS 1000
If datos="D" Then High ledIR :DelayMS 1000;; CODE NEC

Low ledr :Low leda : Low ledv

GoTo recibir
[/quote]

superpro


Quote from: keytapper on Jul 24, 2022, 07:46 PM
Quote from: superpro on Jul 24, 2022, 03:00 PMhello friend, can you post the circuit. I'm a bit stuck, hehehehe.
I may spend a little while to produce a functioning project, just to mention which hardware you want to implement. For MCU like 16F628A, 16F887A, 12F675 and 12F1840 I have the hardware to test.
So you mention to use the 16F877A, then I will try something usable. Forgive me if I will take a bit long, I have to manage with several tasks.
Probably each transmitter has a different code for their key press. So the first step is to decode each key received and assign them an alias.
If you plan to use another MCU for the transmitter, then it's a bit much of work, usually I took those cheap Chinese transmitter. For transmission I found some assembly source, but it works on a 12F675.

OK, I HAVE 16F628A, FOR PROBE , NO PROBLEM !!

keytapper

For a transmitter is good to use the PWM module, which the 16F887A (or 12F1840) has, so a simple IR LED will be sufficient. On the other hand a 555 may be use to generate the 38 kHz carrier.
Ignorance comes with a cost

top204

#11
I do not have any NEC transmitting or receiving equipment, but reading a PDF on the NEC protocol, I have written the experimental test code listing below. It has not been tested, but in the simulator it is creating the pulses, and they look, somewhat, correct. It should give you an idea how to create an NEC protocol transmitter.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Experimental NEC InfraRed Tranmsitter. Untested and based upon the "adoh-necinfraredtransmissionprotocol-281113-1713-47344.pdf" file
' The program uses the CCP1 peripheral to produce the 38 KHz modulation
'
' For the NEC protocol, when a key is pressed on the remote control unit, the message transmitted consists of the following, in order:
'
' A 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
' A 4.5ms space
' The 8-bit address for the receiving device
' The 8-bit logical inverse of the address
' The 8-bit command
' The 8-bit logical inverse of the command
' A final 562.5µs pulse burst to signify the end of message transmission.
'
' The four bytes of data bits are each sent least significant bit first
'
' Written for the Positron8 compiler by Les Johnson
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at

    Declare CCP1_Pin = PORTC.2                      ' Tell the compiler what pin is used for the CCP1 peripheral

    Symbol TX_Pin = PORTC.2                         ' The CCP1 peripheral's pin to use for the IR LED  
'
' Create some variables
'   
    Dim NEC_bAddress As Byte                        ' Holds the address value
    Dim NEC_bCommand As Byte                        ' Holds the command value
   
'-------------------------------------------------------------------------------------------------------
' The main program starts here
' Transmit the address and command values using the NEC infrared protocol
'
Main:
    Setup()                                         ' Setup the program
    DelayMS 500                                     ' A delay before starting, so that the PWM waveform does not interfere
   
    NEC_bAddress = 0b00001111                       ' Load the address value to send
    NEC_bCommand = 0b00000001                       ' Load the command value to send
    Do                                              ' Create a loop
        NEC_TX(NEC_bAddress, NEC_bCommand)          ' Transmit the information    
        DelayMS 500                                 ' Create a delay between transmissions
    Loop                                            ' Do it forever

'-------------------------------------------------------------------------------------------------------
' Transmit the address and command bytes, as well as the start and finish pulses
' Input     : pAddress holds the address value to transmit
'           : pCom holds the command value to transmit
' Output    : None
' Notes     : None
'
Proc NEC_TX(pAddress As Byte, pCom As Byte)
    NEC_PulseStart()                        ' Send the starting pulse
    NEC_TX_Byte(pAddress)                   ' Transmit the address byte      
    NEC_TX_Byte(~pAddress)                  ' Transmit the inverse of the address byte
       
    NEC_TX_Byte(pCom)                       ' Transmit the command byte
    NEC_TX_Byte(~pCom)                      ' Transmit the inverse of the command byte       
    NEC_PulseEnd()                          ' Send the endig pulse
EndProc

'-------------------------------------------------------------------------------------------------------
' Transmit 8-bits using the NEC infrared protocol
' Input     : pValue holds the byte to transmit
' Output    : None
' Notes     : None
'
Proc NEC_TX_Byte(pValue As Byte)
    Dim bIndex As Byte
   
    For bIndex = 0 To 7                     ' Create a loop to examine the 8 bits (LSB)
        If GetBit pValue, bIndex = 1 Then   ' Is a bit set?
            NEC_SendOne()                   ' Yes. So send the burst for a 1 bit
        Else                                ' Otherwise. The bit is clear
            NEC_SendZero()                  ' So send the burst for a 0 bit
        EndIf 
    Next
EndProc

'-------------------------------------------------------------------------------------------------------
' Send the bursts of a zero bit
' Input     : None
' Output    : None
' Notes     : Logical '0' is a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
'  
Proc NEC_SendZero()
    PinOutput TX_Pin            ' Enable the 38 KHz PWM signal
    DelayCS 2251                ' Delay for 562.5 uS (with a 16 MHz oscillator)
    PinInput TX_Pin             ' Disable the 38 KHz PWM signal
    DelayCS 2251                ' Delay for 562.5 uS (with a 16 MHz oscillator)
EndProc

'-------------------------------------------------------------------------------------------------------
' Send the bursts of a one bit
' Input     : None
' Output    : None
' Notes     : Logical '1' is a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25ms
'  
Proc NEC_SendOne()
    PinOutput TX_Pin            ' Enable the 38 KHz PWM signal
    DelayCS 2251                ' Delay for 562.5 uS (with a 16 MHz oscillator)
    PinInput TX_Pin             ' Disable the 38 KHz PWM signal
    DelayUS 1687                ' Delay for 1.687 mS
EndProc

'-------------------------------------------------------------------------------------------------------
' Send the start burst
' Input     : None
' Output    : None
' Notes     : Creates a 9ms signal burst and a 4.5ms blank
'
Proc NEC_PulseStart()
    PinOutput TX_Pin            ' Enable the 38 KHz PWM signal
    DelayMS 9                   ' Delay for 9 mS
    PinInput TX_Pin             ' Disable the 38 KHz PWM signal
    DelayUS 4500                ' Delay for 4.5 mS
EndProc

'-------------------------------------------------------------------------------------------------------
' Send the end burst
' Input     : None
' Output    : None
' Notes     : Creates a 562.5 uS pulse burst
'
Proc NEC_PulseEnd()
    PinOutput TX_Pin            ' Enable the 38 KHz PWM signal
    DelayCS 2251                ' Delay for 562.5 uS (with a 16 MHz oscillator)
    PinInput TX_Pin             ' Disable the 38 KHz PWM signal
EndProc

'-------------------------------------------------------------------------------------------------------
' Setup the program
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    HPWM 1, 127, 38000          ' Start CCP1 transmitting at 38 KHz
    PinInput TX_Pin             ' Make its pin an input to stop the waveform
EndProc

The DelayCs timings are based upon an 18F device or enhanced 14-bit core device operating at 16 MHz and they will differ for different oscillator settings. So for a standard 14-bit core devices, you will need to work out how many Nops are required after a DelayUs 562 command to give the 562.5 uS pulse timing.

Also, I am not quite sure what they meant about the Logical Inverse, so the program above inverts the bits, but it may mean the whole byte needs to be reversed.

Below is the program listing operating in a simulator, and the pulse bursts can be seen. The PDF where I found the simple explanation of the protocol for the NEC transmission is also attached below:

NEC_Experiment.jpg

keytapper

Thanks for your interest, Mr Les, a masterpiece as usual :D
. The timing of the transmission is a bit trivial because of the chosen clock, which means that will need care for each clock frequency. Whenever is carried out by a delay or by the help of a timer. Unfortunately a delay may be affected by ISR, so the program should take it in account.
My studies about IR led me to make a single method for NEC an RC5 protocol,  just changing few parameters.
Ignorance comes with a cost

top204

#13
I did notice the similarity of the NEC to the RC5 protocol, because they both use a form of Manchester Encoding. However there are quite a few differences in their timings and head and end pulses and the way the bytes are sent, and the RC5 has a toggle bit.

If using interrupts, the best method for more accurate delays is using a Timer peripheral because they are not effected by the interrupts firing.

The code listing above for the experimental NEC is deliberately over-bloated to show the methodology of the process, and it could be compressed down to three procedures.

top204

#14
For RC5 InfraRed transmissions, the code listing below can be used:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' RC5 InfraRed Transmitter
' The program uses the CCP1 peripheral to produce the 38 KHz modulation
'
' Written for the Positron8 compiler by Les Johnson
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at

    Declare CCP1_Pin = PORTC.2                      ' Tell the compiler what pin is used for the CCP1 peripheral

    Symbol TX_Pin = PORTC.2                         ' The CCP1 peripheral's pin to use for the IR LED
'
' Create some variables
'
    Dim RC5_bSystem  As Byte                        ' Holds the system value
    Dim RC5_bCommand As Byte                        ' Holds the command value
    Dim RC5_tToggle  As Bit                         ' Holds the toggle bit

'-------------------------------------------------------------------------------------------------------
' The main program starts here
' Transmit the system and command values using the RC5 infrared protocol
'
Main:
    Setup()                                         ' Setup the program

    RC5_bSystem = 0b00001111                        ' Load the system value to send
    RC5_bCommand = 0b00000001                       ' Load the command value to send
    Do                                              ' Create a loop
        RC5_TX(RC5_bSystem, RC5_bCommand)           ' Send the values from the TX_Pin, via an IR LED
        Inc RC5_bCommand                            ' Increment the command value
        DelayMS 500                                 ' Create a delay between transmissions
    Loop                                            ' Do it forever

'------------------------------------------------------------------------------
' Transmit the Toggle, System, and Command values as 14-bit RC5 code
' Input     : pSystem (RC5_bSystem) holds the system value
'           : pCommand (RC5_bCommand) holds the command value
' Output    : None
' Notes     : None
'
Proc RC5_TX(pSystem As RC5_bSystem, pCommand As RC5_bCommand)
    Dim bBitIndex As Byte
      
    RC5_tToggle = ~RC5_tToggle                  ' Load the Toggle bit
    RC5_SendOne()                               ' Send the first Start bit
    RC5_SendOne()                               ' Send the second Start bit
'
' Send the toggle bit
'
    If RC5_tToggle = 0 Then                     ' Is the toggle bit clear?
        RC5_SendZero()                          ' Yes. So send a logic zero pulse
    Else                                        ' Otherwise... The bit is set  
        RC5_SendOne()                           ' So. Send a logic one pulse
    EndIf
'
' Send the 5-bit System value, MSB first
'
    For bBitIndex = 4 DownTo 0                  ' Create a loop for the 5 system bits 
        If GetBit(pSystem, bBitIndex) = 0 Then  ' Is the bit to send clear?
            RC5_SendZero()                      ' Yes. So send a logic zero pulse
        Else                                    ' Otherwise... The bit is set
            RC5_SendOne()                       ' So. Send a logic one pulse
        EndIf
    Next
'
' Send the 6-bit Command value, MSB first
'
    For bBitIndex = 5 DownTo 0                  ' Create a loop for the 6 command bits   
        If GetBit(pCommand, bBitIndex) = 0 Then ' Is the bit to send clear?
            RC5_SendZero()                      ' Yes. So send a logic zero pulse
        Else                                    ' Otherwise... The bit is set
            RC5_SendOne()                       ' So. Send a logic one pulse
        EndIf
    Next
    PinInput TX_Pin                             ' Make sure the 38 KHz modulation is disabled
    DelayUS 87                                  ' Frame gap delay
EndProc

'------------------------------------------------------------------------------
' Send a two-phase pulse for a logic 0
' Input     : None
' Output    : None
' Notes     : The logic sequence is High to Low
'
Proc RC5_SendZero()
    PinOutput TX_Pin                            ' Enable the 38 KHz modulation
    DelayUS 887                                 ' Delay for the required time
    PinInput TX_Pin                             ' Disable the 38 KHz modulation
    DelayUS 884                                 ' Delay for the required time
EndProc

'------------------------------------------------------------------------------
' Send a two-phase pulse for a logic 1
' Input     : None
' Output    : None
' Notes     : The logic sequence is Low to High
'
Proc RC5_SendOne()
    PinInput TX_Pin                             ' Disable the 38 KHz modulation
    DelayUS 887                                 ' Delay for the required time
    PinOutput TX_Pin                            ' Enable the 38 KHz modulation
    DelayUS 884                                 ' Delay for the required time
EndProc

'-------------------------------------------------------------------------------------------------------
' Setup the program
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    HPWM 1, 127, 38000                          ' Start CCP1 transmitting at 38KHz
    PinInput TX_Pin                             ' Make its pin an input to stop the waveform
    RC5_tToggle = 0                             ' Reset the toggle state
EndProc

keytapper

I noticed a couple of new keywords PinOutput & PinInput. Probably I should take more time to RTFM. :D
It seems that are alias to HIGH and LOW, am I right ?
Ignorance comes with a cost

top204

#16
PinInput operates the same as Input
PinOutput operates the same as Output

PinLow operates the same as Low
PinHigh operates the same as High

It just makes the code listing easier to scan and read.

In the above programs, the PWM waveform is enabled but its pin is set as an input, so the waveform does not come from the pin. As soon as the pin is made an output, the PWM waveform will appear on it. To turn it off again, set the pin as an input. This is a very efficient way of turning on and off the modulation waveform.