Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I modify this code so I can get something like 01:05:879890 instead of g

ID: 651772 • Letter: H

Question

How can I modify this code so I can get something like 01:05:879890 instead of getting 00:65:879890. It cannot get my minutes working, is only doing it in seconds.

#include

int16 overflow_count;

#int_timer1

void timer1_isr(){

   overflow_count++;

}

void main(){

   int32 time;

setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);

   enable_interrupts(int_timer1);

   while(TRUE){

      enable_interrupts(global);

while(input(PUSH_BUTTON)); //Wait for press

      set_timer1(0);

      overflow_count=0;

      while(!input(PUSH_BUTTON)); //WAIT FOR RELEASE

      disable_interrupts(global);

      time=get_timer1();

      time=time+((int32)overflow_count<<16);

      time-=15; //substract overhead

      printf("Time is %02lu:%02lu.%06lu minutes. ",

      time/1000000000, (time/6000000), (time/5)%1000000);

   }

}

Explanation / Answer

Before printing the statement, try to check the seconds, minutes also.

If you want the seconds to be of 60 seconds running, then first check it as,

if the value of seconds is greater than 60 seconds, subtract 60 from the obtained seconds and add it to minutes values.

Similarly, if the minutes is >=60 minutes, then need to subtract 60 from the value and asign the remaining value into the minutes and add 1 hour to the hours value.

If the minutes value crosses the 120 seconds, then use the formula as (obtained minutes value)/60.

Depending on the value multiply that many times to 60 and subtract the value from the original.

Example,

1:85:2345

Hours=1

minutes=85

seconds=23

First check seconds if seconds needed to be changed when it is greater than the 60.

Here, it is lesser than 60.

So, now get into minutes,

if(minutes>=60)

{

              int mult=minutes/60;

              int trans_minutes=60*mult;

             int modified_minutes=minutes-trans_minutes;

             hours=hours+mult;

}           

printf("hours: minutes: seconds = %d:%d:%d ", hours, modified_minutes, seconds);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote