# nec_rc_decoder

NEC Infra Red Remote Control Decoder Library.

Author | Rob Jansen, Copyright © 2023..2023, all rights reserved.
Adapted-by |
Compiler | 2.5r8

Description

    Library for decoding NEC Remote Control messages. The library
                 can be configured to use either the external interrupt or an
                 IOC interrupt to decode the Remote Control Messages.
                 The default is using an external interupt. In order to switch
                 to using an IOC interrupt the user must initialize the IOC
                 to be active on both the rising and falling edge and must
                 define the following alias (example for IOC on pin A2):
                 alias nec_rc_decoder_ioc_flag is IOCAF_IOCAF2

Sources

    Documentation on the NEC protocol:
                https://www.renesas.com/us/en/document/apn/1184-remote-control-ir-receiver-decoder

Notes

    The 38 kHz Infra Red receiver (active low) must be connected
                 to the external interrupt pin or the used IOC pin.

Dependencies

No dependency found

## Summary

### Global variables/contants

- `const _NEC_RC_EDGE_RISING = TRUE`
- `const dword _NEC_RC_LEAD_SPACE_MIN = (3375 * NEC_RC_SCALING) / 100`
- `const _NEC_RC_EDGE_FALLING = FALSE`
- `const dword _NEC_RC_BURST_MIN = (422 * NEC_RC_SCALING) / 100`
- `const byte _NEC_RC_LEAD = 1`
- `var byte _nec_rc_command`
- `var byte _nec_rc_count`
- `const dword _NEC_RC_1_MIN = (1267 * NEC_RC_SCALING) / 100`
- `const byte _NEC_RC_BITS = 32`
- `var word _nec_rc_bit_time`
- `var bit _nec_rc_protocol_standard`
- `var byte _nec_rc_command_n`
- `const byte _NEC_RC_END_DATA = 4`
- `const byte _NEC_RC_SPACE = 2`
- `var bit _nec_rc_available`
- `const dword _NEC_RC_BURST_MAX = (703 * NEC_RC_SCALING) / 100`
- `const dword _NEC_RC_0_MIN = (422 * NEC_RC_SCALING) / 100`
- `const word _NEC_RC_OVERFLOW = 65535`
- `var byte _nec_rc_state`
- `var dword _nec_rc_message`
- `var bit _nec_rc_timer1_expired`
- `const dword _NEC_RC_1_MAX = (2109 * NEC_RC_SCALING) / 100`
- `const byte _NEC_RC_END_REPEAT = 5`
- `var bit _nec_rc_repeat`
- `var bit _nec_rc_burst_pulse`
- `var byte _nec_rc_address`
- `const dword _NEC_RC_REP_SPACE_MIN = (1688 * NEC_RC_SCALING) / 100`
- `const dword _NEC_RC_LEAD_SPACE_MAX = (5625 * NEC_RC_SCALING) / 100`
- `const dword _NEC_RC_0_MAX = (703 * NEC_RC_SCALING) / 100`
- `const byte _NEC_RC_DATA = 3`
- `const byte _NEC_RC_IDLE = 0`
- `const dword _NEC_RC_LEAD_MIN = (6750 * NEC_RC_SCALING) / 100`
- `const dword _NEC_RC_REP_SPACE_MAX = (2812 * NEC_RC_SCALING) / 100`
- `const dword _NEC_RC_LEAD_MAX = (11250 * NEC_RC_SCALING) / 100`
- `var byte _nec_rc_address_n`

### Procedures

- `nec_rc_set_protocol_extended()`
- `nec_rc_decoder_init()`
- `nec_rc_set_protocol_standard()`
- `nec_rc_get_data_standard(byte out address, byte out command)`
- `nec_rc_get_data_extended(word out address, byte out command)`

Private

- `_nec_rc_interrupt()`
- `_nec_rc_timer1_interrupt()`
- `_nec_rc_reset_decoder()`
- `_nec_rc_timer1_reset()`

### Functions

- `nec_rc_repeat_received() return bit`
- `nec_rc_message_received() return bit`

Private

- `_nec_rc_handle_interrupt_flag() return bit`
- `_nec_rc_get_timer1_value() return word`

## API details

### Global variables/contants

- `const _NEC_RC_EDGE_RISING = TRUE`

      We decode the IR bitstream on both edges for better performance.

- `const dword _NEC_RC_LEAD_SPACE_MIN = (3375 * NEC_RC_SCALING) / 100`

      Nominal is 4500 us.

- `const _NEC_RC_EDGE_FALLING = FALSE`

  No documentation found

- `const dword _NEC_RC_BURST_MIN = (422 * NEC_RC_SCALING) / 100`

      Nominal is 562.5 us

- `const byte _NEC_RC_LEAD = 1`

  No documentation found

- `var byte _nec_rc_command`

      Holds the received command

- `var byte _nec_rc_count`

      Bit counter

