Simple maths?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I really should be able to get this working, but embarrasingly I can't get it right. I am a total hobbyist when it comes to programming, and maths obviously.

I am using an Atmega16 with a HD44780 LCD screen. I need it to count down time in hours minutes and seconds. I have the overflow timer working fine and can write the time to the screen as the seconds go by. But I just can't get a simple formula working right so that it counts down 60 seconds to the minute and 60 minutes to the hour. The time that it will count down could be up to 99 hours and I need to know when it hits zero. Can someone help me with this simple maths? Thanks in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This seems to yield decent results with -Os turned on:

#include 

volatile unsigned long ElapsedSeconds = 0;

int main(void)
{
	unsigned char Hours;
	unsigned char Minutes;
	unsigned char Seconds;

	Hours   = ElapsedSeconds / (60 * 60);
	Minutes = (ElapsedSeconds % (60 * 60)) / 60;
	Seconds = (ElapsedSeconds % (60 * 60)) % 60;
}

That will translate the absolute seconds in "ElapsedSeconds" into hours, minutes and seconds. Run those variables through itoa to get their ASCII equivalents, and display that onto your LCD. To count down, use (TotalSeconds - ElapsedSeconds) instead.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for that Dean, a much simpler way than trying to keep track of 3 variables, for hours, minutes and seconds. I need to be able to store the value for ElapsedSeconds in the eeprom. Any ideas how I store a long in eeprom? I've only stored bytes and words using eeprom.h so far.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use eeprom_write_block - see my tutorial in the tutorials section about the EEMEM attribute.

1) Don't forget to convert the hours/minutes/seconds over to ASCII for display via itoa - otherwise you'll be displaying gibberish

2) Don't forget that the EEPROM has a limited lifespan of 100,000 writes, thus write cycles should be limited to only when absolutely nessesary

As for the maths to the above. Hours is the integer division of the number of seconds elapsed by the number of seconds in an hour (60 * 60). Minutes is the integer division of the remaining seconds in the current hour (via modulus) by the number of seconds in a minute. Seconds in the modulus of the number of remaining seconds in the current hour by the number of seconds in a minute.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Alternatively, if you do want to go the three-variable route:

volatile unsigned char Hours;
volatile unsigned char Minutes;
volatile unsigned char Seconds;

void IncrementTime(void)
{
  Seconds++;

  Mintes  += (Seconds / 60);
  Seconds %= 60;

  Hours   += (Minutes / 60);
  Minutes %= 60;
}

Might end up smaller than all the long variable manipulations, but then you'd have to convert to an absolute value (total seconds elapsed) to get a countdown.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks again for the help and tips Dean. I have used your eeprom tutorial before and have found it extremely useful. I'll have a look at using eeprom_write_block. 1) With regards to the display, I am using itoa conversion and it is working pefectly.
2) The eeprom would not be written to very often. It is basically a programmable count down timer, once the times are set they would rarely be changed.

And thanks for the maths explanation. The % bits threw me, but I get the idea. Much appreciated.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, not many people use the modulus operator. It returns the remainder from the division:

10 / 5 = 2
10 % 5 = 0

10 / 6 = 1
10 % 6 = 4

Great to know it's all sorted out.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!