fork download
  1. #include <xc.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4.  
  5. // Configuration bits
  6. #pragma config FOSC = FRC // Internal oscillator
  7. #pragma config FWDTEN = OFF // Watchdog timer disabled
  8.  
  9. // Define LED pins
  10. #define RED_LED LATBbits.LATB0
  11. #define WHITE_LED LATBbits.LATB1
  12. #define GREEN_LED LATBbits.LATB2
  13. #define POWER_BUTTON PORTAbits.RA0
  14.  
  15. // Function prototypes
  16. void initializeIO(void);
  17. void initializeTimer(void);
  18.  
  19. volatile bool powerOn = true; // State of power button
  20.  
  21. void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
  22. static bool greenLEDState = false;
  23. IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
  24.  
  25. // Toggle green LED every 2 seconds
  26. greenLEDState = !greenLEDState;
  27. GREEN_LED = greenLEDState;
  28. }
  29.  
  30. void initializeIO(void) {
  31. // Set LEDs as outputs
  32. TRISBbits.TRISB0 = 0; // Red LED
  33. TRISBbits.TRISB1 = 0; // White LED
  34. TRISBbits.TRISB2 = 0; // Green LED
  35.  
  36. // Set button as input
  37. TRISAbits.TRISA0 = 1;
  38.  
  39. // Initial LED states
  40. RED_LED = 1; // Power ON state
  41. WHITE_LED = 0;
  42. GREEN_LED = 0;
  43. }
  44.  
  45. void initializeTimer(void) {
  46. T1CON = 0x8030; // Timer1 ON, prescaler 1:256
  47. PR1 = 15625; // For 2-second delay (assuming 4 MHz clock)
  48. IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
  49. IEC0bits.T1IE = 1; // Enable Timer1 interrupt
  50. }
  51.  
  52. int main(void) {
  53. initializeIO();
  54. initializeTimer();
  55.  
  56. while (1) {
  57. // Poll the power button
  58. if (POWER_BUTTON == 1) {
  59. __delay_ms(50); // Debounce delay
  60. if (POWER_BUTTON == 1) {
  61. powerOn = !powerOn; // Toggle power state
  62. __delay_ms(500); // Button hold delay
  63. }
  64. }
  65.  
  66. if (powerOn) {
  67. RED_LED = 1;
  68. WHITE_LED = 0;
  69. } else {
  70. RED_LED = 0;
  71. WHITE_LED = 1;
  72. GREEN_LED = 0; // Ensure green LED is off when powered down
  73. }
  74. }
  75.  
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0.03s 25640KB
stdin
Standard input is empty
stdout
#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;
}