Permalink
Browse files

shutoff the ADC after seeding rand

no reason to keep that unit eating power
  • Loading branch information...
1 parent c84be08 commit d3a5cc78ba412b183e02b9492489257c42ba358a @beerriot committed Feb 28, 2011
Showing with 11 additions and 2 deletions.
  1. +1 −0 include/nkrand.h
  2. +0 −1 src/blockgame.c
  3. +10 −1 src/nkrand.c
View
1 include/nkrand.h
@@ -2,6 +2,7 @@
#define __NKRAND_H__
void nkrand_init();
+void nkrand_close();
uint8_t nkrand_next_bit();
uint16_t nkrand_seed();
View
1 src/blockgame.c
@@ -34,7 +34,6 @@ int main() {
nklcd_init();
nkbuttons_init();
nktimer_init(60);
- nkrand_init();
srand(nkrand_seed());
bghighscore_init();
sei(); //enable interrupts
View
11 src/nkrand.c
@@ -22,7 +22,11 @@ void nkrand_init() {
// fire a conversion just to get the ADC warmed up
ADCSRA |= (1<<ADSC);
}
-
+
+void nkrand_close() {
+ ADCSRA = 0;
+}
+
uint8_t nkrand_next_bit() {
// Lowest bit from ADC is the one most likely to change due to minute
// variations in temperature as measured by LM37 and noise in power
@@ -53,6 +57,8 @@ uint16_t nkrand_seed() {
uint16_t seed = 0;
int8_t i;
+ nkrand_init();
+
// 'seed' is the value we are going to generate.
// Starting with zeros in all 16 bits of the 16-bit unsigned integer,
// we XOR the bits one by one with a highly volatile bit value from ADC,
@@ -61,5 +67,8 @@ uint16_t nkrand_seed() {
// XOR the seed with the random bit from ADC shifted
seed ^= (nkrand_next_bit() << (i%16));
}
+
+ nkrand_close();
+
return seed;
}

0 comments on commit d3a5cc7

Please sign in to comment.