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.

83 lines
2.0 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. //tests5 tested and works (though 950Hz). unplug cable to programmer to see pin 4 active.
  9. //reference: https://www.avrfreaks.net/forum/sample-project-attiny10
  10. //https://blog.podkalicki.com/attiny13-blinky-with-timer-compa/
  11. //watchdog is too slow and inaccurate to get 6000Hz
  12. ISR(TIM0_COMPA_vect)
  13. {
  14. // Toggle PB2 Hi/Low depending on current state
  15. PINB = 1<<2;
  16. //PORTB ^= _BV(LED_PIN); // toggle LED pin
  17. }
  18. /*
  19. Delay in powerdown mode. Wake up by watchdog interrupt.
  20. * //NOTE: see earlier code, e.g. tests3 in attiny10 elec projects 2020
  21. */
  22. /*
  23. void delay_power_down_wdt(uint8_t wdto)
  24. {
  25. wdt_reset();
  26. wdt_enable(wdto);
  27. WDTCSR |= (1<<WDIE);
  28. //so far (with 128Khz clk) this sleep will be about 30-40 seconds.
  29. //(however, I'll add the below to)
  30. //adjust sleep speed here:
  31. // 0110 is 1hz at 128KHz
  32. //WDTCSR |= (0<< WDP3);
  33. //WDTCSR |= (1<< WDP2);
  34. //WDTCSR |= (1<< WDP1);
  35. //WDTCSR |= (0<< WDP0);
  36. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  37. sleep_enable();
  38. // Make sure interrups are enabled and the I flag is restored
  39. NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE)
  40. {
  41. sleep_cpu();
  42. wdt_disable();
  43. }
  44. sleep_disable();
  45. }
  46. */
  47. int main(void)
  48. {
  49. //Write CCP (to enable changing clock)
  50. //CCP = 0xD8;
  51. //change CLK to 128KHz
  52. //CLKMSR = 0b01;
  53. TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
  54. // TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
  55. TCCR0B |= _BV(CS00); // set prescaler to 1 (CLK=1MHz/1)
  56. OCR0A = 167; // with one timer, ever 166.667hz we want to count once (3000hz)
  57. TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
  58. // PB2 change to output
  59. DDRB = 1<<2;
  60. sei();
  61. while(1)
  62. {
  63. //handled by interrupt
  64. }
  65. }