pyrorf/analog.c
2019-10-22 11:56:33 +02:00

58 lines
863 B
C

#include "analog.h"
#include "status.h"
#include <avr/io.h>
uint8_t analog_cpt=0;
#define BATT 9
#define WAIT 10
uint8_t st = WAIT;
void start(){
//setup adc
ADCSRA = _BV(ADEN)|_BV(ADPS2)|_BV(ADPS1); // ADC Clock : 1/64 (125KHz)
ADMUX = _BV(REFS1)|_BV(REFS0); // Reference 2.56V
st=0;
ADCSRA |= _BV(ADSC); // start first conversion
}
void stop(){
ADCSRA=0;
}
void analog_run()
{
if(st==WAIT){
if (analog_cpt)
return;
start();
return;
}
if(!( ADCSRA & _BV(ADSC) )) // ADC still running
return;
if(st >= BATT){
uint16_t battval= ADC;
//TODO => lowbatt detection;
st=WAIT;
stop();
return;
}
lines[st] = ADC;
st++;
if(st<=7){
ADMUX = _BV(REFS1)|_BV(REFS0)| st;
ADCSRA |= _BV(ADSC);
return;
}
if(st==BATT){
ADMUX = _BV(REFS0)| 0b11110; // mesure la ref 1.22 / VCC
ADCSRA |= _BV(ADSC);
return;
}
}