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.

305 lines
8.7 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 1536000
  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. //use external clk:
  64. CLKMSR = 0b10;
  65. //again need CCP (to enable changing prescaler (this time))
  66. CCP = 0xD8;
  67. //clock prescaler from divide by 8 (defualt) to none
  68. // prescaler of 1:
  69. //CLKPSR = 0b0000;
  70. // prescaler of 256:
  71. CLKPSR = 0b1000;
  72. //notes: test results (osccal is 8 bit)
  73. // osccal | hz output
  74. // 0xe6 10khz
  75. // 0x06 3.58khz
  76. // 0x0F 3.83Khz
  77. // 0x2F 4.62Khz
  78. // 0x5F 5.84Khz
  79. // 0x65 6.003KHz -winrar (for this chip))
  80. // 0x8F 7.27Khz
  81. //OSCCAL = 0x65;
  82. //////// OCR0A = 0;
  83. //////// TCNT0 = 0;
  84. //////// TCCR0A = 0;
  85. //////// TCCR0B = 0;
  86. ////////
  87. //////// TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
  88. //////// // TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
  89. //////// TCCR0B |= _BV(CS00); // set prescaler to 1 (CLK=1MHz/1)
  90. //OCR0A is 16 bits. in ASM it must be loaded separately.
  91. //in C, it can be loaded > 256? (test)
  92. //520 about 950hz, 25us wide pulse
  93. //OCR0A = 520; // with one timer, ever 166.667hz we want to count once (3000hz)
  94. //OCR0A = 1; //475hz
  95. //this goes to 475hz (pulses). doesn't make sense.
  96. //// OCR0AH = 0xFF; //
  97. //// OCR0AL = 0xFF;
  98. //attiny10 output compare broken apparently
  99. //this still 960hz, with 100us or so pulse size (high is 100us, low is longer)
  100. //OCR0A = 100;
  101. //inline asm to load values into 16 bit register
  102. //; variables
  103. //; For 1 Hz output, the timer delay has to be 1/2 second (1/2 second on / 1/2 second off)
  104. //; delay = OCR0A * 1024 * 8 / 8000000
  105. //; for 1/2 second, OCR0A = 488 (0x01E8)
  106. //.EQU OCR0AHigh = 0x01
  107. //.EQU OCR0ALow = 0xe8
  108. // in bash type: printf "%x\n" 488
  109. //.EQU OCR0AHigh = 0x02
  110. //.EQU OCR0ALow = 0x08
  111. // ldi r17, OCR0AHigh //; Sets the output compare register value
  112. //ldi r16, OCR0ALow
  113. //this asm doesn't work (why?) //perhaps OCR0A is really just 8 bits on attiny10...?)
  114. ////// asm volatile(
  115. ////// "ldi r17, 0x02"
  116. ////// "ldi r16, 0x08"
  117. ////// "out OCR0AH, r17"
  118. ////// "out OCR0AL, r16"
  119. ////// );
  120. //this asm works
  121. // asm volatile("nop\n\t"
  122. //"nop\n\t"
  123. //"nop\n\t"
  124. //"nop\n\t"
  125. //::);
  126. ////////
  127. //////// TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
  128. ////////
  129. // PB2 change to output
  130. // DDRB = 1<<2;
  131. //// sei();
  132. //int x = 0;
  133. while(1)
  134. {
  135. //this goes about 60.3KHz.
  136. //experiment FAIL
  137. //will just buy a 6KHz oscillator.
  138. PINB = 1<<2;
  139. //EDIT: disabled optimization in mplab (level 0)
  140. //and ended up geting much slower IO
  141. //asm("nop;");
  142. //for(x=0;x<10;x++){
  143. //note it's _delay_us not delay_us
  144. //delay_us(1) is 44.3KHz
  145. //_delay_us(8); //9 is 6.4KHz, 8 is roughly 7.2KHz
  146. //delay_us(10) is 5.8KHz
  147. //not much granularity there...)
  148. //so adding the following...
  149. //////////// if( x == 3){
  150. //////////// _delay_us(4);//these two are 5.98 or so KHz
  151. //////////// x = 0;
  152. //////////// goto jump;
  153. //////////// }
  154. ////////////
  155. //////////// //NOTE: the above ends up causing an ugly looking clock. may not matter
  156. //////////// //for fpga clocking off of rising edge, BUT, it is ugly, should remove,
  157. //////////// //and just do single delay. Will do in part 9.
  158. ////////////
  159. //////////// x++;
  160. //////////// jump:
  161. //////////// //in combination with the jump, gives closer to 6KHz
  162. //////////// _delay_us(8);
  163. ///below didn't work out, so using osccal
  164. //bit convuluted anyways
  165. /*if( x == 4){
  166. _delay_us(1);
  167. //x = 0;
  168. goto jump;
  169. }
  170. if( x == 8){
  171. _delay_us(2);
  172. x = 0;
  173. goto jump;
  174. }
  175. x++;
  176. jump:
  177. //in combination with the jump, gives closer to 6KHz
  178. _delay_us(8);*/
  179. //the above idles at 5.98 - 5.99KHz.
  180. //best so far.
  181. //Now, calibrate oscillator with OSCCAL
  182. //if more accuracy desired.
  183. //or adjust if clause above
  184. //tried to make my own delay in ASM, BUT
  185. //compiler keeps optimizing it out (even w/out optimization enabled)
  186. //... some other flag may be culprit. So instead will use delay_us. Two delay
  187. //functions are delay_ms and delay_us. see headers at top and delay.h)
  188. /* __asm__("mov r16, r17;"
  189. "mov r17, r16;"
  190. "inc r16;"
  191. "dec r16;"
  192. "mov r16, r17;"
  193. "mov r17, r16;"
  194. "inc r16;"
  195. "dec r16;"
  196. "clr r16;"
  197. "clr r17;"
  198. "clr r18;"
  199. "clr r19;"
  200. "clr r20;"
  201. "clr r21;"
  202. "clr r22;"
  203. "clr r23;"
  204. "mov r16, r17;"
  205. "mov r17, r16;"
  206. "inc r16;"
  207. "dec r16;"
  208. "mov r16, r17;"
  209. "mov r17, r16;"
  210. "clr r16;"
  211. "clr r17;"
  212. "clr r18;"
  213. "clr r19;"
  214. "clr r20;"
  215. "clr r21;"
  216. "clr r22;"
  217. "clr r23;"
  218. "mov r16, r17;"
  219. "mov r17, r16;"
  220. "inc r16;"
  221. "dec r16;"
  222. "mov r16, r17;"
  223. "mov r17, r16;"
  224. );*/
  225. //mplab delay is:
  226. /** \ingroup util_delay_basic
  227. Delay loop using an 8-bit counter \c __count, so up to 256
  228. iterations are possible. (The value 256 would have to be passed
  229. as 0.) The loop executes three CPU cycles per iteration, not
  230. including the overhead the compiler needs to setup the counter
  231. register.
  232. Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
  233. can be achieved.
  234. */
  235. //////////void
  236. //////////_delay_loop_1(uint8_t __count)
  237. //////////{
  238. ////////// __asm__ volatile (
  239. ////////// "1: dec %0" "\n\t"
  240. ////////// "brne 1b"
  241. ////////// : "=r" (__count)
  242. ////////// : "0" (__count)
  243. ////////// );
  244. //////////}
  245. //////////
  246. //////////
  247. //ignore below
  248. // asm("nop;""nop;""nop;""nop;""nop;"
  249. //"nop;""nop;"//"nop;""nop;"//"nop;""nop;"
  250. //);
  251. //}//just this asm block is about 121.22KHz
  252. }
  253. }