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.

150 lines
4.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 (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. ////////
  58. //////// TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
  59. //////// // TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
  60. //////// TCCR0B |= _BV(CS00); // set prescaler to 1 (CLK=1MHz/1)
  61. //OCR0A is 16 bits. in ASM it must be loaded separately.
  62. //in C, it can be loaded > 256? (test)
  63. //520 about 950hz, 25us wide pulse
  64. //OCR0A = 520; // with one timer, ever 166.667hz we want to count once (3000hz)
  65. //OCR0A = 1; //475hz
  66. //this goes to 475hz (pulses). doesn't make sense.
  67. //// OCR0AH = 0xFF; //
  68. //// OCR0AL = 0xFF;
  69. //attiny10 output compare broken apparently
  70. //this still 960hz, with 100us or so pulse size (high is 100us, low is longer)
  71. //OCR0A = 100;
  72. //inline asm to load values into 16 bit register
  73. //; variables
  74. //; For 1 Hz output, the timer delay has to be 1/2 second (1/2 second on / 1/2 second off)
  75. //; delay = OCR0A * 1024 * 8 / 8000000
  76. //; for 1/2 second, OCR0A = 488 (0x01E8)
  77. //.EQU OCR0AHigh = 0x01
  78. //.EQU OCR0ALow = 0xe8
  79. // in bash type: printf "%x\n" 488
  80. //.EQU OCR0AHigh = 0x02
  81. //.EQU OCR0ALow = 0x08
  82. // ldi r17, OCR0AHigh //; Sets the output compare register value
  83. //ldi r16, OCR0ALow
  84. //this asm doesn't work (why?) //perhaps OCR0A is really just 8 bits on attiny10...?)
  85. ////// asm volatile(
  86. ////// "ldi r17, 0x02"
  87. ////// "ldi r16, 0x08"
  88. ////// "out OCR0AH, r17"
  89. ////// "out OCR0AL, r16"
  90. ////// );
  91. //this asm works
  92. // asm volatile("nop\n\t"
  93. //"nop\n\t"
  94. //"nop\n\t"
  95. //"nop\n\t"
  96. //::);
  97. ////////
  98. //////// TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
  99. ////////
  100. // PB2 change to output
  101. DDRB = 1<<2;
  102. //// sei();
  103. int x = 0;
  104. while(1)
  105. {
  106. //this goes about 60.3KHz.
  107. //experiment FAIL
  108. //will just buy a 6KHz oscillator.
  109. PINB = 1<<2;
  110. //EDIT: disabled optimization in mplab (level 0)
  111. //and ended up geting much slower IO
  112. //asm("nop;");
  113. //for(x=0;x<2;x++){
  114. // asm("nop;""nop;""nop;""nop;""nop;"
  115. //"nop;""nop;"//"nop;""nop;"//"nop;""nop;"
  116. // );
  117. //}//just this asm block is about 121.22KHz
  118. }
  119. }