fork download
  1. #include <xc.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4.  
  5. // Configuration bits
  6. #pragma config OSC = HS // High-Speed Oscillator
  7. #pragma config WDT = OFF // Watchdog Timer disabled
  8. #pragma config LVP = OFF // Low-Voltage Programming disabled
  9. #pragma config BOR = ON // Brown-out Reset enabled
  10. #pragma config PWRT = ON // Power-up Timer enabled
  11.  
  12. #define _XTAL_FREQ 20000000 // External 20MHz crystal
  13. #define IR_PIN PORTEbits.RE0 // IR receiver pin
  14.  
  15. // Define variables for output pins
  16. #define bulb1 PORTBbits.RB0
  17. #define bulb2 PORTBbits.RB2
  18. #define fan PORTBbits.RB4
  19.  
  20. // Define variables for virtual ground/vcc pins
  21. #define gnd1 PORTEbits.RE1 = 0;
  22. #define vcc PORTEbits.RE2 = 1;
  23.  
  24. // Global variables for decoding the NEC protocol
  25. uint32_t ir_code;
  26.  
  27. // UART initialization
  28. void UART_Init()
  29. {
  30. SPBRG = 129; // Baud rate = 9600 for 20MHz crystal
  31. TXSTAbits.BRGH = 1; // High-speed baud rate
  32. TXSTAbits.TXEN = 1; // Enable transmission
  33. RCSTAbits.SPEN = 1; // Enable UART module
  34. RCSTAbits.CREN = 1; // Enable reception
  35. }
  36.  
  37. // UART send a single character
  38. void UART_Write(char data)
  39. {
  40. while (!TXSTAbits.TRMT); // Wait for buffer to be empty
  41. TXREG = data; // Send character
  42. }
  43.  
  44. // UART send a string
  45. void UART_Write_String(const char *str)
  46. {
  47. while (*str)
  48. {
  49. UART_Write(*str++);
  50. }
  51. }
  52.  
  53. // NEC protocol signal decoding
  54. bool nec_remote_read()
  55. {
  56. uint8_t count = 0, i;
  57.  
  58. // Check 9ms pulse (remote sends logic high)
  59. while ((IR_PIN == 0) && (count < 200))
  60. {
  61. count++;
  62. __delay_us(50);
  63. }
  64.  
  65. if ((count > 199) || (count < 160))
  66. return false;
  67.  
  68. count = 0;
  69.  
  70. // Check 4.5ms space (remote sends logic low)
  71. while ((IR_PIN == 1) && (count < 100))
  72. {
  73. count++;
  74. __delay_us(50);
  75. }
  76.  
  77. if ((count > 99) || (count < 60))
  78. return false;
  79.  
  80. // Read message (32 bits)
  81. ir_code = 0; // Reset ir_code before reading
  82. for (i = 0; i < 32; i++)
  83. {
  84. count = 0;
  85. while ((IR_PIN == 0) && (count < 14))
  86. {
  87. count++;
  88. __delay_us(50);
  89. }
  90.  
  91. if ((count > 13) || (count < 8))
  92. return false;
  93.  
  94. count = 0;
  95.  
  96. while ((IR_PIN == 1) && (count < 40))
  97. {
  98. count++;
  99. __delay_us(50);
  100. }
  101.  
  102. if ((count > 39) || (count < 8))
  103. return false;
  104.  
  105. if (count > 20) // If space width > 1ms
  106. ir_code |= (1UL << (31 - i)); // Write 1 to bit (31 - i)
  107. else // If space width < 1ms
  108. ir_code &= ~(1UL << (31 - i)); // Write 0 to bit (31 - i)
  109. }
  110.  
  111. return true;
  112. }
  113.  
  114. void main(void)
  115. {
  116. // Configure the system
  117. TRISEbits.TRISE0 = 1; // RE0 as input (IR receiver)
  118. TRISEbits.TRISE1 = 0; // RE1 as output (GND)
  119. TRISEbits.TRISE2 = 0; // RE2 as output (VCC)
  120. TRISB = 0x00; // PORTB as output
  121. LATB = 0x00; // Initialize PORTB to 0 (all pins low)
  122.  
  123. // Initialize UART
  124. UART_Init();
  125.  
  126. // Initialize virtual ground and VCC
  127. gnd1;
  128. vcc;
  129.  
  130. while (1)
  131. {
  132. while (IR_PIN == 1); // Wait until IR pin falls (IR signal)
  133.  
  134. if (nec_remote_read())
  135. {
  136. // Send decoded data to serial monitor
  137. UART_Write_String("IR Code: 0x");
  138. for (int8_t i = 28; i >= 0; i -= 4)
  139. {
  140. uint8_t nibble = (ir_code >> i) & 0xF;
  141. UART_Write(nibble > 9 ? 'A' + (nibble - 10) : '0' + nibble);
  142. }
  143. UART_Write('\n');
  144.  
  145. // Check raw data and perform actions
  146. if (ir_code == 0x00FFA25D)
  147. {
  148. bulb1 = !bulb1; // Toggle bulb1 (RB0)
  149. UART_Write_String("Bulb1 toggled\n");
  150. }
  151. else if (ir_code == 0x00FF629D)
  152. {
  153. bulb2 = !bulb2; // Toggle bulb2 (RB2)
  154. UART_Write_String("Bulb2 toggled\n");
  155. }
  156. else if (ir_code == 0x00FFE21D)
  157. {
  158. fan = !fan; // Toggle fan (RB4)
  159. UART_Write_String("Fan toggled\n");
  160. }
  161. else if (ir_code == 0x00FF9867)
  162. {
  163. // Turn off all devices
  164. bulb1 = 0;
  165. bulb2 = 0;
  166. fan = 0;
  167. UART_Write_String("All devices turned off\n");
  168. }
  169.  
  170. __delay_ms(200); // Short delay for stability
  171. }
  172. }
  173. }
  174.  
