项目作者: wagiminator

项目描述 :
Tiny electronic dice
高级语言: C++
项目地址: git://github.com/wagiminator/ATtiny13-TinyDice.git
创建时间: 2020-03-24T14:02:03Z
项目社区:https://github.com/wagiminator/ATtiny13-TinyDice

开源协议:Other

下载


TinyDice - Electronic Dice based on ATtiny13A

TinyDice is a tiny (35mm * 17mm) electronic dice powered by ATtiny13A.

pic1.jpg

Hardware

The wiring is pretty simple:

wiring.png

The fact that the opposite pairs of dots on a dice always appear together was used for the circuit diagram. This means that there is no need for Multi or Charlieplexing. However, the supply voltage must be at least twice as high as the forward voltage of the LEDs. Therefore only red LEDs and the rechargeable LIR2032 li-ion batteries should be used.

Software

Implementation

Timer0 is used to constantly change the number of pips in the background. Chance is created by the uncertainty of the moment the button is pressed by the user, which brings the current number of pips to display. As long as nothing else needs to be done, the ATtiny remains in IDLE and only wakes up when you press a button (pin change interrupt). Then it rolls the dice, in which a series of numbers are shown on the dice with increasing time interval. Finally, the last number shown remains and the ATtiny changes back to IDLE. The number of pips shown on the dice corresponds to the respective variable pips, which is constantly changed by the timer overflow interrupt. A simple matrix is used to control the LEDs, with which the respective number is converted into the values for the PORTB register.

  1. // Libraries
  2. #include <avr/io.h> // for GPIO
  3. #include <avr/sleep.h> // for sleep mode
  4. #include <avr/interrupt.h> // for interrupts
  5. #include <util/delay.h> // for delays
  6. // Global variables
  7. volatile uint8_t pips = 0; // current number of pips
  8. // Main function
  9. int main(void) {
  10. // Local variables
  11. uint8_t matrix[] = {0b00110001, // 1
  12. 0b00110100, // 2
  13. 0b00110011, // 3
  14. 0b00110110, // 4
  15. 0b00110111, // 5
  16. 0b00111110};// 6 - for converting pips to pins
  17. // Setup pins
  18. DDRB = 0b00001111; // PB0 - PB3 as output, PB4 input
  19. PORTB = 0b00110001; // pull-up for PB4/5; LED7 on
  20. // Setup timer/counter
  21. TCCR0A = 0b00000000; // no output
  22. TCCR0B = 0b00000011; // set prescaler to 64
  23. TIMSK0 = 0b00000010; // enable timer overflow interrupt
  24. // Setup pin change interrupt
  25. GIMSK = 0b00100000; // turn on pin change interrupts
  26. PCMSK = 0b00010000; // pin change interrupt on button pin
  27. SREG |= 0b10000000; // enable global interrupts
  28. // Disable unused peripherals and set sleep mode to save power
  29. ACSR = 0b10000000; // disable analog comperator
  30. PRR = 0b00000001; // shut down ADC
  31. set_sleep_mode(SLEEP_MODE_IDLE);// set sleep mode to IDLE
  32. // Loop
  33. while(1) {
  34. sleep_mode(); // go to sleep
  35. if(~PINB & 0b00010000) { // if button pressed:
  36. for(uint8_t i = 0; i < 16; i++) { // roll the dice
  37. uint8_t del = (i << 4); // increasing delay between pip-shows
  38. while(del--) _delay_ms(1); // set the delay
  39. PORTB = matrix[pips]; // show current number of pips
  40. }
  41. while(~PINB & 0b00010000); // wait for button released
  42. _delay_ms(10); // debounce
  43. }
  44. }
  45. }
  46. // Timer0 overflow interrupt service routine
  47. ISR(TIM0_OVF_vect) {
  48. if(++pips > 5) pips = 0; // cycle number of pips on every timer overflow
  49. }
  50. // Pin change interrupt service routine
  51. EMPTY_INTERRUPT(PCINT0_vect); // nothing to be done here, just wake up from sleep

Compiling and Uploading

Since there is no ICSP header on the board, you have to program the ATtiny either before soldering using an SOP adapter, or after soldering using an EEPROM clip. The AVR Programmer Adapter can help with this.

If using the Arduino IDE

  • Make sure you have installed MicroCore.
  • Go to Tools -> Board -> MicroCore and select ATtiny13.
  • Go to Tools and choose the following board options:
    • Clock: 1.2 MHz internal osc.
    • BOD: BOD disabled
    • Timing: Micros disabled
  • Connect your programmer to your PC and to the ATtiny.
  • Go to Tools -> Programmer and select your ISP programmer (e.g. USBasp).
  • Go to Tools -> Burn Bootloader to burn the fuses.
  • Open TinyDice.ino and click Upload.

If using the precompiled hex-file

  • Make sure you have installed avrdude.
  • Connect your programmer to your PC and to the ATtiny.
  • Open a terminal.
  • Navigate to the folder with the hex-file.
  • Execute the following command (if necessary replace “usbasp” with the programmer you use):
    1. avrdude -c usbasp -p t13 -U lfuse:w:0x2a:m -U hfuse:w:0xff:m -U flash:w:tinydice.hex

If using the makefile (Linux/Mac)

  • Make sure you have installed avr-gcc toolchain and avrdude.
  • Connect your programmer to your PC and to the ATtiny.
  • Open a terminal.
  • Navigate to the folder with the makefile and sketch.
  • Run PROGRMR=usbasp make install to compile, burn the fuses and upload the firmware (change PROGRMR accordingly).

References, Links and Notes

  1. ATtiny13A Datasheet

pic3.jpg
pic4.jpg

License

license.png

This work is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported License.
(http://creativecommons.org/licenses/by-sa/3.0/)