[avr] write sensor data to EEPROM when power-loss is detected

This commit is contained in:
Bart Van Der Meerssche 2011-02-03 12:17:33 +01:00
parent e842040395
commit f99ea292ff
2 changed files with 19 additions and 16 deletions

View File

@ -241,25 +241,16 @@ ISR(TIMER1_COMPA_vect)
ISR(ANALOG_COMP_vect) ISR(ANALOG_COMP_vect)
{ {
uint8_t i; disable_led();
PORTB |= (1<<PB0); eeprom_write_block((const void*)&sensor, (void*)&EEPROM_sensor, sizeof(sensor));
eeprom_write_block((const void*)&event, (void*)&EEPROM_event, sizeof(event));
//disable uC sections to consume less power while writing to EEPROM // uint8_t i;
//disable UART Tx and Rx: // for (i=0; i<128; i++)
UCSR0B &= ~((1<<RXEN0) | (1<<TXEN0)); // eeprom_write_byte((uint8_t *)i, i);
//disable ADC:
ADCSRA &= ~(1<<ADEN);
for (i=0; i<128; i++) setup_led();
eeprom_write_byte((uint8_t *)i, i);
//enable UART Tx and Rx:
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
// enable ADC and start a first ADC conversion
ADCSRA |= (1<<ADEN) | (1<<ADSC);
PORTB &= ~(1<<PB0);
} }
void setup_datastructs(void) void setup_datastructs(void)
@ -273,10 +264,20 @@ void setup_datastructs(void)
void setup_led(void) void setup_led(void)
{ {
// set output low (= LED enabled)
PORTB &= ~(1<<PB0);
// set LED pin (PB0) as output pin // set LED pin (PB0) as output pin
DDRB |= (1<<DDB0); DDRB |= (1<<DDB0);
} }
void disable_led(void)
{
// set LED pin (PB0) as input pin
DDRB &= ~(1<<DDB0);
// disable pull-up
PORTB &= ~(1<<PB0);
}
void setup_pulse_input(void) void setup_pulse_input(void)
{ {
// PD2=INT0 and PD3=INT1 configuration // PD2=INT0 and PD3=INT1 configuration

View File

@ -91,6 +91,8 @@ asm volatile ( \
) )
void register_pulse(volatile struct sensor_struct *psensor, volatile struct state_struct *pstate); void register_pulse(volatile struct sensor_struct *psensor, volatile struct state_struct *pstate);
void setup_led(void);
void disable_led(void);
void setup_datastructs(void); void setup_datastructs(void);
void setup_pulse_input(void); void setup_pulse_input(void);
void setup_adc(void); void setup_adc(void);