--------------------------------------------------------------------------------
 Copyright (c) 2012 by Silicon Laboratories. 
 All rights reserved. This program and the accompanying materials
 are made available under the terms of the Silicon Laboratories End User 
 License Agreement which accompanies this distribution, and is available at
 http://developer.silabs.com/legal/version/v10/License_Agreement_v10.htm
 Original content and implementation provided by Silicon Laboratories.
--------------------------------------------------------------------------------

Program Description:
--------------------------------------------------------------------------------
This is a simple example for the Advanced Capture Counter (ACCTR0) module in LC 
mode using the LC tank daughter card and SiM3L1xx MCU Card.

In LC mode, the ACCTR works by perturbing an LC tank and counting the 
number of peaks seen before the oscillation is damped to the point where
the IC can no longer distinguish peaks. See the AN741 documentation on the
Silicon Labs website for more information on the operation of this circuit.

Resources Used:
--------------------------------------------------------------------------------
RTC0 module
LCD0 module
ACCTR0 module

Applicaton Operation:
--------------------------------------------------------------------------------
COUNTING PULSES:
The system counts pulses using the ACCTR in single channel mode. The ACCTR0 is 
not using quadrature mode. The LC tank is formed by a 470 uH inductor and a 39 
pF capacitor. This example uses phases P and A. Phase P is a preconditioning 
phase that takes 2 cycles. In Phase A, the ACCTR0 module counts pulses and 
lasts 2 cycles. Digital histeresis is used to prevent false counts when the 
impeller stalls near the decision point. The shortest possible pulse is used to
excite the tank. This is both to reduce power consumption and to prevent 
clipping of the measured signal. The analog comparator hysteresis and threshold
settings are used to make the peak count as stable as possible while retaining 
as much MIN/MAX delta as possible. The comparator is also set to the 400 ns 
setting to ensure enough MIN/MAX delta for robust operation. 

CALIBRAION:
When the demo starts, it enters the calibration phase. To calibrate the system,
spin the meter several times and press the center switch. After calibration, the
part enters ACTIVE mode.
While in calibration mode, the hardware keeps track of the MIN and MAX peaks seen. 
When calibration ends, the relationship between the MIN and MAX (delta) is fixed,
and the hardware will automatically slide the MIN/MAX window as it sees new min
and max values. See the ATRKEN bit for more details.
After calibration, the hardware also automatically sets the decision point to 
(MIN+MAX)/2 (ACDEN). With both ACDEN and ATRKEN set, the ACCTR0 module 
automatically adjusts to changes in the peak count caused by outside influences
(voltage changes, etc.). It is recomended that calibraton be performed at the 
lowest expected operating voltage.

ACTIVE MODE:
Active mode allows the user to see what the hardware is seeing and debug. During
active mode, the count is shown on the LCD updated every 100 ms. Debug signals 
are also routed out to the pins in this mode. TP3 is connected to the input to 
the LCD comparator. The ringing of the circut can be observed on this testpoint.
TP1 is the comparator output, and the number of peaks counted can be observed on
this testpoint. TP5 is the signal controlling the excitation pulses and can be 
used to trigger the scope.
  TP1: Trigger (excitation pulse)
  TP3: Tank ringing
  TP5: Peaks being counted

SLEEP:
When SW4 is pressed while in active mode, the system enters sleep mode, which is
the lowest power mode using Power Mode 8 (PM8). The system continues counting
pulses but does not use the LCD to conserve power.
The system remains asleep until the center joystick button is pressed. The system
then enters active mode and updates the LCD to show the total count (including 
sleep counts). The expected current of the MCU card in this mode with no scope 
probes attached is 2.5 uA.
Transitions between sleep and monitor modes are not supported, and the system 
must enter active mode first before transitioning to the next low power mode.
  
