static uint16_t track_current_samples[16] = { 0 };
static uint8_t track_current_head = 0;
static uint16_t track_current_sum = 0;
+static uint16_t track_current_peak = 0;
static uint16_t overcurrent_limit = 9707;
static uint8_t overcurrent_sent = 0;
static volatile uint8_t adc_state = 0;
static volatile uint16_t adc_value = 0;
+static uint16_t current_milliamps(uint16_t);
+
void monitor_init(void)
{
DDRB |= 0x02;
track_current_sum += value;
track_current_head = (i+1)&15;
+ if(track_current_sum>track_current_peak)
+ track_current_peak = track_current_sum;
+
if(track_current_sum>overcurrent_limit)
{
output_set_power(0);
return LENGTH_ERROR;
uint16_t value = monitor_track_current();
- uint8_t reply[3];
+ uint16_t peak = current_milliamps(track_current_peak);
+ track_current_peak = 0;
+
+ uint8_t reply[5];
reply[0] = TRACK_CURRENT;
reply[1] = value>>8;
reply[2] = value;
+ reply[3] = peak>>8;
+ reply[4] = peak;
interface_send(reply, sizeof(reply));
}
else if(cmd_buf[0]==SET_OVERCURRENT_LIMIT)
return COMMAND_OK;
}
-uint16_t monitor_track_current(void)
+static uint16_t current_milliamps(uint16_t value)
{
- uint16_t value = track_current_sum;
-
// Convert to milliamps: (v/16*5/1024-2.5)*1000/0.185
if(value<8192) // Ignore negative current readings
return 0;
}
}
+uint16_t monitor_track_current(void)
+{
+ return current_milliamps(track_current_sum);
+}
+
uint16_t monitor_input_voltage(void)
{
uint16_t value = input_voltage_sum;