You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.6 KiB

3 years ago
  1. #include <xc.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/sleep.h>
  6. #include <util/atomic.h>
  7. #include <avr/wdt.h>
  8. //reference: https://www.avrfreaks.net/forum/sample-project-attiny10
  9. //another ref: https://blog.podkalicki.com/attiny13-blinky-with-timer-compa/
  10. //
  11. // Wake up by WDT interrupt.
  12. // Don't need to do anything here but (auto-) clearing the interrupt flag.
  13. EMPTY_INTERRUPT(WDT_vect);
  14. /*
  15. Delay in powerdown mode. Wake up by watchdog interrupt.
  16. */
  17. void delay_power_down_wdt(uint8_t wdto)
  18. {
  19. wdt_reset();
  20. wdt_enable(wdto);
  21. WDTCSR |= (1<<WDIE);
  22. //so far (with 128Khz clk) this sleep will be about 30-40 seconds.
  23. //(however, I'll add the below to)
  24. //adjust sleep speed here:
  25. // 0110 is 1hz at 128KHz
  26. //WDTCSR |= (0<< WDP3);
  27. //WDTCSR |= (1<< WDP2);
  28. //WDTCSR |= (1<< WDP1);
  29. //WDTCSR |= (0<< WDP0);
  30. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  31. sleep_enable();
  32. // Make sure interrups are enabled and the I flag is restored
  33. NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE)
  34. {
  35. sleep_cpu();
  36. wdt_disable();
  37. }
  38. sleep_disable();
  39. }
  40. int main(void)
  41. {
  42. sei();
  43. //Write CCP (to enable changing clock)
  44. CCP = 0xD8;
  45. //change CLK to 128KHz
  46. CLKMSR = 0b01;
  47. //In order to use the lowest sleep mode, we must either
  48. //use watchdog or pin change interrupt to wake ic.
  49. //here i will use watchdog
  50. // PB2 change to output
  51. DDRB = 1<<2;
  52. while(1)
  53. {
  54. // Toggle PB2 Hi/Low
  55. PINB = 1<<2;
  56. //_delay_ms(500);
  57. delay_power_down_wdt(WDTO_1S);
  58. }
  59. }