fork download
  1. // Include the XC8 library for PIC microcontroller functions
  2. #include <xc.h>
  3. #define _XTAL_FREQ 4000000 // Define the crystal oscillator frequency to enable proper delay calculations
  4.  
  5. // Pin Definitions
  6. #define BUZZER RB0 // Assign the buzzer to RB0 pin for alarm activation
  7. #define SNOOZE_BUTTON RB1 // Assign the snooze button to RB1 pin to allow snoozing the alarm
  8. #define MOTOR_STEP RA0
  9. #define LCD_RS RD0
  10. #define LCD_EN RD1
  11. #define LCD_D4 RD2
  12. #define LCD_D5 RD3
  13. #define LCD_D6 RD4
  14. #define LCD_D7 RD5
  15.  
  16. // Function Prototypes
  17. void LCD_Init();
  18. void LCD_Command(unsigned char); // Send a command to the LCD for controlling its operations
  19. void LCD_Char(unsigned char); // Send a character to the LCD for display
  20. void LCD_String(const char* str); // Display a string on the LCD
  21. void Stepper_Move(); // Move the stepper motor to create the alarm movement effect
  22. void Alarm_Trigger(); // Trigger the alarm by turning on the buzzer and moving the stepper motor
  23. void Snooze_Function(); // Implement snooze functionality by turning off the buzzer temporarily
  24.  
  25. void main() {
  26. TRISB = 0x02; // Set RB1 as input for snooze button and RB0 as output for buzzer
  27. TRISA = 0x01; // Set RA0 as output for stepper motor control
  28. TRISD = 0x00; // LCD as output
  29.  
  30. LCD_Init();
  31.  
  32. while (1) {
  33. // Simulated alarm condition (replace with RTC check in real implementation)
  34. if (/* Time Matched with Alarm Time */) {
  35. Alarm_Trigger();
  36. }
  37.  
  38. if (SNOOZE_BUTTON == 1) {
  39. Snooze_Function();
  40. }
  41. }
  42. }
  43.  
  44. void LCD_Init() {
  45. // Initialize the LCD in 4-bit mode for efficient communication
  46. LCD_Command(0x02);
  47. LCD_Command(0x28);
  48. LCD_Command(0x0C);
  49. LCD_Command(0x06);
  50. LCD_Command(0x01);
  51. __delay_ms(2);
  52. }
  53.  
  54. void LCD_Command(unsigned char cmd) {
  55. PORTD = (cmd & 0xF0);
  56. LCD_RS = 0;
  57. LCD_EN = 1;
  58. __delay_ms(1);
  59. LCD_EN = 0;
  60. PORTD = ((cmd << 4) & 0xF0);
  61. LCD_EN = 1;
  62. __delay_ms(1);
  63. LCD_EN = 0;
  64. }
  65.  
  66. void LCD_Char(unsigned char data) {
  67. PORTD = (data & 0xF0);
  68. LCD_RS = 1;
  69. LCD_EN = 1;
  70. __delay_ms(1);
  71. LCD_EN = 0;
  72. PORTD = ((data << 4) & 0xF0);
  73. LCD_EN = 1;
  74. __delay_ms(1);
  75. LCD_EN = 0;
  76. }
  77.  
  78. void LCD_String(const char* str) {
  79. while (*str) {
  80. LCD_Char(*str++);
  81. }
  82. }
  83.  
  84. void Stepper_Move() {
  85. for (int i = 0; i < 50; i++) {
  86. MOTOR_STEP = 1;
  87. __delay_ms(10);
  88. MOTOR_STEP = 0;
  89. __delay_ms(10);
  90. }
  91. }
  92.  
  93. void Alarm_Trigger() {
  94. LCD_Command(0x01);
  95. LCD_String("ALARM TRIGGERED!");
  96. BUZZER = 1;
  97. Stepper_Move();
  98. }
  99.  
  100. void Snooze_Function() {
  101. LCD_Command(0x01);
  102. LCD_String("SNOOZING...");
  103. BUZZER = 0;
  104. __delay_ms(5000); // Snooze for 5 seconds
  105. }
Success #stdin #stdout 0.04s 25524KB
stdin
Standard input is empty
stdout
// Include the XC8 library for PIC microcontroller functions
#include <xc.h>
#define _XTAL_FREQ 4000000  // Define the crystal oscillator frequency to enable proper delay calculations

// Pin Definitions
#define BUZZER RB0  // Assign the buzzer to RB0 pin for alarm activation
#define SNOOZE_BUTTON RB1  // Assign the snooze button to RB1 pin to allow snoozing the alarm
#define MOTOR_STEP RA0
#define LCD_RS RD0
#define LCD_EN RD1
#define LCD_D4 RD2
#define LCD_D5 RD3
#define LCD_D6 RD4
#define LCD_D7 RD5

// Function Prototypes
void LCD_Init();
void LCD_Command(unsigned char); // Send a command to the LCD for controlling its operations
void LCD_Char(unsigned char); // Send a character to the LCD for display
void LCD_String(const char* str); // Display a string on the LCD
void Stepper_Move(); // Move the stepper motor to create the alarm movement effect
void Alarm_Trigger(); // Trigger the alarm by turning on the buzzer and moving the stepper motor
void Snooze_Function(); // Implement snooze functionality by turning off the buzzer temporarily

void main() {
    TRISB = 0x02; // Set RB1 as input for snooze button and RB0 as output for buzzer
    TRISA = 0x01; // Set RA0 as output for stepper motor control
    TRISD = 0x00; // LCD as output
    
    LCD_Init();
    
    while (1) {
        // Simulated alarm condition (replace with RTC check in real implementation)
        if (/* Time Matched with Alarm Time */) {
            Alarm_Trigger();
        }
        
        if (SNOOZE_BUTTON == 1) {
            Snooze_Function();
        }
    }
}

void LCD_Init() {
    // Initialize the LCD in 4-bit mode for efficient communication
    LCD_Command(0x02);
    LCD_Command(0x28);
    LCD_Command(0x0C);
    LCD_Command(0x06);
    LCD_Command(0x01);
    __delay_ms(2);
}

void LCD_Command(unsigned char cmd) {
    PORTD = (cmd & 0xF0);
    LCD_RS = 0;
    LCD_EN = 1;
    __delay_ms(1);
    LCD_EN = 0;
    PORTD = ((cmd << 4) & 0xF0);
    LCD_EN = 1;
    __delay_ms(1);
    LCD_EN = 0;
}

void LCD_Char(unsigned char data) {
    PORTD = (data & 0xF0);
    LCD_RS = 1;
    LCD_EN = 1;
    __delay_ms(1);
    LCD_EN = 0;
    PORTD = ((data << 4) & 0xF0);
    LCD_EN = 1;
    __delay_ms(1);
    LCD_EN = 0;
}

void LCD_String(const char* str) {
    while (*str) {
        LCD_Char(*str++);
    }
}

void Stepper_Move() {
    for (int i = 0; i < 50; i++) {
        MOTOR_STEP = 1;
        __delay_ms(10);
        MOTOR_STEP = 0;
        __delay_ms(10);
    }
}

void Alarm_Trigger() {
    LCD_Command(0x01);
    LCD_String("ALARM TRIGGERED!");
    BUZZER = 1;
    Stepper_Move();
}

void Snooze_Function() {
    LCD_Command(0x01);
    LCD_String("SNOOZING...");
    BUZZER = 0;
    __delay_ms(5000); // Snooze for 5 seconds
}