// 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); } }
Standard input is empty
// 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);
}
}