Editor Version ×
recommended

Pro Edition

free

Brand new interactions and interfaces

Smooth support for design sizes of over 3W

devices or 10W pads

More rigorous design constraints, more

standardized processes

For enterprises, more professional users

Std Edition

Easy to use and quick to get started

The process supports design scales of 300

devices or 1000 pads

Supports simple circuit simulation

For students, teachers, creators

Ongoing

STD ATtiny814 USB-RTC

ATtiny814 USB-RTC

Project tags

License

CC-BY-SA 3.0

License: CC-BY-SA 3.0

Mode:

Mode

Editors' pick

Editors' pick

  • 2.3k
  • 0
  • 3
Update time: 2022-12-05 17:20:07
Creation time: 2021-06-29 18:00:14
Description

Description

# Overview The USB-RTC is a simple real-time clock that can supply devices without one (e.g. Raspberry Pi) with the current time and date via USB. The CR1220, CR1225 or LIR1220 (recommended) backup battery keeps the clock running even without an external power supply. The built-in 32.768 kHz crystal ensures a reasonable accuracy of the clock. The on board CH330N (or CH340N) USB-to-serial adapter can also be used as a UPDI programmer, so that no external programming device is required. This makes the USB-RTC also suitable as a development board for RTC applications based on the new tinyAVR or megaAVR microcontrollers. - Firmware (Github): https://github.com/wagiminator/ATtiny814-USB-RTC ![pic1.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny814-USB-RTC/main/documentation/USB-RTC_pic1.jpg) ![pic2.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny814-USB-RTC/main/documentation/USB-RTC_pic2.jpg) # Hardware The wiring is pretty simple: ![wiring.png](https://raw.githubusercontent.com/wagiminator/ATtiny814-USB-RTC/main/documentation/USB-RTC_wiring.png) The power path control ensures that the battery is disconnected as soon as the device is supplied with power via USB. By using a MOSFET, the battery can supply the ATtiny with almost no voltage drop when no power is available via USB. The CH330N can be replaced by a CH340N. With the toggle switch the user can select UART mode for data transfer or UPDI mode for programming the device. C3/C4 are calculated for a crystal with a load capacitance (CL) of 12.5pF (C3 = C4 = 2 * CL - 7pF). # Software ## RTC Implementation In order to make the RTC tick, the external 32.768 kHz crystal must first be selected as the clock source. Then the Periodic Interrupt Timer (PIT) is started with a prescaler of 32768, which triggers an interrupt exactly once per second and wakes the ATtiny from sleep. The clock is then advanced by one second in the main routine. For more information on the RTC refer to [Microchip Technical Brief TB3213](https://ww1.microchip.com/downloads/en/Appnotes/TB3213-Getting-Started-with-RTC-DS90003213.pdf). ```c // Setup external 32.768 kHz crystal and periodic interrupt timer (PIT) void RTC_init(void) { _PROTECTED_WRITE(CLKCTRL_XOSC32KCTRLA, CLKCTRL_ENABLE_bm); // enable crystal RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc; // select external 32K crystal RTC.PITINTCTRL = RTC_PI_bm; // enable periodic interrupt RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc // set period to 1 second | RTC_PITEN_bm; // enable PIT } // Interrupt service routine for PIT (wake up from sleep every second) ISR(RTC_PIT_vect) { RTC.PITINTFLAGS = RTC_PI_bm; // clear interrupt flag } ``` ## Time and Date The time and date functions surprisingly take care of the time and date. When the firmware is uploaded, the time is automatically set to the current time (compilation time). The time and date are updated every second, triggered by the Periodic Interrupt Timer (PIT). Leap years are taken into account and weekdays are calculated. For more information on the relevant calculations, see [Microchip Application Note AN2543](https://ww1.microchip.com/downloads/en/Appnotes/AN2543-Temperature-Logger-with-ATtiny817-and-SD-Card-v2-00002543C.pdf) and [Sakamoto's method on wikipedia](https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto's_methods). ```c // Returns TRUE if year is NOT a leap year (refer to AN2543) uint8_t TIME_notLeapYear(void) { if (!(t.year % 100)) return (t.year % 400); return (t.year % 4); } // Returns day of the week (Sakamoto's method, refer to wikipedia) uint8_t TIME_dayOfTheWeek(void) { static uint8_t td[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; uint16_t y = t.year; if (t.month 4.3V (65=256*1.1V/4.3V) } ``` ## Power Saving In order to save energy, the ATtiny spends most of its time in sleep mode power down when it is running on batteries. The Periodic Interrupt Timer (PIT), which is driven by the 32.768 kHz crystal, wakes the ATtiny once per second to update the time. In addition, all pins that are not required are switched off. For more information on power saving refer to [Microchip Training Manual](https://ww1.microchip.com/downloads/en/DeviceDoc/ADC-Power-Optimization-with-tinyAVR-0-1-series-and-megaAVR-0-series.pdf). ```c // Disable unused pins to save power for (uint8_t pin=7; pin; pin--) (&PORTA.PIN0CTRL)[pin] = PORT_ISC_INPUT_DISABLE_gc; PORTB.PIN0CTRL = PORT_ISC_INPUT_DISABLE_gc; PORTB.PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc; // Prepare sleep mode SLPCTRL.CTRLA = SLPCTRL_SMODE_PDOWN_gc // set sleep mode to power down | SLPCTRL_SEN_bm; // enable sleep mode ``` According to the measurements with the [Power Profiler Kit II](https://www.nordicsemi.com/Products/Development-hardware/Power-Profiler-Kit-2), an average current of 2.5µA at a voltage of 3V is consumed in battery operation. A typical CR1225 battery has a capacity of 50mAh. This results in a theoretical battery life of 20,000 hours or 833 days or 2.34 years. A rechargeable LIR1220 battery, on the other hand, only has a capacity of 8mAh, which leads to a service life of around 3,200 hours or 133 days. However, this is sufficient for most applications and since a LIR1220 can withstand higher current peaks, which leads to more stable operation, it is to be preferred overall. ![power1.png](https://raw.githubusercontent.com/wagiminator/ATtiny814-USB-RTC/main/documentation/USB-RTC_power1.png) ![power2.png](https://raw.githubusercontent.com/wagiminator/ATtiny814-USB-RTC/main/documentation/USB-RTC_power2.png) ## Compiling and Uploading - Install the CR1220, CR1225 or LIR1220 (recommended) buffer battery. - Set the selector switch on the device to UPDI. - Plug the device into a USB port of your PC. ### If using the Arduino IDE - Open your Arduino IDE. - Make sure you have installed [megaTinyCore](https://github.com/SpenceKonde/megaTinyCore). - Go to **Tools -> Board -> megaTinyCore** and select **ATtiny1614/1604/814/804/414/404/214/204**. - Go to **Tools** and choose the following board options: - **Chip:** ATtiny814 or ATtiny414 or ATtiny214 - **Clock:** 5 MHz internal - **Programmer:** SerialUPDI - Leave the rest at the default settings. - Go to **Tools -> Burn Bootloader** to burn the fuses. - Open USB-RTC sketch and click **Upload**. - Set the selector switch on the device back to UART. ### If using the makefile (Linux/Mac) - Download [AVR 8-bit Toolchain](https://www.microchip.com/mplab/avr-support/avr-and-arm-toolchains-c-compilers) and extract the sub-folders (avr, bin, include, ...) to /software/tools/avr-gcc. To do this, you have to register for free with Microchip on the download site. - Open a terminal. - Navigate to the folder with the makefile and the Arduino sketch. - Run "make install" to compile, burn the fuses and upload the firmware. - Set the selector switch on the device back to UART. The time and date of the USB-RTC are automatically set to the current time (compilation time) when the firmware is uploaded. # Operating Instructions 1. Set the selector switch on the device to UART. 2. Plug the device into a USB port of your PC. 3. Open a serial monitor and set it to 9600 BAUD. # References, Links and Notes 1. [ATtiny814 Datasheet](https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny417-814-816-817-DataSheet-DS40002288A.pdf) 2. [TB3213 - Getting Started with RTC](https://ww1.microchip.com/downloads/en/Appnotes/TB3213-Getting-Started-with-RTC-DS90003213.pdf) 3. [TB3216 - Getting Started with USART](https://ww1.microchip.com/downloads/en/Appnotes/TB3216-Getting-Started-with-USART-DS90003216.pdf) 4. [AN2447 - Measure VCC/Battery Voltage](https://ww1.microchip.com/downloads/en/Appnotes/00002447A.pdf) 5. [AN2543 - Temperature Logger](https://ww1.microchip.com/downloads/en/Appnotes/AN2543-Temperature-Logger-with-ATtiny817-and-SD-Card-v2-00002543C.pdf) 6. [ADC and Power Optimization](https://ww1.microchip.com/downloads/en/DeviceDoc/ADC-Power-Optimization-with-tinyAVR-0-1-series-and-megaAVR-0-series.pdf) 7. [Determination of the Day of the Week](https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto's_methods) ![pic3.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny814-USB-RTC/main/documentation/USB-RTC_pic3.jpg) # License ![license.png](https://i.creativecommons.org/l/by-sa/3.0/88x31.png) This work is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported License. (http://creativecommons.org/licenses/by-sa/3.0/)
Design Drawing

