[Semi Beginner] Please explain the difference between these AVR watchdog modes - Page 1
Forum section: electronics / general

author: microbug
author_rank: Frequent Contributor
timestamp: May 30, 2015, 07:46:00 pm
content: I am working with the ATtiny10. I have used the Arduino before and have some general programming knowledge, this is my first non-Arduino MCU program. The project will run on a CR2032 battery so it needs to have a very low power consumption. I have already turned off the ADC, comparator, timer and digital input buffers so that the power consumption is lower when active, and now I am trying to get a very low power 'sleep' mode (called power-down by the datasheet to distinguish from other modes).What I want the MCU to do is go into power-down for 8 seconds (WDP3..0 = 0b1001) with the watchdog timer running. The watchdog should wake it up, check whether it has gone into power-down enough times, and if so, make a sound before going back to power-down. If not, just go back to power-down.The watchdog can only run for a max of 8 seconds.I have attached a screengrab from the ATtiny10 datasheet. From it, it seems like what I need is WDE = 0 and WDIE = 1 to enable interrupt mode, but I don't understand the difference between what the datasheet calls 'Interrupt Mode' and 'Interrupt and System Reset Mode'.Can anyone explain in a more accessible way than the datasheet does?

author: bobcat
author_rank: Regular Contributor
timestamp: May 30, 2015, 09:54:04 pm
content: Interrupt mode triggers the watchdog interrupt only.Reset mode resets the MPU like at power on.

author: ralphd
author_rank: Frequent Contributor
timestamp: May 30, 2015, 11:51:26 pm
content: This explains it better than I can...https://www.sparkfun.com/tutorials/309

author: ralphd
author_rank: Frequent Contributor
timestamp: May 30, 2015, 11:56:04 pm
content: And I'd avoid the ramless tinies, as they are not well supporred by gcc.The tiny13a is cheap (30c in small qty), and well supported by gcc and avr-libc.

author: microbug
author_rank: Frequent Contributor
timestamp: May 31, 2015, 09:04:41 am
content: Thanks for the tutorial!It's a bit late for me to change MCU, as I'm programming the finished hardware. That said, if I run into problems with RAM size / program space, I might make a new set of PCBs and get a different ATtiny. I don't see much difference between the ATtiny13a and 10 -- just that the ATtiny10 has 32 bytes of RAM instead of 64.

author: westfw
author_rank: Super Contributor
timestamp: May 31, 2015, 09:09:36 am
content: QuoteWhere? Most places seem to want about twice that. (Wah! I want my two-bit ($0.25) attiny11s back!)Unless you're building lots, I'd spring for a tiny85.But the "brain dead" attinys DO have RAM these days. They got rid of some registers, but a tiny10 has a whole 32bytes of RAM as well.I think just because Atmel wanted to appease the C users.WDT Interrupt means cause an interrupt. Reset mode causes a reset."Interrupt AND reset" mode will have the first WDT expiration cause an interrupt, and if you don't tickle the WDT properly, it will cause a RESET the second time the timer expires.

author: microbug
author_rank: Frequent Contributor
timestamp: May 31, 2015, 09:14:23 am
content: Thanks for the clarification, westfw. As it happens, I do have some ATtiny85s in PDIP packages, so if this goes horribly wrong on the ATtiny10 then I could redo the PCBs and get the program working on the ATtiny85s in the meantime. That would be a bit of a pain though as I'd have to get some ATtiny85s in MLF packages as well — space is a limiting factor here.

author: Jeroen3
author_rank: Super Contributor
timestamp: May 31, 2015, 11:58:45 am
content: The Attiny10 are great for LED blinkies. But using C is a challenge. You must use some tricks:- You must attribute main to not stack any return frames. You'll lose valuable ram if you don't.- You shouldn't call any routine.- You shouldn't use nested interrupts.- Interrupts should not use stack. (no locals)And you'll need some knowledge of assembler, because you will need to inspect the compiler output if it doesn't work.Last but not least, do not upgrade compiler for the project ever again when the program is finished. A newer compiler can generate completely different output that won't work anymore.Keep a backup of the compiler toolchain.

