Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me with my coding in timer display in LCD..

Status
Not open for further replies.

UyAb

Junior Member level 2
Junior Member level 2
Joined
Mar 6, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Tacloban City, Philippines
Visit site
Activity points
1,416
this is my code..my problem with this is..for example,if my initial time is 1:15..
it will countdown like this..
1:15,1:14,1:13...1:10,1:05,1:04,1:03...1:00,0:15..
please help me change my coding..
Code:
void display_time()
{
 Lcd_Chr(2,4,h+48);
 Lcd_Chr(2,5,':');
 Lcd_Chr(2,6,m2+48);
 Lcd_Chr(2,7,m1+48);
 Lcd_Chr(2,8,':');
 Lcd_Chr(2,9,s2+48);
 Lcd_Chr(2,10,s1+48);
}

void countdown_timer()
{
 Lcd_Cmd(Lcd_Clear);
 Lcd_Out(1,1,"Time Left:");
 Lcd_Out(2,12,"H:M:S");
 
 for(h=code[0]-48;h>=0;h--) 
 {display_time();                   
   for(m2=code[1]-48;m2>=0;m2--)      
   {display_time();
     for(m1=code[2]-49;m1>=0;m1--)
     {display_time();
      for(s2=5;s2>=0;s2--)
      {display_time();
       for(s1=9;s1>=0;s1--)
       {display_time();
        if(m1<0)
        {s2=0;s1=0;}
        delay_ms(1000);
       }
      }
     }
   }
 }
}
 

Hello!

1. First try a few values to be sure that your display is good:
ex: h = 11, m = 35, s = 18
display_time();
And repeat this until the time display is right.
Once you have a working function, your code should look like this:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
main(void) {
    // Variables declarations, etc...
    while((h > 0) && (m > 0) && (s > 0)) {
        s--;
        // If the seconds variable is negative, then we have to decrement minutes
        if(s < 0) {
            s = 59;
            m--;
            // If the minutes variable is negative, then we have to decrement hours
            if(m < 0) {
                m = 59;
                h--;
            }
        }
        display_time();
        delay_ms(1000);
    }
}



But this code is not good enough: the duration of one loop will be 1000ms (the value of your delay)
but the processing time (calculation of the new time and display) will be added to that, so the real
delay will be more.
Therefore: you should use a timer and a timer interrupt.

Dora.
 
  • Like
Reactions: UyAb

    UyAb

    Points: 2
    Helpful Answer Positive Rating
wait,i can't understand what you mean..can you please write it down in codes so i can easily understand?if you wont mind..sorry for being such a noob..i'm just a beginner in programming..

- - - Updated - - -

wait,i can't understand what you mean..can you please write it down in codes so i can easily understand?if you wont mind..sorry for being such a noob..i'm just a beginner in programming..
 

Why do you split the minutes and seconds to m1, m2, s1, s2. Can't you just m and s and do m-- and s-- and convert the value of m and s to string and display it on LCD?
wait,i can't understand what you mean..can you please write it down in codes so i can easily understand?if you wont mind..sorry for being such a noob..i'm just a beginner in programming..
 

doraemon has already shown that. You are using m1 and m2 for minutes and s1 and s2 for seconds. I said used m and s for minutes and seconds and decrement it to 0 starting from 59.
 

Hello!

wait,i can't understand what you mean..can you please write it down in codes so i can easily understand?if you wont mind..sorry for being such a noob..i'm just a beginner in programming..

Let's be didactic, here is an exercise:

1. Write a function void display_time(uint8 h, uint8 m, uint8 s) that can write any time
between 00:00:00 and 23:59:59
-> Try if this function works for various times, especially 1-digit and 2 digits.
Example:
h = 1, m = 1, s = 1
h = 10, m = 10, s = 10
Don't try to go to the next step if this function does not fully work.

2. Take the code I have already written above, declare the variables, and replace display_time()
by display_time(h, m, s)

This should work. Not accurately because of the timing problem, but this should work.

Dora.
 
  • Like
Reactions: UyAb

    UyAb

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top