This project implements a NEC IR decoder for CH32V003 series microcontrollers. It's designed to be a lightweight and efficient library for receiving and decoding infrared signals from common remote controls that use the NEC protocol.
- Non-blocking: The decoder uses pin change interrupts and a state machine to decode the signal without blocking the main loop.
- Lightweight: The library has a small footprint, making it suitable for resource-constrained microcontrollers like the CH32V003.
- Easy to use: A simple
available()andread()interface is provided. - Debug Mode: Includes an optional debug mode to print detailed state machine transitions and timing information to the serial port.
The pin configuration is straightforward.
-
IR Receiver Input: The IR receiver's data pin should be connected to a pin on the CH32V003. In the example
main.cpp, this isPD4. You can change this in theNecIrDecoderconstructor:NecIrDecoder ir_decoder(PD4); // Using PD4 as the IR input pin
-
Status LED (Optional): The example uses a "heartbeat" LED to indicate that the system is running. This is defined on pin
PC3.#define HEARTBEAT_PIN PC3 // Using PC3 for a status LED
The src/main.cpp file serves as a test program and an example of how to use the NecIrDecoder library.
-
Initialization:
- It initializes the serial communication for printing output.
- It creates an instance of the
NecIrDecoder, passing the IR input pin (PD4). - It calls
ir_decoder.begin()to set up the pin and the interrupt handler.
-
Main Loop (
loop()):- The loop is non-blocking.
- It continuously checks if the decoder has data available using
ir_decoder.available(). - If data is available, it reads the decoded data into a
NecDecodedDatastruct usingir_decoder.read(). - The decoded data (address, command, raw data, and repeat flag) is then printed to the serial monitor.
The main.cpp file includes a compile-time switch NEC_IR_DECODER_DEBUG.
- If
NEC_IR_DECODER_DEBUGis set to1, the program will print detailed information about the ISR (Interrupt Service Routine) state transitions, timings, and function calls within the decoder. This is useful for debugging the decoder itself or troubleshooting signal reception issues. - If
NEC_IR_DECODER_DEBUGis set to0, all debug output is disabled, resulting in a cleaner and faster execution, suitable for a release version.
- Connect your IR receiver's data pin to
PD4on your CH32V003. - Connect the IR receiver's VCC and GND to the appropriate power pins.
- (Optional) Connect an LED to
PC3to act as a status indicator. - Upload the code using PlatformIO.
- Open the Serial Monitor at 115200 baud.
- Point a NEC-compatible remote at the sensor and press buttons. You should see the decoded address and command codes printed in the monitor.