Start PICSimLab. Select PIC16F877A as microcontroller using the microcontroller tab. Select McLab2. Select 1MHzclock. The schematic of board McLab2 is available at PICSimLab→ Helptab → Contentstab → English Manuallink → Features of Board 2link → Board 2 Schematicslink.Configure the microcontroller through your code as follows:__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _CP_OFFProgram the microcontroller so that the prescaler of the Timer 0will be 1:256and L4LEDon the board will be turned ON and OFF every 250milliseconds.During this alteration time, when the L4 LED is open display the digitsstarting from 0 to 9by using 7-segmentdisplay method.(L4LED will be ON for 250msand digits will be displayed, after that it will OFF for 250msand nothing will be displayed. The process should continue until all digits are displayed.).



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

}

Other Questions