author: microbug
author_rank: Frequent Contributor
timestamp: May 31, 2015, 01:17:18 pm
content: I think I am just about sorted for this project now, since the code is all working and is nearly finished.Just to clarify:- What do you mean by 'stack return frames'? Do you just mean that main should return void?- What is a routine? Should I just avoid libraries if I can code it myself (e.g., implement sleep mode / watchdog without their respective libraries)?- Why shouldn't interrupts use the stack (they don't in my code, anyway)?Thanks for the info!

author: Jeroen3
author_rank: Super Contributor
timestamp: May 31, 2015, 05:03:31 pm
content: - It is not necessary for main to have a return address since it won't ever return. Saves you a costly 2 bytes (iirc) of stack.- Any jump to a subroutine or function causes a stackframe with a return address and register stacking. The ATTiny10 does not have much room for this in the 32 byte volatile memory.You can use inlines or macro's.- As I said, there is only 32 bytes of "ram", keep an eye for stack overflows.

author: westfw
author_rank: Super Contributor
timestamp: May 31, 2015, 09:04:47 pm
content: QuoteI disagree with your missive to avoid function calls. You have 32 bytes of RAM, and that's pretty much what it's for. In total, 16 registers and 32 bytes of RAM compares very favorably with other ultra-small uCs (like the PIC10F206, which has 1 register, 24bytes of RAM, and a 2-word return stack.") The usual avr-gcc function call only pushes the return address; the ABI and register allocator prevent the need for stack frames or register stacking (usually.) So you should be able to have a "reasonable" depth of function calling without problems. The compiler may inline your functions anyway.You do especially have to watch interrupts, since those WILL normally stack extra registers (and not very efficiently! :-() Especially avoid calling other functions from ISRs.(All for avr-gcc; other compilers may vary!)QuoteAlways a good idea for very limited systems!QuoteWell, you CAN, but you'll need to repeat that "inspect the compiler output" step again. And hope that you commented the code well enough that you can modify it as needed.

author: ralphd
author_rank: Frequent Contributor
timestamp: May 31, 2015, 10:29:30 pm
content: The tiny4/5/9/10 are a different architecture than the others. Some explaination here:https://github.com/cpldcpu/u-wire/blob/master/README.md

author: ralphd
author_rank: Frequent Contributor
timestamp: May 31, 2015, 10:37:56 pm
content: 20 attiny13a soic for $6.59:http://m.aliexpress.com/item/32213899236.html

author: ralphd
author_rank: Frequent Contributor
timestamp: May 31, 2015, 10:41:09 pm
content: The tiny4/5/9/10 also have a funky programming mode so a $2 usbasp wont work like it does for all the other attinies and atmegas.

author: ralphd
author_rank: Frequent Contributor
timestamp: May 31, 2015, 10:57:54 pm
content: Quote from: westfw on May 31, 2015, 09:09:36 amI just did a quick search on Aliexpress for the tiny85-ssu, and for 659c you can't even get 10. That would cost you a little over $8. If I need more than the tiny13, the t85 is OK, but for $15 you can get 10 Atmega168 Pro Minis with hardware SPI, UART, lots more IO, ...

author: microbug
author_rank: Frequent Contributor
timestamp: June 01, 2015, 03:42:33 am
content: Actually, if you update the firmware (which I've always failed at doing) on the USBASP then it does support TPI. You can buy ones where the seller has updated the firmware (at an additional cost!). I'm using an Arduino sketch which I found online; seems to be working fine.Aliexpress can be hit and miss for parts (I have got 10 Pro Minis using 328s which work fine). Since I would only need to respin the PCBs if I switched to an MSP430, I'd probably just get some samples from TI rather than rack up a sub-£20 delivery charge from Farnell.

author: ralphd
author_rank: Frequent Contributor
timestamp: June 01, 2015, 11:58:26 am
content: On Aliexpress I usually buy one or two at first from sellers if I am unsure of the product. And I rarely buy anything worth more than $10. If something is $16 on aliexpress vs $18 or even $20 from Newark or Mouser, then Ill get it from the official distributor.