Digital Scale 1.0 Digital Scale

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Functions | Variables
scale.c File Reference

The Main Program and miscellaneous Routines. More...

## Functions
void | Calibrate (void)
| Performs Calibration of the Scale. More...
void | DelayMs (delay_t ms)
| Delays for ms Milliseconds. More...
void | doEFlags (void)
| Handles Error Flags. More...
void | doSwitch (void)
| Handles Switch Events. More...
void | doTflags (void)
| Handles Timer Flags. More...
weight_t | GetAVG1 (weight_t w)
| Get averaged Weight. More...
weight_t | GetAVG2 (weight_t w)
| Get filtered Weight. More...
weight_t | GetAVG3 (weight_t w)
| Get filtered Weight. More...
weight_t | GetWeightFromRaw (int32_t raw)
| Gets the calibrated Weight from a raw HX-Value. More...
void | Init (void)
| Initializes the System. More...
static void | InitAVG (void)
| Initializes the Weight Averaging. More...
int | main (void)
void | readHX (uint8_t extraPulses)
| Reads out the HX711. More...
static void | readSwitch (void)
| Handles the Switch. More...
void | ResetTimeout (void)
| Reset Timeout Counters. More...
void | TIMER_COMPA_vect (void)
| Timer compare interrupt (1000Hz) More...
void | VBat2Display_alt ()
| Writes VBat to display_alt. More...

## Variables
volatile uint8_t | clickCnt
| Time since last click. Used to detect doubleclicks. More...
uint8_t | cnt128
| Must be set to zero to start summing. More...
static volatile delay_t | delaycnt
| Counter for delay(). Count value is milliseconds. More...
volatile uint8_t | eFlags
| Error flags. More...
uint8_t | histSize
| Number of values used for moving average. More...
uint8_t | hxCnt
| is incremented each time the HX receives a new value More...
bool | HXdisplayWeight
| If true, reading a new HX value updates the display. May be disabled for service display. More...
weight_t | lastWeightAvg
| The weightAvg 1 second ago (used to detect activity) More...
| LOCKBITS = (LB_MODE_1)
| Fuse settings for the Production ELF file. More...
const weight_t | refWeights []
| List of possible reference Weights for Calibration. More...
volatile uint8_t | secCnt
| Seconds of inactivity. More...
weight_t | sum128
| Contains the sum of 128 values when cnt128==128. More...
volatile SWITCH_ACTION_t | switchAction
| Is set by interrupt if the switch has been pressed. Must be reset by the surrounding loop. More...
uint16_t | Temp
| Raw ADC Value of Chip Temperature. Typ. 354=85°C, 292=25°C, 225=-45°C, ~1°C/bit. More...
volatile weight_t | testWeight
volatile uint8_t | tFlags
| Flags set by Time. More...
volatile uint8_t | tickCnt
| Counter for timer0 interrupts. More...
uint16_t | VBat
| Raw ADC Value of Battery Voltage. 28.5mV/LSB or 29.19V FSR with a 120k/4k7 divider. More...
weight_t | weightAvg
| The moving mean value of the raw weight values. More...
weight_t | weightHist [MAXHIST]
| the last 8 values used for averaging More...
uint8_t | weightIndex
| pointer to the next value of weightHist More...
volatile weight_t | weightRaw
| The raw Weight as received from the HX711. More...
weight_t | weightSum
| the sum of all weightHist More...
weight_t | weightZero
| The averaged weight at zero load. More...
volatile uint8_t | zeroWeightSecCnt
| Counts seconds of 'no load'. Used for accelerated power off. More...

## Detailed Description

The Main Program and miscellaneous Routines.

Copyright
  Copyright (C) 2018 Robert Loos http://www.loosweb.de

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.

## Function Documentation

void ResetTimeout | ( | void | ) |  |

Reset Timeout Counters.

weight_t GetWeightFromRaw | ( | int32_t | raw) |  |

Gets the calibrated Weight from a raw HX-Value.

Parameters

  raw | The raw Value

Returns
  The calculated Weight according to Calibration Data

static void InitAVG | ( | void | ) |  |
| inline static

Initializes the Weight Averaging.

Must be called once during startup

weight_t GetAVG1 | ( | weight_t | w) |  |

Get averaged Weight.

This routine uses sliding averages i.e. the result is the average over the last n values.

Parameters

  w | The actual weight

Returns
  The average over the last HIST_SIZE values

weight_t GetAVG2 | ( | weight_t | w) |  |

