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.

135 lines
3.4 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 (at ~950hz). unplug cable to programmer to see pin 4 active.
  9. //however...
  10. //tests6 DEADEND
  11. //never worked right. giving up and using delay.
  12. //reference: https://www.avrfreaks.net/forum/sample-project-attiny10
  13. //https://blog.podkalicki.com/attiny13-blinky-with-timer-compa/
  14. //watchdog is too slow and inaccurate to get 6000Hz
  15. ISR(TIM0_COMPA_vect)
  16. {
  17. // Toggle PB2 Hi/Low depending on current state
  18. PINB = 1<<2;
  19. TIFR0 |= 1<<OCF0A; //clear flag (is this required here? documentation unclear)
  20. //PORTB ^= _BV(LED_PIN); // toggle LED pin
  21. }
  22. /*
  23. Delay in powerdown mode. Wake up by watchdog interrupt.
  24. * //NOTE: see earlier code, e.g. tests3 in attiny10 elec projects 2020
  25. */
  26. /*
  27. void delay_power_down_wdt(uint8_t wdto)
  28. {
  29. wdt_reset();
  30. wdt_enable(wdto);
  31. WDTCSR |= (1<<WDIE);
  32. //so far (with 128Khz clk) this sleep will be about 30-40 seconds.
  33. //(however, I'll add the below to)
  34. //adjust sleep speed here:
  35. // 0110 is 1hz at 128KHz
  36. //WDTCSR |= (0<< WDP3);
  37. //WDTCSR |= (1<< WDP2);
  38. //WDTCSR |= (1<< WDP1);
  39. //WDTCSR |= (0<< WDP0);
  40. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  41. sleep_enable();
  42. // Make sure interrups are enabled and the I flag is restored
  43. NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE)
  44. {
  45. sleep_cpu();
  46. wdt_disable();
  47. }
  48. sleep_disable();
  49. }
  50. */
  51. int main(void)
  52. {
  53. OCR0A = 0;
  54. TCNT0 = 0;
  55. TCCR0A = 0;
  56. TCCR0B = 0;
  57. TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
  58. // TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
  59. TCCR0B |= _BV(CS00); // set prescaler to 1 (CLK=1MHz/1)
  60. //OCR0A is 16 bits. in ASM it must be loaded separately.
  61. //in C, it can be loaded > 256? (test)
  62. //520 about 950hz, 25us wide pulse
  63. //OCR0A = 520; // with one timer, ever 166.667hz we want to count once (3000hz)
  64. //OCR0A = 1; //475hz
  65. //this goes to 475hz (pulses). doesn't make sense.
  66. //// OCR0AH = 0xFF; //
  67. //// OCR0AL = 0xFF;
  68. //attiny10 output compare broken apparently
  69. //this still 960hz, with 100us or so pulse size (high is 100us, low is longer)
  70. //OCR0A = 100;
  71. //inline asm to load values into 16 bit register
  72. //; variables
  73. //; For 1 Hz output, the timer delay has to be 1/2 second (1/2 second on / 1/2 second off)
  74. //; delay = OCR0A * 1024 * 8 / 8000000
  75. //; for 1/2 second, OCR0A = 488 (0x01E8)
  76. //.EQU OCR0AHigh = 0x01
  77. //.EQU OCR0ALow = 0xe8
  78. // in bash type: printf "%x\n" 488
  79. //.EQU OCR0AHigh = 0x02
  80. //.EQU OCR0ALow = 0x08
  81. // ldi r17, OCR0AHigh //; Sets the output compare register value
  82. //ldi r16, OCR0ALow
  83. //this asm doesn't work (why?) //perhaps OCR0A is really just 8 bits on attiny10...?)
  84. ////// asm volatile(
  85. ////// "ldi r17, 0x02"
  86. ////// "ldi r16, 0x08"
  87. ////// "out OCR0AH, r17"
  88. ////// "out OCR0AL, r16"
  89. ////// );
  90. //this asm works
  91. // asm volatile("nop\n\t"
  92. //"nop\n\t"
  93. //"nop\n\t"
  94. //"nop\n\t"
  95. //::);
  96. TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
  97. // PB2 change to output
  98. DDRB = 1<<2;
  99. sei();
  100. while(1)
  101. {
  102. //handled by interrupt
  103. }
  104. }