#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() { // Check 9ms pulse (remote sends logic high) { count++; __delay_us(50); } return false; // Check 4.5ms space (remote sends logic low) { count++; __delay_us(50); } return false; // Read message (32 bits) ir_code = 0; // Reset ir_code before reading for (i = 0; i < 32; i++) { { count++; __delay_us(50); } return false; { count++; __delay_us(50); } return false; 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 } } }
Standard input is empty
#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
}
}
}