This commit is contained in:
Christoph J. Scherr 2023-12-09 14:36:58 +01:00
parent 467ebb3e9c
commit c5bc169461
3 changed files with 9 additions and 6 deletions

View File

@ -10,9 +10,9 @@ void adc_init() {
ADMUX = (1<<REFS0); ADMUX = (1<<REFS0);
} }
uint16_t adc_read(uint8_t channel) { uint16_t adc_read(uint8_t channel) {
ADMUX &= 0xf0; ADMUX &= 0xf0; // max channel?
ADMUX |= (channel & 0x0f); ADMUX |= (channel & 0x0f); // select channel
ADCSRA |= (1<<ADSC); ADCSRA |= (1<<ADSC); // interrupt enable
while((ADCSRA&(1<<ADIF))==0); while((ADCSRA&(1<<ADIF))==0); // wait for something
return ADCL + (ADCH << 8); return ADCL + (ADCH << 8); // read
} }

View File

@ -1,5 +1,7 @@
#include "adc.h"
int rand() { int rand() {
static unsigned int seed = 1337191337; static unsigned int seed = 1337191337;
seed *= adc_read(3);
unsigned int buf[] = { unsigned int buf[] = {
(seed*31832)%677, (seed*31832)%677,
(seed*32932)%2332 (seed*32932)%2332

View File

@ -17,7 +17,6 @@ int main (void)
adc_init(); adc_init();
sei(); sei();
char buf[10];
uint16_t sensor[2] = { 0, 1 }; uint16_t sensor[2] = { 0, 1 };
while (1) while (1)
@ -28,6 +27,8 @@ int main (void)
// we want to print this, so we store the ascii repr in our buffer // we want to print this, so we store the ascii repr in our buffer
uart_printi("sensor 0: ", sensor[0]); uart_printi("sensor 0: ", sensor[0]);
uart_printi("sensor 1: ", sensor[1]); uart_printi("sensor 1: ", sensor[1]);
uart_printi("sensor 2 (noise): ", adc_read(2));
uart_printi("rand: ", rand());
_delay_ms(1000); _delay_ms(1000);
} }
} }