Cleanup, reorganization
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
PRG = main
|
||||
|
||||
MCU_TARGET = attiny13
|
||||
OPTIMIZE = 2
|
||||
|
||||
HZ = 9600000UL
|
||||
|
||||
|
||||
LFUSE = 0x7A
|
||||
HFUSE = 0xFF
|
||||
|
||||
|
||||
|
||||
|
||||
DEFS =-std=gnu99 -funsigned-char -funsigned-bitfields -ffunction-sections -fpack-struct -fshort-enums -ffreestanding -fwhole-program -fno-inline-small-functions -fno-split-wide-types -fno-tree-scev-cprop -Wl,--relax,--gc-sections
|
||||
CC = avr-gcc
|
||||
OBJCOPY = avr-objcopy
|
||||
OBJDUMP = avr-objdump
|
||||
|
||||
OBJ = $(PRG).o
|
||||
|
||||
override CFLAGS = -g2 -Wall -O$(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -DF_CPU=$(HZ)
|
||||
|
||||
# program: $(PRG).elf lst hex
|
||||
#
|
||||
|
||||
$(PRG).elf: $(OBJ)
|
||||
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
|
||||
avr-size -C -d --mcu=$(MCU_TARGET) $(PRG).elf
|
||||
|
||||
lst: $(PRG).lst
|
||||
|
||||
%.lst: %.elf
|
||||
$(OBJDUMP) -h -S $< > $@
|
||||
|
||||
hex: $(PRG).hex lst
|
||||
|
||||
build: hex ehex
|
||||
|
||||
%.hex: %.elf
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
|
||||
|
||||
wflash: hex
|
||||
sudo avrdude -P usb -c dragon_isp -p $(MCU_TARGET) -U flash:w:$(PRG).hex
|
||||
|
||||
|
||||
wfuses:
|
||||
sudo avrdude -P usb -c dragon_isp -p $(MCU_TARGET) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m
|
||||
|
||||
|
||||
weeprom: ehex
|
||||
sudo avrdude -P usb -c dragon_isp -p $(MCU_TARGET) -U lfuse:w:$(LFUSE):m -U eeprom:w:$(PRG)_eeprom.hex
|
||||
|
||||
ee: ehex
|
||||
eeprom: ehex
|
||||
|
||||
ehex: $(PRG)_eeprom.hex
|
||||
|
||||
%_eeprom.hex: %.elf
|
||||
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.o $(PRG).elf *.hex *.lst *~
|
||||
|
||||
term:
|
||||
sudo avrdude -P usb -c dragon_isp -p $(MCU_TARGET) -t
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
# Electronic Dice with AVR
|
||||
|
||||
The idea is pretty straightforward, hardware is simple, but it's still darn
|
||||
cool!
|
||||
|
||||
The project is built around ATTiny13, but you can of course use any bigger chip
|
||||
or even arduino, but it'll really be overkill. It's not making full use even of
|
||||
the few peripherals the ATTiny13 provides!
|
||||
|
||||
|
||||
## What it does?
|
||||
|
||||
Really simple, you have a couple LEDs that represent dots on a dice, and one
|
||||
button.
|
||||
|
||||
When the button is down, the numbers quickly spin, and when you release
|
||||
it, they slow down to a crawl, then the number blinks a few times and that's an
|
||||
indication that your roll is complete.
|
||||
|
||||
When the dice is idle for 5 seconds, it goes to sleep (can be woken by pressing
|
||||
the button). The delay can be customized in the source file.
|
||||
|
||||
|
||||
## Hardware
|
||||
|
||||
The dice is not really challenging on the hardware side.
|
||||
|
||||
All you need is 7 LEDs with resistors, one button, and obviously the AVR.
|
||||
You can also add a power switch, but it's not really needed, since the dice
|
||||
is smart enough to sleep when it's not used.
|
||||
|
||||
It can be powered by any DC source, 3..5V. Three AA batteries in series can do
|
||||
the trick, four AA's won't hurt anything as well - the chip is quite durable
|
||||
if you don't overdo it.
|
||||
|
||||
Cunsumption when lights are on is (with my LEDs and 180R resistors) somewhat
|
||||
around 50 mA, in sleep mode it's negligible (< 1 uA if my meter can be trusted).
|
||||
|
||||
|
||||
### Pinout
|
||||
|
||||
```
|
||||
+--u--+
|
||||
RST --| |-- Vcc
|
||||
SEG_DIAG2 : PB3 --| t13 |-- PB2 : SEG_DIAG1
|
||||
SEG_HORIZ : PB4 --| |-- PB1 : SEG_DOT
|
||||
GND --| |-- PB0 : BUTTON
|
||||
+-----+
|
||||
```
|
||||
|
||||
|
||||
### Placement of the LED segments
|
||||
|
||||
One could think that using 7 pins for 7 leds is needed, but in fact, you can use
|
||||
just four - which is great, since ATTiny13 has only 5 I/O pins (unless you use
|
||||
the reset pin as I/O, but that's quite uncommon).
|
||||
|
||||
```
|
||||
|
||||
(DIAG1) (DIAG2)
|
||||
|
||||
(HORIZ) (DOT) (HORIZ)
|
||||
|
||||
(DIAG2) (DIAG1)
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Wiring of the segments
|
||||
|
||||
In the project, LEDs are connected by anodes to the pins. It's okay,
|
||||
since the chip can handle it just fine, but you may want to connect them by
|
||||
cathode instead (useful for greater loads).
|
||||
|
||||
To do so, adjust the `show()` routine accordingly (there's an explanatory
|
||||
comment included). Basically, just negate the output states, and you're good.
|
||||
|
||||
```
|
||||
DOT sengment:
|
||||
|
||||
PIN ---> LED -> RESISTOR ---> GND
|
||||
|
||||
|
||||
Other segments:
|
||||
|
||||
PIN -+-> LED -> RESISTOR -+-> GND
|
||||
| |
|
||||
'-> LED -> RESISTOR -'
|
||||
|
||||
Button:
|
||||
|
||||
PIN --> BUTTON --> GND
|
||||
```
|
||||
|
||||
|
||||
## The program
|
||||
|
||||
Now that you have your dice hardware assembled, let's move on to the fun part -
|
||||
the software.
|
||||
|
||||
You can of course just grab it from the repository, compile, flash and be done
|
||||
with it, but I think it's useful to explain how it works.
|
||||
|
||||
Feel free to tinker with the source, add extra effects etc, it's fun!
|
||||
|
||||
|
||||
### Generating random numbers
|
||||
|
||||
Firstly, we don't use any hardware entropy, or even a random number generator.
|
||||
That may sound odd, but it's in fact not really needed.
|
||||
|
||||
The core idea is that the button is never pressed at the same time and for the
|
||||
same duration. So, we can easily spin a counter when the button is down, and
|
||||
when it's released, we pretty much already have our random number.
|
||||
|
||||
To add more randomness, there's also a second, "entropy" counter, that spins all
|
||||
the time (unless, of course, the dice is sleeping), and when the button is
|
||||
pushed, the value from this "entropy" counter is copied to the main counter.
|
||||
The idea is that people can't cheat by somehow managing to press the button
|
||||
for the exact same time.
|
||||
|
||||
Note, that those counters are just ints incrememnted and wrapped by the program
|
||||
- we don't have to use of the hardware timers for this.
|
||||
|
||||
|
||||
### Sleep mode
|
||||
|
||||
When the dice is inactive for five seconds (that is, no animation and
|
||||
interaction), it automatically enters a POWER DOWN sleep mode.
|
||||
|
||||
Clearly, going to sleep without a way to wake up would be silly. Therefore, it
|
||||
also enables a Pin Change Interrupt right before entering the sleep mode, so a
|
||||
button press will wake it up. The cool thing is that it immediately starts
|
||||
spinning the numbers and resumes normal operation.
|
||||
|
||||
Note, that the dice also starts into the sleep mode, so when you power it on,
|
||||
it will sleep until you press the button. This is important, because I didn't
|
||||
like the idea of showing the same number it starts - this way, you randomize it
|
||||
right away by the first button press.
|
||||
@@ -0,0 +1,281 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Fuses are defined in makefile.
|
||||
*
|
||||
* Led segments:
|
||||
*
|
||||
* (DIAG1) (DIAG2)
|
||||
* (HORIZ) (DOT) (HORIZ)
|
||||
* (DIAG2) (DIAG1)
|
||||
*
|
||||
*
|
||||
* Wiring:
|
||||
*
|
||||
* +--u--+
|
||||
* RST --| |-- Vcc
|
||||
* SEG_DIAG2 : PB3 --| t13 |-- PB2 : SEG_DIAG1
|
||||
* SEG_HORIZ : PB4 --| |-- PB1 : SEG_DOT
|
||||
* GND --| |-- PB0 : BUTTON
|
||||
* +-----+
|
||||
*
|
||||
* SEG: PIN -> LED -> RESISTOR -> GND
|
||||
* BTN: PIN -> BUTTON -> GND
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
// general macros
|
||||
#define SECTION(pos) __attribute__((naked, used, section(pos)))
|
||||
|
||||
#define true 1
|
||||
#define false 0
|
||||
typedef uint8_t bool;
|
||||
|
||||
|
||||
|
||||
// individual segment pins
|
||||
#define SEG_DOT _BV(PB1)
|
||||
#define SEG_DIAG1 _BV(PB2)
|
||||
#define SEG_DIAG2 _BV(PB3)
|
||||
#define SEG_HORIZ _BV(PB4)
|
||||
|
||||
// button pin
|
||||
#define PIN_BUTTON _BV(PINB0)
|
||||
|
||||
#define OUT_PINS (SEG_DOT | SEG_DIAG1 | SEG_DIAG2 | SEG_HORIZ)
|
||||
#define IN_PINS (BUTTON)
|
||||
|
||||
// test if button is down
|
||||
#define BUTTON_DOWN() ((PINB & PIN_BUTTON) == 0)
|
||||
|
||||
|
||||
void init_io() SECTION(".init8");
|
||||
|
||||
|
||||
/**
|
||||
* Initialize IO ports.
|
||||
*/
|
||||
void init_io()
|
||||
{
|
||||
DDRB = OUT_PINS;
|
||||
PORTB = PIN_BUTTON; // pullup
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Consume interrupt used to wake from sleep
|
||||
*/
|
||||
ISR(PCINT0_vect)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show dice segments.
|
||||
*
|
||||
* @param segments to show
|
||||
*/
|
||||
void show(uint8_t bits)
|
||||
{
|
||||
// PIN -> LED -> GND
|
||||
PORTB = (PORTB & ~OUT_PINS) | (bits & OUT_PINS);
|
||||
|
||||
// VCC -> LED -> PIN
|
||||
//PORTB = (PORTB & ~OUT_PINS) | ~(bits & OUT_PINS);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show number using segments.
|
||||
* 0=one, 5=six.
|
||||
*
|
||||
* @param number to show. MUST BE IN RANGE 0..5
|
||||
*/
|
||||
void show_number(const uint8_t number)
|
||||
{
|
||||
if(number > 5) return;
|
||||
|
||||
static const uint8_t num2seg[] = {
|
||||
/*1*/ SEG_DOT,
|
||||
/*2*/ SEG_DIAG1,
|
||||
/*3*/ SEG_DIAG2 | SEG_DOT,
|
||||
/*4*/ SEG_DIAG1 | SEG_DIAG2,
|
||||
/*5*/ SEG_DIAG1 | SEG_DIAG2 | SEG_DOT,
|
||||
/*6*/ SEG_DIAG1 | SEG_DIAG2 | SEG_HORIZ,
|
||||
};
|
||||
|
||||
show( num2seg[number] );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enter power down mode and wait for pin change to wake up.
|
||||
*/
|
||||
void go_sleep()
|
||||
{
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||
cli();
|
||||
|
||||
GIMSK = _BV(PCIE);
|
||||
PCMSK = _BV(PCINT0);
|
||||
|
||||
sleep_enable();
|
||||
sei();
|
||||
sleep_cpu();
|
||||
cli();
|
||||
sleep_disable();
|
||||
|
||||
GIMSK = 0;
|
||||
PCMSK = 0;
|
||||
|
||||
sei();
|
||||
}
|
||||
|
||||
|
||||
// Delay between faces in slowing roll,
|
||||
// after which the dice is stopped
|
||||
#define DELAY_MS_FOR_STOP 600
|
||||
|
||||
|
||||
// How long until dice enters sleep mode
|
||||
#define IDLE_MS_FOR_SLEEP 5000
|
||||
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
void main()
|
||||
{
|
||||
// == VARS ==
|
||||
|
||||
// Used to randomize when button goes down
|
||||
// Needed to prevent cheating and add more randomness
|
||||
uint8_t entropy = 0;
|
||||
|
||||
// Number shown on dice (zero-based, 0 ... one dot)
|
||||
uint8_t number = 0;
|
||||
|
||||
// Delay between face flip in roll
|
||||
uint16_t delay_ms = DELAY_MS_FOR_STOP;
|
||||
|
||||
// Added to delay_ms each flip.
|
||||
// Is increased to make slowing non-linear
|
||||
uint16_t plus = 1;
|
||||
|
||||
// counts milliseconds when the dice was idle
|
||||
// used to trigger sleep mode
|
||||
uint16_t idle_counter = 0;
|
||||
|
||||
// flag that dice is either spinning or slowing down
|
||||
bool rolling = false;
|
||||
|
||||
|
||||
// == BRING IT ON! ==
|
||||
|
||||
// start by sleeping (avoid showing one default number)
|
||||
go_sleep();
|
||||
|
||||
show_number(number);
|
||||
|
||||
while(1) {
|
||||
// increment entropy counter
|
||||
entropy++;
|
||||
if(entropy > 5) entropy -= 6;
|
||||
|
||||
// grab button state
|
||||
bool down = BUTTON_DOWN();
|
||||
|
||||
if(!down && !rolling) {
|
||||
|
||||
// dice idle
|
||||
|
||||
// increment idle counter
|
||||
_delay_ms(5);
|
||||
idle_counter += 5;
|
||||
|
||||
if(idle_counter > IDLE_MS_FOR_SLEEP) {
|
||||
// go sleep
|
||||
|
||||
show(0);
|
||||
|
||||
go_sleep();
|
||||
|
||||
show_number(number);
|
||||
|
||||
idle_counter = 0;
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
} else {
|
||||
// button down, or rolling
|
||||
// reset idle counter
|
||||
idle_counter = 0;
|
||||
}
|
||||
|
||||
|
||||
if(down) {
|
||||
|
||||
// button pressed
|
||||
|
||||
// start rolling
|
||||
delay_ms = 1;
|
||||
plus = 1;
|
||||
|
||||
if(!rolling) {
|
||||
// was not rolling before
|
||||
// apply entropy to number
|
||||
number = entropy;
|
||||
rolling = true;
|
||||
}
|
||||
|
||||
_delay_ms(10); // delay to make iterating visible
|
||||
|
||||
} else {
|
||||
|
||||
// button not pressed
|
||||
|
||||
if(delay_ms >= DELAY_MS_FOR_STOP) {
|
||||
// too slow flip, stop the rolling
|
||||
|
||||
rolling = false;
|
||||
|
||||
// blink
|
||||
for(uint8_t i=0; i<4; i++) {
|
||||
show(0);
|
||||
_delay_ms(40);
|
||||
show_number(number);
|
||||
_delay_ms(40);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// slow down a bit
|
||||
delay_ms += plus;
|
||||
plus += 6;
|
||||
}
|
||||
|
||||
|
||||
// advance to next number
|
||||
number++;
|
||||
if(number > 5) number -= 6;
|
||||
|
||||
show_number(number);
|
||||
|
||||
// delay in rolling
|
||||
for(uint16_t i=0; i < delay_ms; i++) {
|
||||
_delay_ms(1);
|
||||
|
||||
// button pressed? Cancel delay.
|
||||
if(BUTTON_DOWN())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user