License

CC-BY-SA 3.0

Recommend Projects

ATtiny13 TinyDice SMD

1 year ago 968
  • Description
  • Documents
  • BOM
  • Attachments
  • Members
  • Comments

Description

Overview

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

pic2.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.

// libraries
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>

// global variables
volatile uint8_t pips = 0;        // current number of pips

// main function
int main(void) {
  // local variables
  uint8_t matrix[] = {0b00110001, // 1
                      0b00110100, // 2
                      0b00110011, // 3
                      0b00110110, // 4
                      0b00110111, // 5
                      0b00111110};// 6 - for converting pips to pins

  // setup pins
  DDRB   = 0b00001111;            // PB0 - PB3 as output, PB4 input
  PORTB  = 0b00110001;            // pull-up for PB4/5; LED7 on

  // setup timer/counter
  TCCR0A = 0b00000000;            // no output
  TCCR0B = 0b00000011;            // set prescaler to 64
  TIMSK0 = 0b00000010;            // enable timer overflow interrupt

  // setup pin change interrupt
  GIMSK  = 0b00100000;            // turn on pin change interrupts
  PCMSK  = 0b00010000;            // pin change interrupt on button pin
  SREG  |= 0b10000000;            // enable global interrupts

  // disable unused peripherals and set sleep mode to save power
  ACSR   = 0b10000000;            // disable analog comperator
  PRR    = 0b00000001;            // shut down ADC
  set_sleep_mode(SLEEP_MODE_IDLE);// set sleep mode to IDLE

  // main loop
  while(1) {
    sleep_mode();                         // go to sleep
    if (~PINB & 0b00010000) {             // if button pressed:  
      for (uint8_t i = 0; i < 16; i++) {  // roll the dice
        uint8_t del = (i << 4);           // increasing delay between pip-shows
        while (del--) _delay_ms(1);       // set the delay
        PORTB = matrix[pips];             // show current number of pips
      }
      while(~PINB & 0b00010000);          // wait for button released
      _delay_ms(10);                      // debounce
    }
  }
}

// timer0 overflow interrupt service routine
ISR (TIM0_OVF_vect) {
  if (++pips > 5) pips = 0;       // cycle number of pips on every timer overflow
}

// pin change interrupt service routine
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 2.7V
    • 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):
    avrdude -c usbasp -p t13 -U lfuse:w:0x2a:m -U hfuse:w:0xfb: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 the makefile and change the programmer if you are not using usbasp.
  • Open a terminal.
  • Navigate to the folder with the makefile and main.c.
  • Run "make install" to compile, burn the fuses and upload the firmware.

References, Links and Notes

  1. ATtiny13A Datasheet

pic1.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/)

Documents

TinyDice

TinyDice_v1.0

BOM

ID Name Designator Footprint Quantity BOM_Manufacturer Part BOM_Manufacturer BOM_Supplier BOM_Supplier Part
1 MSK-11027 KEY1 MSK-11027 1 MSK-11027 Toggle Switches ReliaPro LCSC C21381
2 TS24CA KEY2 SW-SMD_TS24CA 1 TS24CA SHOU HAN LCSC C393942
3 47R R5,R4,R3 0603 3 0603WAF470JT5E UniOhm LCSC C23182
4 10k R1 0603 1 0603WAF1002T5E UniOhm LCSC C25804
5 1k R2 0603 1 0603WAF1001T5E UniOhm LCSC C21190
6 CR2032-BS-6-1 B1 BATTERY-3 1 CR2032-BS-6-1 Q&J LCSC C70377
7 Red LED2,LED3,LED6,LED7,LED4,LED1,LED5 LED-0805 7 KT-0603R KENTO LCSC C2286
8 100n C1 0603 1 CC0603KRX7R9BB104 YAGEO LCSC C14663
9 47u C2 1206 1 1206F476M160NT FH LCSC C30300
10 ATTINY13A-SSU U1 SOIC-8_150MIL 1 ATTINY13A-SSU MICROCHIP LCSC C14075

Attachments

None
Success
The owner does not allow comments in this project now

Comments (0)

goToTop
你现在访问的是EasyEDA海外版,使用建立访问速度更快的国内版 https://lceda.cn(需要重新注册)
如果需要转移工程请在个人中心 - 工程 - 工程高级设置 - 下载工程,下载后在https://lceda.cn/editor 打开保存即可。
有问题联系QQ 3001956291 不再提醒
svg-battery svg-battery-wifi svg-books svg-more svg-paste svg-pencil svg-plant svg-ruler svg-share svg-user svg-logo-cn svg-double-arrow
We use cookies to offer you a better experience. Detailed information on the use of cookies on this website is provided in our Privacy Policy. By using this site, you consent to the use of our cookies.