-4

Suppose, I have a motor. I want to count the amount of time the motor runs daily and at the end of the month count the total time the motor ran. How can I do this using an ATmega 32?

1000 Bites
  • 1,010
  • 9
  • 9
  • 2
    `How it should be coded` With some code, probably. Welcome to the site! This is a bad question. I recommend you read [the entire Help Center](http://stackoverflow.com/help) and head back when you have a specific, programming-related question. – admdrew Aug 11 '14 at 18:36
  • Depends. What controls the motor? How do you "sense" when the motor runs? How do you convey the count to the user? Should it be number of times it starts/stops, or total time it runs? – Drew McGowen Aug 11 '14 at 18:36
  • Does the ATmega32 have a RTC? – Fiddling Bits Aug 11 '14 at 18:39
  • 1
    `How it should be coded` Very carefully. – Fiddling Bits Aug 11 '14 at 18:40

1 Answers1

3

There are two methods to determine time in an embedded system:

  1. Counting system ticks.
  2. Reading a Real Time Clock (RTC).

Counting System Ticks

If your system has a timer interrupt, read the number of counts before starting the motor and reading the counts after. Subtract the two values and convert to the time base of your choice.

You may need to modify the timer interrupt handler to use a larger data type, depending on the frequency of the interrupt.

Reading a Real Time Clock

If your embedded system has an RTC, then read the appropriate registers before starting the motor and read them after. Subract accordingly to get your duration. Convert duration to the time base of your choice.

The RTC may be external to the System On a Chip (SOC) or there may be one on there. You have the details, I don't.

Counting Motor Checks

Create two integral variables, one for motor times on and the other for motor times off.

You could modify the timer ISR to check the status of the motor.

When the ISR determines the motor is on, increment the motor on time.
When the ISR determines the motor is off, increment the motor off time.

This method only counts the total time the motor is on and the total time the motor is off. It is not as accurate as reading the system time before starting the motor and after stopping the motor.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154