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.

302 lines
8.5 KiB

3 years ago
  1. #include <xc.h>
  2. #include <avr/io.h>
  3. //this req'd for (and before calling) delay
  4. //see delay.h
  5. //compiler optimizations must also be enabled
  6. #define F_CPU 8000000
  7. #include <util/delay.h>
  8. #include <avr/interrupt.h>
  9. //#include <avr/sleep.h>
  10. //#include <util/atomic.h>
  11. //#include <avr/wdt.h>
  12. //tests5 tested and works (at ~950hz). unplug cable to programmer to see pin 4 active.
  13. //however...
  14. //tests6 DEADEND
  15. //never worked right. giving up and using delay.
  16. //reference: https://www.avrfreaks.net/forum/sample-project-attiny10
  17. //https://blog.podkalicki.com/attiny13-blinky-with-timer-compa/
  18. //watchdog is too slow and inaccurate to get 6000Hz
  19. //ISR(TIM0_COMPA_vect)
  20. //{
  21. // // Toggle PB2 Hi/Low depending on current state
  22. // PINB = 1<<2;
  23. // TIFR0 |= 1<<OCF0A; //clear flag (is this required here? documentation unclear)
  24. // //PORTB ^= _BV(LED_PIN); // toggle LED pin
  25. //}
  26. /*
  27. Delay in powerdown mode. Wake up by watchdog interrupt.
  28. * //NOTE: see earlier code, e.g. tests3 in attiny10 elec projects 2020
  29. */
  30. /*
  31. void delay_power_down_wdt(uint8_t wdto)
  32. {
  33. wdt_reset();
  34. wdt_enable(wdto);
  35. WDTCSR |= (1<<WDIE);
  36. //so far (with 128Khz clk) this sleep will be about 30-40 seconds.
  37. //(however, I'll add the below to)
  38. //adjust sleep speed here:
  39. // 0110 is 1hz at 128KHz
  40. //WDTCSR |= (0<< WDP3);
  41. //WDTCSR |= (1<< WDP2);
  42. //WDTCSR |= (1<< WDP1);
  43. //WDTCSR |= (0<< WDP0);
  44. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  45. sleep_enable();
  46. // Make sure interrups are enabled and the I flag is restored
  47. NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE)
  48. {
  49. sleep_cpu();
  50. wdt_disable();
  51. }
  52. sleep_disable();
  53. }
  54. */
  55. int main(void)
  56. {
  57. //// //Write CCP (to enable changing clock)
  58. CCP = 0xD8;
  59. //// //change CLK to 128KHz
  60. // CLKMSR = 0b01;
  61. //change clk to 8mhz
  62. CLKMSR = 0b00;
  63. //clock prescaler from divide by 8 (defualt) to none
  64. //CLKPS0 = 0x0;
  65. //CLKPS1 = 0x0;
  66. //Just omit the bits that are reserved, see also clkmsr which is 2 bit active
  67. // 8 bit inactive
  68. CLKPSR = 0b0000;
  69. //notes: test results (osccal is 8 bit)
  70. // osccal | hz output
  71. // 0xe6 10khz
  72. // 0x06 3.58khz
  73. // 0x0F 3.83Khz
  74. // 0x2F 4.62Khz
  75. // 0x5F 5.84Khz
  76. // 0x65 6.003KHz -winrar (for this chip))
  77. // 0x8F 7.27Khz
  78. OSCCAL = 0x65;
  79. //////// OCR0A = 0;
  80. //////// TCNT0 = 0;
  81. //////// TCCR0A = 0;
  82. //////// TCCR0B = 0;
  83. ////////
  84. //////// TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
  85. //////// // TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
  86. //////// TCCR0B |= _BV(CS00); // set prescaler to 1 (CLK=1MHz/1)
  87. //OCR0A is 16 bits. in ASM it must be loaded separately.
  88. //in C, it can be loaded > 256? (test)
  89. //520 about 950hz, 25us wide pulse
  90. //OCR0A = 520; // with one timer, ever 166.667hz we want to count once (3000hz)
  91. //OCR0A = 1; //475hz
  92. //this goes to 475hz (pulses). doesn't make sense.
  93. //// OCR0AH = 0xFF; //
  94. //// OCR0AL = 0xFF;
  95. //attiny10 output compare broken apparently
  96. //this still 960hz, with 100us or so pulse size (high is 100us, low is longer)
  97. //OCR0A = 100;
  98. //inline asm to load values into 16 bit register
  99. //; variables
  100. //; For 1 Hz output, the timer delay has to be 1/2 second (1/2 second on / 1/2 second off)
  101. //; delay = OCR0A * 1024 * 8 / 8000000
  102. //; for 1/2 second, OCR0A = 488 (0x01E8)
  103. //.EQU OCR0AHigh = 0x01
  104. //.EQU OCR0ALow = 0xe8
  105. // in bash type: printf "%x\n" 488
  106. //.EQU OCR0AHigh = 0x02
  107. //.EQU OCR0ALow = 0x08
  108. // ldi r17, OCR0AHigh //; Sets the output compare register value
  109. //ldi r16, OCR0ALow
  110. //this asm doesn't work (why?) //perhaps OCR0A is really just 8 bits on attiny10...?)
  111. ////// asm volatile(
  112. ////// "ldi r17, 0x02"
  113. ////// "ldi r16, 0x08"
  114. ////// "out OCR0AH, r17"
  115. ////// "out OCR0AL, r16"
  116. ////// );
  117. //this asm works
  118. // asm volatile("nop\n\t"
  119. //"nop\n\t"
  120. //"nop\n\t"
  121. //"nop\n\t"
  122. //::);
  123. ////////
  124. //////// TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
  125. ////////
  126. // PB2 change to output
  127. DDRB = 1<<2;
  128. //// sei();
  129. int x = 0;
  130. while(1)
  131. {
  132. //this goes about 60.3KHz.
  133. //experiment FAIL
  134. //will just buy a 6KHz oscillator.
  135. PINB = 1<<2;
  136. //EDIT: disabled optimization in mplab (level 0)
  137. //and ended up geting much slower IO
  138. //asm("nop;");
  139. //for(x=0;x<10;x++){
  140. //note it's _delay_us not delay_us
  141. //delay_us(1) is 44.3KHz
  142. //_delay_us(8); //9 is 6.4KHz, 8 is roughly 7.2KHz
  143. //delay_us(10) is 5.8KHz
  144. //not much granularity there...)
  145. //so adding the following...
  146. if( x == 3){
  147. _delay_us(4);//these two are 5.98 or so KHz
  148. x = 0;
  149. goto jump;
  150. }
  151. //NOTE: the above ends up causing an ugly looking clock. may not matter
  152. //for fpga clocking off of rising edge, BUT, it is ugly, should remove,
  153. //and just do single delay. Will do in part 9.
  154. x++;
  155. jump:
  156. //in combination with the jump, gives closer to 6KHz
  157. _delay_us(8);
  158. ///below didn't work out, so using osccal
  159. //bit convuluted anyways
  160. /*if( x == 4){
  161. _delay_us(1);
  162. //x = 0;
  163. goto jump;
  164. }
  165. if( x == 8){
  166. _delay_us(2);
  167. x = 0;
  168. goto jump;
  169. }
  170. x++;
  171. jump:
  172. //in combination with the jump, gives closer to 6KHz
  173. _delay_us(8);*/
  174. //the above idles at 5.98 - 5.99KHz.
  175. //best so far.
  176. //Now, calibrate oscillator with OSCCAL
  177. //if more accuracy desired.
  178. //or adjust if clause above
  179. //tried to make my own delay in ASM, BUT
  180. //compiler keeps optimizing it out (even w/out optimization enabled)
  181. //... some other flag may be culprit. So instead will use delay_us. Two delay
  182. //functions are delay_ms and delay_us. see headers at top and delay.h)
  183. /* __asm__("mov r16, r17;"
  184. "mov r17, r16;"
  185. "inc r16;"
  186. "dec r16;"
  187. "mov r16, r17;"
  188. "mov r17, r16;"
  189. "inc r16;"
  190. "dec r16;"
  191. "clr r16;"
  192. "clr r17;"
  193. "clr r18;"
  194. "clr r19;"
  195. "clr r20;"
  196. "clr r21;"
  197. "clr r22;"
  198. "clr r23;"
  199. "mov r16, r17;"
  200. "mov r17, r16;"
  201. "inc r16;"
  202. "dec r16;"
  203. "mov r16, r17;"
  204. "mov r17, r16;"
  205. "clr r16;"
  206. "clr r17;"
  207. "clr r18;"
  208. "clr r19;"
  209. "clr r20;"
  210. "clr r21;"
  211. "clr r22;"
  212. "clr r23;"
  213. "mov r16, r17;"
  214. "mov r17, r16;"
  215. "inc r16;"
  216. "dec r16;"
  217. "mov r16, r17;"
  218. "mov r17, r16;"
  219. );*/
  220. //mplab delay is:
  221. /** \ingroup util_delay_basic
  222. Delay loop using an 8-bit counter \c __count, so up to 256
  223. iterations are possible. (The value 256 would have to be passed
  224. as 0.) The loop executes three CPU cycles per iteration, not
  225. including the overhead the compiler needs to setup the counter
  226. register.
  227. Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
  228. can be achieved.
  229. */
  230. //////////void
  231. //////////_delay_loop_1(uint8_t __count)
  232. //////////{
  233. ////////// __asm__ volatile (
  234. ////////// "1: dec %0" "\n\t"
  235. ////////// "brne 1b"
  236. ////////// : "=r" (__count)
  237. ////////// : "0" (__count)
  238. ////////// );
  239. //////////}
  240. //////////
  241. //////////
  242. //ignore below
  243. // asm("nop;""nop;""nop;""nop;""nop;"
  244. //"nop;""nop;"//"nop;""nop;"//"nop;""nop;"
  245. //);
  246. //}//just this asm block is about 121.22KHz
  247. }
  248. }