fork download
  1. // PIC18F4321 Configuration Bit Settings
  2. #pragma config OSC = HS // HS oscillator (8MHz crystal)
  3. #pragma config WDT = OFF // Watchdog Timer disabled
  4. #pragma config LVP = OFF // Low-Voltage Programming disabled
  5. #pragma config PBADEN = OFF // PortB<4:0> pins are configured as digital I/O on Reset
  6.  
  7. #include <xc.h>
  8.  
  9. #define _XTAL_FREQ 8000000 // 8 MHz Crystal
  10.  
  11. volatile unsigned int seconds = 0;
  12. volatile unsigned int minutes = 0;
  13. volatile unsigned int hours = 0;
  14.  
  15. void timer0_init() {
  16. T0CONbits.T08BIT = 0; // 16-bit Timer
  17. T0CONbits.T0CS = 0; // Internal instruction cycle clock (Fosc/4)
  18. T0CONbits.PSA = 0; // Prescaler assigned
  19. T0CONbits.T0PS = 0b110; // Prescaler 1:128
  20. TMR0H = 0x0B; // Preload high byte
  21. TMR0L = 0xDC; // Preload low byte (for 1 second interrupt approx.)
  22. INTCONbits.TMR0IE = 1; // Enable Timer0 interrupt
  23. INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag
  24. T0CONbits.TMR0ON = 1; // Turn on Timer0
  25. }
  26.  
  27. void interrupt isr() {
  28. if (INTCONbits.TMR0IF) {
  29. TMR0H = 0x0B; // Reload for next interrupt
  30. TMR0L = 0xDC;
  31. INTCONbits.TMR0IF = 0; // Clear interrupt flag
  32.  
  33. // Increment time
  34. seconds++;
  35. if (seconds >= 60) {
  36. seconds = 0;
  37. minutes++;
  38. if (minutes >= 60) {
  39. minutes = 0;
  40. hours++;
  41. if (hours >= 24) {
  42. hours = 0;
  43. }
  44. }
  45. }
  46. }
  47. }
  48.  
  49. void main() {
  50. // Configure interrupts
  51. INTCONbits.GIE = 1; // Enable Global Interrupts
  52. INTCONbits.PEIE = 1; // Enable Peripheral Interrupts
  53.  
  54. timer0_init();
  55.  
  56. while (1) {
  57. // Main loop does nothing; everything handled in interrupt
  58. // You could add LCD code here to display the time
  59. // For example: lcd_display(hours, minutes, seconds);
  60. }
  61. }
  62.  
Success #stdin #stdout 0.02s 25360KB
stdin
Standard input is empty
stdout
// PIC18F4321 Configuration Bit Settings
#pragma config OSC = HS     // HS oscillator (8MHz crystal)
#pragma config WDT = OFF    // Watchdog Timer disabled
#pragma config LVP = OFF    // Low-Voltage Programming disabled
#pragma config PBADEN = OFF // PortB<4:0> pins are configured as digital I/O on Reset

#include <xc.h>

#define _XTAL_FREQ 8000000 // 8 MHz Crystal

volatile unsigned int seconds = 0;
volatile unsigned int minutes = 0;
volatile unsigned int hours = 0;

void timer0_init() {
    T0CONbits.T08BIT = 0;   // 16-bit Timer
    T0CONbits.T0CS = 0;     // Internal instruction cycle clock (Fosc/4)
    T0CONbits.PSA = 0;      // Prescaler assigned
    T0CONbits.T0PS = 0b110; // Prescaler 1:128
    TMR0H = 0x0B;           // Preload high byte
    TMR0L = 0xDC;           // Preload low byte (for 1 second interrupt approx.)
    INTCONbits.TMR0IE = 1;  // Enable Timer0 interrupt
    INTCONbits.TMR0IF = 0;  // Clear Timer0 interrupt flag
    T0CONbits.TMR0ON = 1;   // Turn on Timer0
}

void interrupt isr() {
    if (INTCONbits.TMR0IF) {
        TMR0H = 0x0B;       // Reload for next interrupt
        TMR0L = 0xDC;
        INTCONbits.TMR0IF = 0; // Clear interrupt flag

        // Increment time
        seconds++;
        if (seconds >= 60) {
            seconds = 0;
            minutes++;
            if (minutes >= 60) {
                minutes = 0;
                hours++;
                if (hours >= 24) {
                    hours = 0;
                }
            }
        }
    }
}

void main() {
    // Configure interrupts
    INTCONbits.GIE = 1;   // Enable Global Interrupts
    INTCONbits.PEIE = 1;  // Enable Peripheral Interrupts

    timer0_init();

    while (1) {
        // Main loop does nothing; everything handled in interrupt
        // You could add LCD code here to display the time
        // For example: lcd_display(hours, minutes, seconds);
    }
}