Project to produce a 10 hour time delay using 8051 microcontroller

MrAl

Joined Jun 17, 2014
13,716
XTAL frequency is 11.0592Mhz 8051 microcontroller and i have posted the code for my program just above in the thread if possible can you take a look and point out if any errors are there
Hi,

What is the clock frequency that drives the timer?
Is it around 1MHz?
 

Thread Starter

Cyfer_135

Joined Nov 14, 2024
14
hi, @Cyfer_135 please use the Code display option, shown on the top menu bar. Moderation

C-like:
#include <reg51.h>  // Use the correct header file for 8051



// Segment codes for 0-9 for 7-segment display (common cathode)


const unsigned char segmentCodes[10] = {


    0x3F, // 0


    0x06, // 1


    0x5B, // 2


    0x4F, // 3


    0x66, // 4


    0x6D, // 5


    0x7D, // 6


    0x07, // 7


    0x7F, // 8


    0x6F  // 9


};



// Timer 1 overflow delay function


void timer1Delay() {


    TH1 = 0x00;     // Set Timer 1 high byte for full count


    TL1 = 0x00;     // Set Timer 1 low byte for full count


    TF1 = 0;        // Clear overflow flag


    TR1 = 1;        // Start Timer 1



    while (TF1 == 0);  // Wait until overflow occurs


    TR1 = 0;        // Stop Timer 1


    TF1 = 0;        // Clear overflow flag again


}



// Function to display count on a 4-digit 7-segment display


void displayProgress(int count) {


    int digit1, digit2, digit3, digit4;


    int i, delay;



    // Extract each digit from the count


    digit1 = count % 10;            // Units place


    digit2 = (count / 10) % 10;     // Tens place


    digit3 = (count / 100) % 10;    // Hundreds place


    digit4 = (count / 1000) % 10;   // Thousands place



    // Multiplex each digit for a brief period


    for (i = 0; i < 4; i++) {


        switch (i) {


            case 0:


                P2 = segmentCodes[digit1]; // Send units digit pattern to port


                P3 = 0x01;                 // Enable units place (digit 1)


                break;


            case 1:


                P2 = segmentCodes[digit2]; // Send tens digit pattern to port


                P3 = 0x02;                 // Enable tens place (digit 2)


                break;


            case 2:


                P2 = segmentCodes[digit3]; // Send hundreds digit pattern to port


                P3 = 0x04;                 // Enable hundreds place (digit 3)


                break;


            case 3:


                P2 = segmentCodes[digit4]; // Send thousands digit pattern to port


                P3 = 0x08;                 // Enable thousands place (digit 4)


                break;


        }



        // Brief delay to hold the display


        for (delay = 0; delay < 1000; delay++);



        P3 = 0x00;  // Turn off all digits before next iteration


    }


}



void main() {


    int overflowCount = 0;      // Counts up to 1000 overflows


    int progressCounter = 0;    // Progress counter to display



    TMOD = 0x10;    // Set Timer 1 to Mode 1 (16-bit timer mode)



    while (1) {


        // Call timer delay function


        timer1Delay();


        overflowCount++;



        // Check if overflowCount reached 1000 (approximately 71.58 seconds)


        if (overflowCount >= 1000) {


            overflowCount = 0;           // Reset overflow counter


            progressCounter++;           // Increment progress counter


            displayProgress(progressCounter); // Update 7-segment display



            // If 600 increments have been reached, we’ve simulated 10 hours


            if (progressCounter >= 600) {


                progressCounter = 0;  // Reset or take any required action


                // Optionally add a signal here to indicate completion


            }


        }


    }


}
This is the code but it's not working can anyone help me debug or give me a working code as i don't have much time in evaluation.
 
Last edited by a moderator:

MrAl

Joined Jun 17, 2014
13,716
hi, @Cyfer_135 please use the Code display option, shown on the top menu bar. Moderation

C-like:
#include <reg51.h>  // Use the correct header file for 8051



// Segment codes for 0-9 for 7-segment display (common cathode)


const unsigned char segmentCodes[10] = {


    0x3F, // 0


    0x06, // 1


    0x5B, // 2


    0x4F, // 3


    0x66, // 4


    0x6D, // 5


    0x7D, // 6


    0x07, // 7


    0x7F, // 8


    0x6F  // 9


};



// Timer 1 overflow delay function


void timer1Delay() {


    TH1 = 0x00;     // Set Timer 1 high byte for full count


    TL1 = 0x00;     // Set Timer 1 low byte for full count


    TF1 = 0;        // Clear overflow flag


    TR1 = 1;        // Start Timer 1



    while (TF1 == 0);  // Wait until overflow occurs


    TR1 = 0;        // Stop Timer 1


    TF1 = 0;        // Clear overflow flag again


}



// Function to display count on a 4-digit 7-segment display


void displayProgress(int count) {


    int digit1, digit2, digit3, digit4;


    int i, delay;



    // Extract each digit from the count


    digit1 = count % 10;            // Units place


    digit2 = (count / 10) % 10;     // Tens place


    digit3 = (count / 100) % 10;    // Hundreds place


    digit4 = (count / 1000) % 10;   // Thousands place



    // Multiplex each digit for a brief period


    for (i = 0; i < 4; i++) {


        switch (i) {


            case 0:


                P2 = segmentCodes[digit1]; // Send units digit pattern to port


                P3 = 0x01;                 // Enable units place (digit 1)


                break;


            case 1:


                P2 = segmentCodes[digit2]; // Send tens digit pattern to port


                P3 = 0x02;                 // Enable tens place (digit 2)


                break;


            case 2:


                P2 = segmentCodes[digit3]; // Send hundreds digit pattern to port


                P3 = 0x04;                 // Enable hundreds place (digit 3)


                break;


            case 3:


                P2 = segmentCodes[digit4]; // Send thousands digit pattern to port


                P3 = 0x08;                 // Enable thousands place (digit 4)


                break;


        }



        // Brief delay to hold the display


        for (delay = 0; delay < 1000; delay++);



        P3 = 0x00;  // Turn off all digits before next iteration


    }


}



void main() {


    int overflowCount = 0;      // Counts up to 1000 overflows


    int progressCounter = 0;    // Progress counter to display



    TMOD = 0x10;    // Set Timer 1 to Mode 1 (16-bit timer mode)



    while (1) {


        // Call timer delay function


        timer1Delay();


        overflowCount++;



        // Check if overflowCount reached 1000 (approximately 71.58 seconds)


        if (overflowCount >= 1000) {


            overflowCount = 0;           // Reset overflow counter


            progressCounter++;           // Increment progress counter


            displayProgress(progressCounter); // Update 7-segment display



            // If 600 increments have been reached, we’ve simulated 10 hours


            if (progressCounter >= 600) {


                progressCounter = 0;  // Reset or take any required action


                // Optionally add a signal here to indicate completion


            }


        }


    }


}
This is the code but it's not working can anyone help me debug or give me a working code as i don't have much time in evaluation.
But the timer is not running at 11MHz is it?
There must be a divider between that and the timer input clock, right?
You'll have to confirm this or figure out what it is or look it up. We need to know this. You may have measured this already, so just state whatever you have measured. For example, is that 71 seconds in your code correct?
 
Top