fg004_a1/load.c

62 lines
1.4 KiB
C

#include "load.h"
#include "ADC.h"
#include "config.h"
#include "RegEdit.h"
#define ADC_LOAD1 4
#define ADC_LOAD2 5
#define ADC_LOAD3 6
#define G1 1
#define G2 3
#define G3 2
#define SAMPLE_QTY 32
uint16_t read_load(uint8_t pin_num)
{
ADC_Init(pin_num);
ADC_Enable(pin_num);
uint16_t value = ADC_ReadValue(pin_num);
ADC_Disable();
return value;
}
void Load_HandlePinLoads(void)
{
//saving the state of the port A output.
uint8_t porta_out_state = PORTA.OUT;
uint8_t portb_out_state = PORTB.OUT;
uint16_t load_one, load_two, load_three;
load_one = 0;
load_two = 0;
load_three = 0;
for(int i = 0; i < SAMPLE_QTY; i++){
load_one += read_load(ADC_LOAD1);
load_one += read_load(ADC_LOAD2);
load_one += read_load(ADC_LOAD3);
}
load_one /= SAMPLE_QTY;
load_two /= SAMPLE_QTY;
load_three /= SAMPLE_QTY;
//load the backed up state(just in case)
RegEdit_SetNum((void *) &PORTA.OUT, porta_out_state);
RegEdit_SetNum((void *) &PORTB.OUT, portb_out_state);
if(load_one < LowThresh || load_one > HighThresh){
RegEdit_ClearBit((void *) &PORTA.OUT, G1);
}
if(load_two < LowThresh || load_two > HighThresh){
RegEdit_ClearBit((void *) &PORTB.OUT, G2);
}
if(load_three < LowThresh || load_three > HighThresh){
RegEdit_ClearBit((void *) &PORTB.OUT, G3);
}
}