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.

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