MONITOR:
When SW3 is pressed while in active mode, the system enters monitor mode. In 
this mode, the part enters Power Mode 8 (PM8) with the LCD displaying the last 
value. The part remains in PM8 counting pulses until a configurable number of 
pulses are seen. At this time, the core wakes up, updates the display, and then 
goes back to sleep. The number of pulses between wakeup events is configured 
using the ACCTR_ALARM_COUNT constant found in myACCTR0.h.
Pressing the center joystick button will exit monitor mode and return the 
system to active mode. Transitions between sleep and monitor modes are not
supported, and the system must enter active mode first before transitioning to 
the next low power mode.

Notes On Modes:
--------------------------------------------------------------------------------
Common settings:
  AHB/APB = 20 MHz
  RTC = XTAL mode
  RamRetention = ALL
  Low Power Charge Pump = ON
  ACCTR sample rate = 500 Hz

Active Mode (PB0.1/Joystick center)
  MCU = RUNNING
  LCD = ON
  DEBUG = ON
        
Monitor Mode (PB1.6/SW3)
  MCU = PM8 (wakes up every 0x16 counts to update display)
  LCD = ON
  DEBUG = OFF

Sleep Mode (PB1.7/SW4)
  MCU = PM8
  LCD = OFF
  DEBUG = OFF

IMPORTANT Notes:
--------------------------------------------------------------------------
1) Power is propotional to the number of RTC cycles the block spends
comparing. In this example, the number of cycles the comparator window
is open (phase A) is 2.
2) Additional current is consumed by the debug adapter. It should be 
disconnected while measuring power.
3) Additonal current is consumed by having scope probes on the 
testpoints. Probes should be removed when measuring sleep mode power.

Pulse Counter Configuration:
-------------------------------------------------------------------------
There are many relevant pulse counter settings. The settings required 
for a particular meter will vary and should be detemined by a process of 
guided/educated trial and error. See AN741 for a detailed list and
discussion of these features.

Bonus Features:
-------------------------------------------------------------------------
1) The user can select how often MONITOR mode wakes in myACCTR0.c
using ACCTR_ALARM_COUNT.
2) While the debugger can not be used if entering SLEEP or MONITOR mode,
the code can be loaded into the debugger and the ACCTR state viewed and
modified using the peripheral viewer. This is a good way to observe the 
effects of the various ACCTR settings. 


How to Use:
--------------------------------------------------------------------------------
1) Connect a USB Debug Adapter to the 10-pin CoreSight connector (J14).
2) (Optional) Remove the three shorting blocks from J7 for power measurement.
3) (Optional) Remove the JP2 Imeasure shorting block and put a multimeter across
(positive side on the bottom pin) for power measurement.
4) Move the VBAT Sel switch (SW2) to the middle +3.3V_VREG position.
5) Move the VIO Sel (SW8) and VIORF Sel (SW9) switches to the bottom VBAT 
position.
6) Connect the LC Resonant circuit to the ACCTR0 inputs (J20) or connect the 
ACCTR0 LC daughter card to the PB0 and PB1 headers. The board should hang out 
over the edge of the MCU card and not cover the MCU.
7) Connect the 9 V power adapter to POWER (J6).
8) Download the code to the board.
9) (Optional) Remove the debug adapter connection and any scope probes for 
power measurement.
10) Upon reset, the board will enter CAL mode. Spin the meter several times
and then press the center switch to finish calibration.
11) The board is now in ACTIVE mode. Counts can be observed on the LCD, and 
a scope can be used to observe signals on the LC daughter card.
12) Press SW3 to enter monitor mode.
13) (Optional) Measure the power of the device.
14) Press the center joystick switch to return to ACTIVE mode.
15) Press SW4 to enter SLEEP mode.
16) (Optional) Measure the power of the device.
17) Press the center joystick switch to return to ACTIVE mode and see pulses
counted while in PM8. 


Revision History:
--------------------------------------------------------------------------------
Release 1.0
    -Initial Revision

---eof--------------------------------------------------------------------------

