2013-08-31 22:26:26 +02:00
|
|
|
#include <pentabug/photons.h>
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
|
|
|
#include <pentabug/hal.h>
|
|
|
|
#include <pentabug/lifecycle.h>
|
|
|
|
|
2013-09-03 20:14:30 +02:00
|
|
|
void photons_init(void) {
|
|
|
|
ADCSRA |= (1 << ADEN);
|
|
|
|
}
|
2013-08-31 22:26:26 +02:00
|
|
|
|
2013-09-03 21:25:07 +02:00
|
|
|
void photons_stop(void) {
|
|
|
|
ADCSRA &= ~(1 << ADEN);
|
|
|
|
}
|
|
|
|
|
2013-09-03 20:14:30 +02:00
|
|
|
uint8_t photons_measure(void) {
|
2013-08-31 22:26:26 +02:00
|
|
|
// save old state
|
|
|
|
|
2013-09-03 21:25:07 +02:00
|
|
|
uint8_t old_right = led_state(RIGHT);
|
|
|
|
uint8_t old_left = led_state(LEFT);
|
2013-08-31 22:26:26 +02:00
|
|
|
|
|
|
|
// all leds off
|
|
|
|
|
|
|
|
led_off(RIGHT);
|
|
|
|
led_off(LEFT);
|
|
|
|
|
2013-09-03 20:14:30 +02:00
|
|
|
// set to ground for discharge
|
2013-08-31 22:26:26 +02:00
|
|
|
|
2013-09-03 21:25:07 +02:00
|
|
|
DDRC |= 1 << 3;
|
2013-08-31 22:26:26 +02:00
|
|
|
PORTC &= ~(1 << 3);
|
|
|
|
|
|
|
|
// wait for discharge
|
|
|
|
|
2014-09-13 11:22:58 +02:00
|
|
|
wait_ms(5);
|
2013-09-03 20:14:30 +02:00
|
|
|
|
|
|
|
// set direction to input for measurement
|
|
|
|
|
|
|
|
DDRC &= ~(1 << 3);
|
|
|
|
|
|
|
|
// collect some photons
|
|
|
|
|
2013-09-06 13:53:19 +02:00
|
|
|
wait_ms(10);
|
2013-08-31 22:26:26 +02:00
|
|
|
|
|
|
|
// start measurement
|
|
|
|
|
2013-09-03 21:25:07 +02:00
|
|
|
// TODO: why can't i move this to the initialization?
|
2013-09-03 20:14:30 +02:00
|
|
|
ADMUX = (1 << REFS0) | (1 << ADLAR);
|
|
|
|
ADCSRA |= (1 << ADPS2) | (1 << ADPS1);
|
2013-08-31 22:26:26 +02:00
|
|
|
|
|
|
|
ADMUX = (ADMUX & ~(0x1F)) | 3;
|
2013-09-03 20:14:30 +02:00
|
|
|
|
2013-08-31 22:26:26 +02:00
|
|
|
ADCSRA |= (1 << ADSC);
|
|
|
|
|
|
|
|
// wait for measurement to finish
|
|
|
|
|
|
|
|
while (ADCSRA & (1 << ADSC)) test_stop_app();
|
|
|
|
|
2013-09-03 20:14:30 +02:00
|
|
|
uint8_t res = ADCH;
|
2013-08-31 22:26:26 +02:00
|
|
|
|
|
|
|
// restore state
|
|
|
|
|
2013-09-03 21:25:07 +02:00
|
|
|
PORTC |= 1 << 3;
|
|
|
|
DDRC |= 1 << 3;
|
|
|
|
|
|
|
|
led_set(RIGHT, old_right);
|
|
|
|
led_set(LEFT, old_left);
|
2013-08-31 22:26:26 +02:00
|
|
|
|
|
|
|
// done
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|