# time.c File Reference

## Detailed Description

Functions for timing.

Contains functions to initialize, set, poll and stop timers.

Application note:
  AVR458: Charging Li-Ion Batteries with BC100
  AVR463: Charging NiMH Batteries with BC100

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

$Name$

Revision
  2299

$RCSfile$

URL
  http://revisor.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/trunk/code/IAR/time.c

Date
  2007-08-23 12:55:51 +0200 (to, 23 aug 2007)

Definition in file time.c.

`#include <ioavr.h>`
`#include <inavr.h>`
`#include "enums.h"`
`#include "main.h"`
`#include "time.h"`

Include dependency graph for time.c:

Go to the source code of this file.

## Functions
__interrupt void | TICK_ISR (void)
| Interrupt service routine for timer 0 overflow.
void | Time_Init (void)
| Initializes timers.
unsigned char | Time_Left (unsigned char timer)
| Checks if a specified timer has expired.
void | Time_Set (unsigned char timer, unsigned int min, unsigned char sec, unsigned char ms)
| Sets the specified timer.
void | Time_Start (void)
| Starts timers.
void | Time_Stop (void)
| Stops timers.
## Variables
unsigned long | timeval [TIMERS]
| Contains the values for each timer.

## Function Documentation

__interrupt void TICK_ISR | ( | void |  | ) |

Interrupt service routine for timer 0 overflow.

Timer 0 runs at 125 kHz and compare match will occur every millisecond (125 / 125 kHz = 1.0 ms), which will result in a call to this function. When called, this function will decrement the time left for each timer, unless they are already at zero.

Definition at line 58 of file time.c.

References TIMERS, and timeval.

    00059 {
    00060         unsigned char i;
    00061
    00062         // 1 ms has passed, decrement all non-zero timers.
    00063         for (i = 0; i < TIMERS; i++) {
    00064                 if(timeval[i] > 0) {
    00065                         timeval[i]--;
    00066                 }
    00067         }
    00068 }

void Time_Init | ( | void |  | ) |

Initializes timers.

Resets all the timer values to 0, then sets up timer 0 for a compare match every millisecond.

Definition at line 134 of file time.c.

References TIMERS, and timeval.

Referenced by Initialize().

    00135 {
    00136         unsigned char i;
    00137
    00138         for (i = 0; i<<TIMERS; i++)     {
    00139                 timeval[i] = 0;
    00140         }
    00141
    00142         //    OCR0A = 0;  // Doesn't matter, will run in normal mode.
    00143
    00144         OCR0A = 125;  // Will give a compare match every ms.
    00145
    00146         OCR0B = 0;  // Doesn't matter, will run in normal mode.
    00147
    00148                 //    TCCR0A = 0;  // Normal 8-bit mode, no input capture.
    00149
    00150         TCCR0A = (1<<WGM00);  // 8-bit CTC mode.
    00151
    00152         //    TCCR0B = (0<<CS02)|(1<<CS01)|(0<<CS00);  // CLKT0 = CLK/8 = 1 MHz.
    00153
    00154         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.
    00155
    00156         //    TIMSK |= (1<<TOIE0);      // Overflow interrupt enabled.
    00157
    00158         TIMSK |= (1<<OCIE0A);  // Timer 0, Compare match A interrupt enabled.
    00159
    00160         // Enable interrupts, just in case they weren't already.
    00161         __enable_interrupt();
    00162 }

unsigned char Time_Left | ( | unsigned char | timer | ) |

Checks if a specified timer has expired.

Parameters:

  | timer | Specifies timer

Return values:

  | TRUE | Timer still going.
  | FALSE | Timer has expired.

Definition at line 78 of file time.c.

References FALSE, timeval, and TRUE.

Referenced by EnableBattery(), HaltNow(), JumperCheck(), PWM_Start(), and USI_OVF_ISR().

    00079 {
    00080         if(timeval[timer] > 0) {
    00081                 return(TRUE);
    00082         } else {
    00083                 return(FALSE);
    00084         }
    00085 }

void Time_Set | ( | unsigned char | timer,
|  | unsigned int | min,
|  | unsigned char | sec,
|  | unsigned char | ms |
| ) |  |  |

Sets the specified timer.

Parameters:

  | timer | Specifies timer
  | min | Minutes for timer to count down
  | sec | Seconds for timer to count down
  | ms | Milliseconds for timer to count down

Definition at line 95 of file time.c.

References timeval.

Referenced by Charge(), EnableBattery(), HaltNow(), JumperCheck(), PWM_Start(), and USI_OVF_ISR().

    00097 {
    00098 //      timeval[i] = 4 * (1000*(sec + 60*min) + ms);   // about 4000 ticks per second
    00099 //      timeval[i] = 240000 * (unsigned long)min;
    00100 //      timeval[i] += 4000 * (unsigned long)sec;
    00101 //      timeval[i] += 4 * (unsigned long)ms;
    00102
    00103         timeval[timer] = 60000 * (unsigned long)min;
    00104         timeval[timer] += 1000 * (unsigned long)sec;
    00105         timeval[timer] += 1 * (unsigned long)ms;
    00106 }

void Time_Start | ( | void |  | ) |

Starts timers.

Sets timer0's clock source to system clock divided by 64.

Definition at line 123 of file time.c.

Referenced by ConstantCurrent(), ConstantVoltage(), and MaxVoltageAndCurrent().

    00124 {
    00125         TCCR0B = (0<<CS02)|(1<<CS01)|(1<<CS00);         // CLKT0 = CLK/64 = 125 kHz.
    00126 }

void Time_Stop | ( | void |  | ) |

Stops timers.

Sets timer0's clock source to none.

Definition at line 113 of file time.c.

Referenced by ConstantCurrent(), ConstantVoltage(), and MaxVoltageAndCurrent().

    00114 {
    00115         TCCR0B = 0;
    00116 }

## Variable Documentation

unsigned long timeval[TIMERS]

Contains the values for each timer.

Definition at line 41 of file time.c.

Referenced by TICK_ISR(), Time_Init(), Time_Left(), Time_Set(), and USI_OVF_ISR().