Skip to content

Instantly share code, notes, and snippets.

@nonchip
Last active February 10, 2017 09:38
Show Gist options
  • Save nonchip/08cf98fde03be3ad3688f9fdd40e601c to your computer and use it in GitHub Desktop.
Save nonchip/08cf98fde03be3ad3688f9fdd40e601c to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
uint16_t ADC_Read( uint8_t channel )
{
ADMUX = (ADMUX & ~(0x1F)) | (channel & 0x1F);
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC) ) {
}
return ADCW;
}
uint16_t ADC_rand(uint8_t channel){
uint16_t out=0;
for(uint16_t i=0; i<16; ++i){
out<<=1;
out|=(ADC_Read(channel)&1);
}
return out;
}
void showBNum(uint8_t num) {
for (uint8_t i = 0; i < 0xff; ++i) //Note: this PWM loop is a software fix for using wrong resistor values
{
PORTD = ~num;
_delay_us(30);
PORTD = 0xff;
_delay_ms(2);
}
}
void showFNum(uint8_t num) {
showBNum(~(0xff<<num));
}
int main (void) {
ADMUX = (1<<REFS1) | (1<<REFS0);
ADCSRA = (1<<ADPS1) | (1<<ADPS0);
ADCSRA |= (1<<ADEN);
PORTD=0xff;
DDRD =0xFF;
PORTB=0b110;
_delay_ms(1);
uint8_t dip= (~PINB>>1) & 3;
uint8_t rfc1149_5=dip&1;
uint8_t countable=dip>>1;
for (int i = 0; i < 16; ++i)
{
srand((ADC_rand(0)
^ ADC_rand(1)
^ ADC_rand(2)
^ ADC_rand(3)
^ ADC_rand(4)
^ ADC_rand(5)
^ rand())
+i
);
}
ADCSRA = 0;
uint8_t value;
if(rfc1149_5){
value=4;
}else{
value=rand();
}
if(countable){
showFNum(value%9);
}else{
showBNum(value);
}
PORTB=0;
DDRD=0;
while(1){
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
cli();
sleep_cpu();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment