# main.c File Reference

## Detailed Description

Main program file.

Contains the main program, which is a basic state machine.

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
  2335

$RCSfile$

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

Date
  2007-09-07 10:11:19 +0200 (fr, 07 sep 2007)

Definition in file main.c.

`#include <ioavr.h>`
`#include <inavr.h>`
`#include <stdlib.h>`
`#include "structs.h"`
`#include "main.h"`
`#include "ADC.h"`
`#include "statefunc.h"`
`#include "battery.h"`
`#include "menu.h"`
`#include "OWI.h"`
`#include "PWM.h"`
`#include "time.h"`
`#include "USI.h"`

Include dependency graph for main.c:

Go to the source code of this file.

## Functions
int | main (void)
| Main program.
## Variables
unsigned char | CurrentState
| Global that indicates current state.

## Function Documentation

int main | ( | void |  | ) |

Main program.

The main function goes into an infinite loop, keeping track of the current state and the next one. If the next state is different from the current, it looks up the address to the next state function, in menu_state[], and updates CurrentState. The state function is then called and will eventually return a new state, and so the loop reiterates.

Todo:
  The variable inp is passed to all state functions, but is not used for anything yet. Remove?

Definition at line 98 of file main.c.

References CurrentState, menu_state, MENU_STATE_struct::pFunc, ST_INIT, MENU_STATE_struct::state, TRUE, and ZERO.

    00099 {
    00100         unsigned char nextstate, inp, i;
    00101         unsigned char (*pStateFunc)(unsigned char);   // Function pointer.
    00102
    00103         // Initialize local state variables.
    00104         inp = ZERO;
    00105         CurrentState = nextstate = ST_INIT;
    00106         pStateFunc = NULL;
    00107
    00108         // Look for function associated with current state, get its address.
    00109         for (i = 0; menu_state[i].state != 0; i++) {
    00110                 if (menu_state[i].state == CurrentState) {
    00111                         pStateFunc = menu_state[i].pFunc;
    00112                 }
    00113         }
    00114
    00115         while (TRUE) {
    00116                 // Run function associated with current state, get next state in return.
    00117                 if (pStateFunc != NULL){
    00118                         nextstate = pStateFunc(inp);
    00119                 }
    00120
    00121                 // Look up function for next state, if it differs from the current.
    00122                 if (nextstate != CurrentState) {
    00123                         CurrentState = nextstate;
    00124                         for ( i = 0; menu_state[i].state != 0; i++) {
    00125                                 if (menu_state[i].state == CurrentState) {
    00126                                         pStateFunc = menu_state[i].pFunc;
    00127                                 }
    00128                         }
    00129                 }
    00130         }
    00131 }

## Variable Documentation

unsigned char CurrentState

Global that indicates current state.

Updated by main().

Note:
  See menu.h for definition of states.

Definition at line 78 of file main.c.

Referenced by Charge(), main(), and SetErrorFlag().