Answer :
Answer:
#include <xc.h>
#define _XTAL_FREQ 1000000 // 1 MHz clock frequency
// Configuration bits
__CONFIG(FOSC_XT & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & CP_OFF);
// Port definitions
#define L4_LED PORTCbits.RC2
#define SEGMENT_PORT PORTB
// Function prototypes
void initialize(void);
void display_digit(unsigned char digit);
// Array to store the segment patterns for digits 0-9
const unsigned char segment_patterns[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void main(void) {
unsigned char digit = 0;
initialize();
while (1) {
L4_LED = 1; // Turn on L4 LED
display_digit(digit); // Display the current digit
__delay_ms(250); // Delay for 250 ms
L4_LED = 0; // Turn off L4 LED
SEGMENT_PORT = 0; // Clear the 7-segment display
__delay_ms(250); // Delay for 250 ms
digit++;
if (digit > 9) {
digit = 0; // Reset the digit to 0 after 9
}
}
}
void initialize(void) {
TRISC2 = 0; // Set RC2 (L4 LED) as output
TRISB = 0; // Set PORTB as output for 7-segment display
// Configure Timer0
OPTION_REGbits.T0CS = 0; // Select internal instruction cycle clock
OPTION_REGbits.PSA = 0; // Assign prescaler to Timer0
OPTION_REGbits.PS = 0b111; // Set prescaler to 1:256
}
void display_digit(unsigned char digit) {
SEGMENT_PORT = segment_patterns[digit]; // Set the segment pattern for the digit
}