Design Drawing

schematic diagram
1 /
PCB
1 /
The preview image was not generated, please save it again in the editor.
ID Name Designator Footprint Quantity BOM_Manufacturer Part BOM_Supplier BOM_Supplier Part
1 CR1220 B1 CR1220-SMD 1 MY-1220-03 LCSC C964818
2 100n C1,C5,C6 C_0603 3 CC0603KRX7R9BB104 LCSC C14663
3 10u C2 C_0603 1 CL10A106MA8NRNC LCSC C96446
4 18p C3,C4 C_0603 2 CL10C180JB8NNNC LCSC C1647
5 5817 D1 SCHOTTKY-SOD-123 1 1N5817W LCSC C727113
6 DMP1045U-7 Q1 SOT-23-3_W 1 DMP1045U-7 LCSC C177033
7 1k R1 0603 1 0603WAF1001T5E LCSC C21190
8 10k R2 0603 1 0603WAF1002T5E LCSC C25804
9 K3-2380S-E1 SW1 SW-SMD_K3-2380S-E1 1 K3-2380S-E1 LCSC C145837
10 ATTINY814-SSN U1 SOIC-14_150MIL 1 ATTINY814-SSNR LCSC C182202
11 CH340N U2 SOP-8_150MIL 1 CH340N LCSC C506813
12 USB-Plug USB1 USB-M-48-2 1 USB-05 LCSC C112454
13 32.768KHz X1 OSC-SMD_3215 1 AH03270014 LCSC C409356

Unfold

Project Attachments

Project Attachments

Empty
Project Members

Project Members

Target complaint
Related Projects
Change a batch
Loading...

Comment

Add to album ×

Loading...

reminder ×

Do you need to add this project to the album?

服务时间

周一至周五 9:00~18:00
  • 0755 - 2382 4495
  • 153 6159 2675

服务时间

周一至周五 9:00~18:00
  • 立创EDA微信号

    easyeda

  • QQ交流群

    664186054

  • 立创EDA公众号

    lceda-cn