Get filtered Weight.

This routine uses an IIR filter to smoothen the display value. The formula is xi=(x(i-1) * c + x) / (c+1)

Parameters

  w | The actual weight

Returns
  The filtered weight over the last 8 values

weight_t GetAVG3 | ( | weight_t | w) |  |

Get filtered Weight.

This routine uses an FIR SINC filter to smoothen the display value.

Parameters

  w | The actual weight

Returns
  The filtered weight over the last NUM_COEFF values

void readHX | ( | uint8_t | extraPulses) |  |

Reads out the HX711.

Parameters

  extraPulses | the number of clock pulses after the value has been received. This selects the gain for the next measurement. may be 1 (g=128), 2 (g=32) or 3 (g=64)

When a new Value is available (DOUT=0) it data is shifted out and weightRaw is set accordingly.

static void readSwitch | ( | void | ) |  |
| inline static

Handles the Switch.

This routine is called 100 times per second from the timer interrupt. If a switch event occurs it sets the switchAction variable accordingly. The surrounding loop must set it to SWITCH_NONE when it processes the event.

void TIMER_COMPA_vect | ( | void | ) |  |

Timer compare interrupt (1000Hz)

void DelayMs | ( | delay_t | ms) |  |

Delays for ms Milliseconds.

Note
  The timer resolution is 1ms and the state of the timer when calling this function is normally undefined. The actual delay may differ up to 1ms from the given value (especially, it may be nearly 0 if you specify 1ms)!

Parameters

  ms | The desired delay in Milliseconds

void Init | ( | void | ) |  |

Initializes the System.

void VBat2Display_alt | ( | ) |  |

Writes VBat to display_alt.

Displays VBat on the rightmost 3 digits. Resolution is 100mV. VBat is only allowed up to 40V so 3 digits are enough. "BAT" can be displayed at the leftmost digits if you remove the comments but it is intended that the left three digits show the temperature then. Anyway, this routine is intended for debugging purposes only. Display is set to alternate mode. Leading zero is suppressed what makes it more readable with common battery voltages.

void doEFlags | ( | void | ) |  |

Handles Error Flags.

void doTflags | ( | void | ) |  |

Handles Timer Flags.

For now, there is only one flag that is set once per second. It is used to acquire the analog values for chip temperature and battery voltage and to detect weight changes for display recovery. Timer flags have to be cleared at the end of this routine.

void doSwitch | ( | void | ) |  |

Handles Switch Events.

void Calibrate | ( | void | ) |  |

Performs Calibration of the Scale.

int main | ( | void | ) |  |

## Variable Documentation

LOCKBITS = (LB_MODE_1)

Fuse settings for the Production ELF file.

The Lock Bits for the production File

volatile uint8_t tickCnt

Counter for timer0 interrupts.

volatile uint8_t secCnt

Seconds of inactivity.

volatile uint8_t clickCnt

Time since last click. Used to detect doubleclicks.

volatile delay_t delaycnt
| static

Counter for delay(). Count value is milliseconds.

volatile uint8_t zeroWeightSecCnt

Counts seconds of 'no load'. Used for accelerated power off.

uint16_t Temp

Raw ADC Value of Chip Temperature. Typ. 354=85°C, 292=25°C, 225=-45°C, ~1°C/bit.

uint16_t VBat

Raw ADC Value of Battery Voltage. 28.5mV/LSB or 29.19V FSR with a 120k/4k7 divider.

volatile weight_t weightRaw

The raw Weight as received from the HX711.

weight_t weightAvg

The moving mean value of the raw weight values.

weight_t weightZero

The averaged weight at zero load.

uint8_t histSize

Number of values used for moving average.

weight_t weightHist[MAXHIST]

the last 8 values used for averaging

Array holding the last Weight Values.

uint8_t weightIndex

pointer to the next value of weightHist

weight_t weightSum

the sum of all weightHist

weight_t lastWeightAvg

The weightAvg 1 second ago (used to detect activity)

uint8_t hxCnt

is incremented each time the HX receives a new value

bool HXdisplayWeight

If true, reading a new HX value updates the display. May be disabled for service display.

weight_t sum128

Contains the sum of 128 values when cnt128==128.

uint8_t cnt128

Must be set to zero to start summing.

const weight_t refWeights[]

Initial value:
={
100000,
200000,
500000,
999999,
}

List of possible reference Weights for Calibration.

volatile weight_t testWeight