Working light measurement

This commit is contained in:
Thammi 2013-09-03 20:14:30 +02:00
parent beadb895d7
commit 9e09d510ac
3 changed files with 21 additions and 13 deletions

View File

@ -13,7 +13,7 @@ static void init(void) {
static void run(void) { static void run(void) {
uint16_t light = photons_measure(); uint16_t light = photons_measure();
pentatonic_all_led_set(light >> 7); pentatonic_all_led_set(light >> 3);
wait_ms(500); wait_ms(500);
} }

View File

@ -5,6 +5,6 @@
void photons_init(void); void photons_init(void);
uint16_t photons_measure(void); uint8_t photons_measure(void);
#endif /* PHOTONS_H */ #endif /* PHOTONS_H */

View File

@ -5,9 +5,11 @@
#include <pentabug/hal.h> #include <pentabug/hal.h>
#include <pentabug/lifecycle.h> #include <pentabug/lifecycle.h>
void photons_init(void) {} void photons_init(void) {
ADCSRA |= (1 << ADEN);
}
uint16_t photons_measure(void) { uint8_t photons_measure(void) {
// save old state // save old state
uint8_t old_port = PORTC; uint8_t old_port = PORTC;
@ -18,34 +20,40 @@ uint16_t photons_measure(void) {
led_off(RIGHT); led_off(RIGHT);
led_off(LEFT); led_off(LEFT);
// set direction, no pullups // set to ground for discharge
DDRC &= ~(1 << 3);
PORTC &= ~(1 << 3); PORTC &= ~(1 << 3);
// wait for discharge // wait for discharge
// TODO: needed?
wait_ms(10); wait_ms(1);
// set direction to input for measurement
DDRC &= ~(1 << 3);
// collect some photons
wait_ms(1);
// start measurement // start measurement
ADMUX = (1 << REFS0); ADMUX = (1 << REFS0) | (1 << ADLAR);
ADCSRA = (1 << ADPS2) | (1 << ADPS1); ADCSRA |= (1 << ADPS2) | (1 << ADPS1);
ADCSRA |= (1 << ADEN);
ADMUX = (ADMUX & ~(0x1F)) | 3; ADMUX = (ADMUX & ~(0x1F)) | 3;
ADCSRA |= (1 << ADSC); ADCSRA |= (1 << ADSC);
// wait for measurement to finish // wait for measurement to finish
while (ADCSRA & (1 << ADSC)) test_stop_app(); while (ADCSRA & (1 << ADSC)) test_stop_app();
uint16_t res = ADCW; uint8_t res = ADCH;
// disable adc // disable adc
ADCSRA &= ~(1 << ADEN); /*ADCSRA &= ~(1 << ADEN);*/
// restore state // restore state