- `const dword _NEC_RC_1_MIN = (1267 * NEC_RC_SCALING) / 100`

      Nominal is 1687 us.

- `const byte _NEC_RC_BITS = 32`

      Number of bits in RC message.

- `var word _nec_rc_bit_time`

      Layout of the _nec_rc_bits:
      bit 07 .. 00 = Address
      bit 15 .. 08 = Address Not
      bit 23 .. 16 = Command
      bit 31 .. 24 = Command Not

- `var bit _nec_rc_protocol_standard`

      If set the standard protocol is supported

- `var byte _nec_rc_command_n`

      Holds the inverted received command

- `const byte _NEC_RC_END_DATA = 4`

  No documentation found

- `const byte _NEC_RC_SPACE = 2`

  No documentation found

- `var bit _nec_rc_available`

      Indicates that an RC message is available

- `const dword _NEC_RC_BURST_MAX = (703 * NEC_RC_SCALING) / 100`

  No documentation found

- `const dword _NEC_RC_0_MIN = (422 * NEC_RC_SCALING) / 100`

      Nominal is 562.5 us.

- `const word _NEC_RC_OVERFLOW = 65535`

      Max value of timer 1 before overflow.

- `var byte _nec_rc_state`

      IR Decoder state machine variable

- `var dword _nec_rc_message`

      Internal decoding variables.

- `var bit _nec_rc_timer1_expired`

      If set timer has overflow

- `const dword _NEC_RC_1_MAX = (2109 * NEC_RC_SCALING) / 100`

  No documentation found

- `const byte _NEC_RC_END_REPEAT = 5`

  No documentation found

- `var bit _nec_rc_repeat`

      Indicates when a message is just a repeat

- `var bit _nec_rc_burst_pulse`

      Indicates that we expect a burst pulse

- `var byte _nec_rc_address`

      Holds the received address

- `const dword _NEC_RC_REP_SPACE_MIN = (1688 * NEC_RC_SCALING) / 100`

      Nominal is 2500 us.

- `const dword _NEC_RC_LEAD_SPACE_MAX = (5625 * NEC_RC_SCALING) / 100`

  No documentation found

- `const dword _NEC_RC_0_MAX = (703 * NEC_RC_SCALING) / 100`

  No documentation found

- `const byte _NEC_RC_DATA = 3`

  No documentation found

- `const byte _NEC_RC_IDLE = 0`

      Decoder states.

- `const dword _NEC_RC_LEAD_MIN = (6750 * NEC_RC_SCALING) / 100`

      Nominal is 9000 us.

- `const dword _NEC_RC_REP_SPACE_MAX = (2812 * NEC_RC_SCALING) / 100`

  No documentation found

- `const dword _NEC_RC_LEAD_MAX = (11250 * NEC_RC_SCALING) / 100`

  No documentation found

- `var byte _nec_rc_address_n`

      Holds the inverted received address

### Procedures

- `nec_rc_set_protocol_extended()`

      Set the library to support the extended protocol.

- `nec_rc_decoder_init()`

      Initialize the NEC Remote Control Decoder. This will initialize timer 1
      and will enable the required interrupts. The Remote Control Decoder is set
      to support the standard NEC protocol.

- `nec_rc_set_protocol_standard()`

      Set the library to support the standard protocol. This is the default.

- `nec_rc_get_data_standard(byte out address, byte out command)`

      Return the last received Remote Control data using the standard protocol.
      When called, the message received flag is cleared.

- `nec_rc_get_data_extended(word out address, byte out command)`

      Return the last received Remote Control data using the extended protocol.
      When called, the message received flag is cleared.

Private

- `_nec_rc_interrupt()`

      External interrupt.

- `_nec_rc_timer1_interrupt()`

      Timer1 interrupt. Occurs when the timer overflows.

- `_nec_rc_reset_decoder()`

      Reset the Remote Control to its initial state.

- `_nec_rc_timer1_reset()`

      Reset Timer 1 and start the timer.

### Functions

- `nec_rc_repeat_received() return bit`

      Returns TRUE is a valid Remote Control repeate message was received.
      When called, the repeat flag is cleared.

- `nec_rc_message_received() return bit`

      Returns TRUE when a valid Remote Control message was received.

Private

- `_nec_rc_handle_interrupt_flag() return bit`

      This procedure handles the interupt flag. Reason for this procedure is to be
      able to use two differnt interrupt types, IOC and external.

- `_nec_rc_get_timer1_value() return word`

       ----------------------------------------------------------------------------
      Stop Timer 1 and return the value of Timer 1. We return the Timer value if
      it has not overflown otherwise this function returns the overflow value.

## Related samples

Here are the list of samples which use this library:

16f1825 | 16f1825_nec_rc_decoder_serial.jal
16f1825 | 16f1825_nec_rc_decoder_ssd1306.jal
16f18857 | 16f18857_nec_rc_decoder_serial.jal
16f877a | 16f877a_nec_rc_decoder_serial.jal