#include <xc.h> #include <stdint.h> #include <stdbool.h> // Configuration bits #pragma config FOSC = FRC // Internal oscillator #pragma config FWDTEN = OFF // Watchdog timer disabled // Define LED pins #define RED_LED LATBbits.LATB0 #define WHITE_LED LATBbits.LATB1 #define GREEN_LED LATBbits.LATB2 #define POWER_BUTTON PORTAbits.RA0 // Function prototypes void initializeIO(void); void initializeTimer(void); volatile bool powerOn = true; // State of power button void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) { static bool greenLEDState = false; IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag // Toggle green LED every 2 seconds greenLEDState = !greenLEDState; GREEN_LED = greenLEDState; } void initializeIO(void) { // Set LEDs as outputs TRISBbits.TRISB0 = 0; // Red LED TRISBbits.TRISB1 = 0; // White LED TRISBbits.TRISB2 = 0; // Green LED // Set button as input TRISAbits.TRISA0 = 1; // Initial LED states RED_LED = 1; // Power ON state WHITE_LED = 0; GREEN_LED = 0; } void initializeTimer(void) { T1CON = 0x8030; // Timer1 ON, prescaler 1:256 PR1 = 15625; // For 2-second delay (assuming 4 MHz clock) IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag IEC0bits.T1IE = 1; // Enable Timer1 interrupt } int main(void) { initializeIO(); initializeTimer(); while (1) { // Poll the power button if (POWER_BUTTON == 1) { __delay_ms(50); // Debounce delay if (POWER_BUTTON == 1) { powerOn = !powerOn; // Toggle power state __delay_ms(500); // Button hold delay } } if (powerOn) { RED_LED = 1; WHITE_LED = 0; } else { RED_LED = 0; WHITE_LED = 1; GREEN_LED = 0; // Ensure green LED is off when powered down } } return 0; }
Standard input is empty
#include <xc.h> #include <stdint.h> #include <stdbool.h> // Configuration bits #pragma config FOSC = FRC // Internal oscillator #pragma config FWDTEN = OFF // Watchdog timer disabled // Define LED pins #define RED_LED LATBbits.LATB0 #define WHITE_LED LATBbits.LATB1 #define GREEN_LED LATBbits.LATB2 #define POWER_BUTTON PORTAbits.RA0 // Function prototypes void initializeIO(void); void initializeTimer(void); volatile bool powerOn = true; // State of power button void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) { static bool greenLEDState = false; IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag // Toggle green LED every 2 seconds greenLEDState = !greenLEDState; GREEN_LED = greenLEDState; } void initializeIO(void) { // Set LEDs as outputs TRISBbits.TRISB0 = 0; // Red LED TRISBbits.TRISB1 = 0; // White LED TRISBbits.TRISB2 = 0; // Green LED // Set button as input TRISAbits.TRISA0 = 1; // Initial LED states RED_LED = 1; // Power ON state WHITE_LED = 0; GREEN_LED = 0; } void initializeTimer(void) { T1CON = 0x8030; // Timer1 ON, prescaler 1:256 PR1 = 15625; // For 2-second delay (assuming 4 MHz clock) IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag IEC0bits.T1IE = 1; // Enable Timer1 interrupt } int main(void) { initializeIO(); initializeTimer(); while (1) { // Poll the power button if (POWER_BUTTON == 1) { __delay_ms(50); // Debounce delay if (POWER_BUTTON == 1) { powerOn = !powerOn; // Toggle power state __delay_ms(500); // Button hold delay } } if (powerOn) { RED_LED = 1; WHITE_LED = 0; } else { RED_LED = 0; WHITE_LED = 1; GREEN_LED = 0; // Ensure green LED is off when powered down } } return 0; }