Success #stdin #stdout 0.02s 25716KB
stdin
Standard input is empty
stdout
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>

// Configuration bits
#pragma config OSC = HS       // High-Speed Oscillator
#pragma config WDT = OFF      // Watchdog Timer disabled
#pragma config LVP = OFF      // Low-Voltage Programming disabled
#pragma config BOR = ON       // Brown-out Reset enabled
#pragma config PWRT = ON      // Power-up Timer enabled

#define _XTAL_FREQ 20000000 // External 20MHz crystal
#define IR_PIN PORTEbits.RE0 // IR receiver pin

// Define variables for output pins
#define bulb1 PORTBbits.RB0
#define bulb2 PORTBbits.RB2
#define fan PORTBbits.RB4

// Define variables for virtual ground/vcc pins
#define gnd1 PORTEbits.RE1 = 0;
#define vcc PORTEbits.RE2 = 1;

// Global variables for decoding the NEC protocol
uint32_t ir_code;

// UART initialization
void UART_Init()
{
    SPBRG = 129;          // Baud rate = 9600 for 20MHz crystal
    TXSTAbits.BRGH = 1;  // High-speed baud rate
    TXSTAbits.TXEN = 1;  // Enable transmission
    RCSTAbits.SPEN = 1;  // Enable UART module
    RCSTAbits.CREN = 1;  // Enable reception
}

// UART send a single character
void UART_Write(char data)
{
    while (!TXSTAbits.TRMT); // Wait for buffer to be empty
    TXREG = data;            // Send character
}

// UART send a string
void UART_Write_String(const char *str)
{
    while (*str)
    {
        UART_Write(*str++);
    }
}

// NEC protocol signal decoding
bool nec_remote_read()
{
    uint8_t count = 0, i;

    // Check 9ms pulse (remote sends logic high)
    while ((IR_PIN == 0) && (count < 200))
    {
        count++;
        __delay_us(50);
    }

    if ((count > 199) || (count < 160))
        return false;

    count = 0;

    // Check 4.5ms space (remote sends logic low)
    while ((IR_PIN == 1) && (count < 100))
    {
        count++;
        __delay_us(50);
    }
    
    if ((count > 99) || (count < 60))
        return false;

    // Read message (32 bits)
    ir_code = 0; // Reset ir_code before reading
    for (i = 0; i < 32; i++)
    {
        count = 0;
        while ((IR_PIN == 0) && (count < 14))
        {
            count++;
            __delay_us(50);
        }

        if ((count > 13) || (count < 8))
            return false;

        count = 0;

        while ((IR_PIN == 1) && (count < 40))
        {
            count++;
            __delay_us(50);
        }

        if ((count > 39) || (count < 8)) 
            return false;

        if (count > 20) // If space width > 1ms
            ir_code |= (1UL << (31 - i)); // Write 1 to bit (31 - i)
        else // If space width < 1ms
            ir_code &= ~(1UL << (31 - i)); // Write 0 to bit (31 - i)
    }

    return true;
}

void main(void)
{
    // Configure the system
    TRISEbits.TRISE0 = 1; // RE0 as input (IR receiver)
    TRISEbits.TRISE1 = 0; // RE1 as output (GND)
    TRISEbits.TRISE2 = 0; // RE2 as output (VCC)
    TRISB = 0x00;         // PORTB as output
    LATB = 0x00;          // Initialize PORTB to 0 (all pins low)

    // Initialize UART
    UART_Init();

    // Initialize virtual ground and VCC
    gnd1;
    vcc;

    while (1)
    {
        while (IR_PIN == 1); // Wait until IR pin falls (IR signal)

        if (nec_remote_read())
        {
            // Send decoded data to serial monitor
            UART_Write_String("IR Code: 0x");
            for (int8_t i = 28; i >= 0; i -= 4)
            {
                uint8_t nibble = (ir_code >> i) & 0xF;
                UART_Write(nibble > 9 ? 'A' + (nibble - 10) : '0' + nibble);
            }
            UART_Write('\n');

            // Check raw data and perform actions
            if (ir_code == 0x00FFA25D)
            {
                bulb1 = !bulb1; // Toggle bulb1 (RB0)
                UART_Write_String("Bulb1 toggled\n");
            }
            else if (ir_code == 0x00FF629D)
            {
                bulb2 = !bulb2; // Toggle bulb2 (RB2)
                UART_Write_String("Bulb2 toggled\n");
            }
            else if (ir_code == 0x00FFE21D)
            {
                fan = !fan; // Toggle fan (RB4)
                UART_Write_String("Fan toggled\n");
            }
            else if (ir_code == 0x00FF9867)
            {
                // Turn off all devices
                bulb1 = 0;
                bulb2 = 0;
                fan = 0;
                UART_Write_String("All devices turned off\n");
            }

            __delay_ms(200); // Short delay for stability
        }
    }
}