Question: I wrote this code to create a 1 0 hour delay on 8 0 5 1 microcontroller and everytime the timer overflows using interrupt increment

I wrote this code to create a 10 hour delay on 8051 microcontroller and everytime the timer overflows using interrupt increment an overflow counter and display that on 7 segment display in hexadecimal but i am not able to display it on that as i am getting ---- on all 4 digit bits. I have attached the image of proteus simulation too. Please give me the correct code and test it on proteus too on your side.
#include
#define TIMER_START_VALUE 0x03CB0// Timer start value for 50ms
#define MAX_COUNT 50633// Counter limit for 10-hour delay
//7-segment display mapping (common cathode)
unsigned char hex_to_7seg[]={
0xC0,//0
0xF9,//1
0xA4,//2
0xB0,//3
0x99,//4
0x92,//5
0x82,//6
0xF8,//7
0x80,//8
0x90,//9
0x88,// A
0x83,// B
0xC6,// C
0xA1,// D
0x86,// E
0x8E // F
};
// Port definitions
sbit DIGIT1= P1^0;
sbit DIGIT2= P1^1;
sbit DIGIT3= P1^2;
sbit DIGIT4= P1^3;
unsigned int counter =0; // Timer overflow counter
void Timer0_Init(void);
void displayHex(unsigned int value);
void delay_ms(unsigned int ms);
void Timer0_ISR(void);
void main(){
Timer0_Init(); // Initialize Timer 0
while (1){
if (counter >= MAX_COUNT){
TR0=0; // Stop the timer when MAX_COUNT is reached
while (1){
displayHex(counter); // Display final value continuously
}
}
displayHex(counter); // Continuously display the counter value
}
}
void Timer0_Init(void){
TMOD |=0x01; // Timer 0, Mode 1(16-bit timer)
TH0=(TIMER_START_VALUE >>8) & 0xFF; // Load high byte
TL0= TIMER_START_VALUE & 0xFF; // Load low byte
ET0=1; // Enable Timer 0 interrupt
EA =1; // Enable global interrupts
TR0=1; // Start Timer 0
}
void Timer0_ISR(void){
TH0=(TIMER_START_VALUE >>8) & 0xFF; // Reload high byte
TL0= TIMER_START_VALUE & 0xFF; // Reload low byte
counter++; // Increment counter
}
void displayHex(unsigned int value){
unsigned char digits[4];
unsigned char i;
// Extract each nibble from the 16-bit value
digits[0]=(value >>12) & 0xF;
digits[1]=(value >>8) & 0xF;
digits[2]=(value >>4) & 0xF;
digits[3]= value & 0xF;
// Sequentially display each digit
for (i =0; i 4; i++){
DIGIT1= DIGIT2= DIGIT3= DIGIT4=1; // Disable all digits
switch (i){
case 0: DIGIT1=0; break;
case 1: DIGIT2=0; break;
case 2: DIGIT3=0; break;
case 3: DIGIT4=0; break;
}
P2= hex_to_7seg[digits[i]]; // Output segment data
delay_ms(10); // Small delay
}
}
void delay_ms(unsigned int ms){
unsigned int i, j;
for (i =0; i ms; i++){
for (j =0; j 120; j++); // Approx 1ms delay at 12 MHz
}
}
I wrote this code to create a 1 0 hour delay on 8

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!