SPS.ino 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. SPS System mit dem Arduino.
  3. Version 0.10
  4. 18.11.2018 WKLA
  5. - new standard programming mode
  6. i added a new programming mode for the default programming, because i thing the old one was a little bit clumsy.
  7. the new one has a nicer interface, as you now always know where you are.
  8. Starting with PRG pushed after Reset.
  9. as a result, all LEDs will shortly blink
  10. now you are in programming mode.
  11. * the D1 LED will blink
  12. * the higher nibble of the address will be shown
  13. * the D2 LED will blink
  14. * the lower nibble of the address will be shown
  15. * the D3 LED will blink
  16. * the command part (high nibble) will be shown
  17. * with SEL you can step thru all commands
  18. * PRG will save the command
  19. * the D4 LED will blink
  20. * the data part (low nibble) will be shown
  21. * with SEL you can step thru all datas
  22. * PRG will save the data
  23. * if the new value has been changed, all LEDs will flash as the byte will be written to the EEPROM
  24. * address will be increased and now it will start with blinking of the D1 LED
  25. *
  26. * To leave the programming simply push reset.
  27. Version 0.9
  28. 18.11.2018 WKLA
  29. - BUGs entfernt. Release.
  30. 10.11.2018 WKLA
  31. - Implementierung Tone Befehl
  32. Version 0.8
  33. 06.11.2018 WKLA
  34. - Umstellung auf dbgOut
  35. - Display TM1637 Anbindung
  36. Version 0.7
  37. 24.09.2012 WKLA
  38. - neue Berechnung A = B - A und Swap A,B...
  39. - Stack auf 16 Bytes berschränkt, wird zu oft gepusht, werden die alten Werte rausgeschoben.
  40. Basierd auf dem TPS System vom elektronik-labor.
  41. Erweiterungen:
  42. - es können bis zu 6 Unterroutinen definiert werden und diese direkt angesprungen werden.
  43. - neben return gibt's auch einen restart
  44. - 2 Servoausgänge für übliche RC Servos. (10° Auflösung in Nibble Modus, <1° Auflösung im Bytemodus)
  45. ACHTUNG: Servo und PWM Ausgänge sind nicht mischbar und können auch nicht gleichzeitig benutzt werden.
  46. - 2 RC Eingänge (16 Schritte auflösung im nibble Modus, Mitte 8, 255 Schritte im Byte Modus)
  47. - fkt. auch mit einem ATTiny84 (44 ist leider auf GRund der Programmgröße nicht mehr für den erweiterten Befehlssatz möglich)
  48. - call stack von bis zu 16 Unterfunktionen
  49. - neue Register e,f
  50. */
  51. // Program im Debugmodus kompilieren, dann werden zus. Ausgaben auf die serielle Schnittstelle geschrieben.
  52. #ifdef __AVR_ATtiny4313__
  53. #define SPS_RCRECEIVER
  54. #endif
  55. #ifdef __AVR_ATmega328P__
  56. #define debug
  57. //#define SPS_USE_DISPLAY
  58. #define SPS_RECEIVER
  59. #define SPS_ENHANCEMENT
  60. #define SPS_SERVO
  61. #define SPS_TONE
  62. #endif
  63. #ifdef __AVR_ATtiny84__
  64. #define SPS_ENHANCEMENT
  65. #define SPS_SERVO
  66. //#define SPS_TONE
  67. #endif
  68. #include <debug.h>
  69. #include <makros.h>
  70. #include <EEPROM.h>
  71. #include <avr/eeprom.h>
  72. #ifdef SPS_SERVO
  73. #include <Servo.h>
  74. #endif
  75. #ifdef SPS_ENHANCEMENT
  76. #include <avdweb_Switch.h>
  77. #endif
  78. #ifdef SPS_TONE
  79. #include "notes.h"
  80. #endif
  81. // Hardwareanbindung
  82. #ifdef __AVR_ATmega328P__
  83. // Arduino Hardware
  84. const byte Din_0 = 0;
  85. const byte Din_1 = 1;
  86. const byte Din_2 = 2;
  87. const byte Din_3 = 3;
  88. const byte Dout_0 = 4;
  89. const byte Dout_1 = 5;
  90. const byte Dout_2 = 6;
  91. const byte Dout_3 = 7;
  92. const byte ADC_0 = 0; //(15)
  93. const byte ADC_1 = 1; //(16)
  94. const byte PWM_1 = 9;
  95. const byte PWM_2 = 10;
  96. #ifdef SPS_RCRECEIVER
  97. const byte RC_0 = 18;
  98. const byte RC_1 = 19;
  99. #endif
  100. #ifdef SPS_SERVO
  101. const byte SERVO_1 = 9;
  102. const byte SERVO_2 = 10;
  103. #endif
  104. const byte SW_PRG = 8;
  105. const byte SW_SEL = 11;
  106. #ifdef SPS_USE_DISPLAY
  107. const byte DIGIT_DATA_IO = 12;
  108. const byte DIGIT_CLOCK = 13;
  109. #endif
  110. #endif
  111. #ifdef __AVR_ATtiny84__
  112. // ATTiny84 Hardware
  113. const byte Dout_0 = 6;
  114. const byte Dout_1 = 5;
  115. const byte Dout_2 = 4;
  116. const byte Dout_3 = 1;
  117. const byte Din_0 = 10;
  118. const byte Din_1 = 9;
  119. const byte Din_2 = 8;
  120. const byte Din_3 = 7;
  121. const byte ADC_0 = 0;
  122. const byte ADC_1 = 1;
  123. const byte PWM_1 = 2;
  124. const byte PWM_2 = 3;
  125. #ifdef SPS_RCRECEIVER
  126. const byte RC_0 = 10;
  127. const byte RC_1 = 9;
  128. #endif
  129. #ifdef SPS_SERVO
  130. const byte SERVO_1 = 2;
  131. const byte SERVO_2 = 3;
  132. #endif
  133. const byte SW_PRG = 0;
  134. const byte SW_SEL = 8;
  135. #ifdef SPS_USE_DISPLAY
  136. const byte DIGIT_DATA_IO = 4;
  137. const byte DIGIT_CLOCK = 5;
  138. #endif
  139. #endif
  140. #ifdef __AVR_ATtiny4313__
  141. // ATTiny4313 Hardware
  142. const byte Dout_0 = 0;
  143. const byte Dout_1 = 1;
  144. const byte Dout_2 = 2;
  145. const byte Dout_3 = 3;
  146. const byte Din_0 = 4;
  147. const byte Din_1 = 5;
  148. const byte Din_2 = 6;
  149. const byte Din_3 = 7;
  150. const byte ADC_0 = 13;
  151. const byte ADC_1 = 14;
  152. const byte PWM_1 = 11;
  153. const byte PWM_2 = 12;
  154. #ifdef SPS_RCRECEIVER
  155. const byte RC_0 = 15;
  156. const byte RC_1 = 16;
  157. #endif
  158. #ifdef SPS_SERVO
  159. const byte SERVO_1 = 11;
  160. const byte SERVO_2 = 12;
  161. #endif
  162. const byte SW_PRG = 9;
  163. const byte SW_SEL = 8;
  164. #endif
  165. // Befehle
  166. const byte PORT = 0x10;
  167. const byte DELAY = 0x20;
  168. const byte JUMP_BACK = 0x30;
  169. const byte SET_A = 0x40;
  170. const byte IS_A = 0x50;
  171. const byte A_IS = 0x60;
  172. const byte CALC = 0x70;
  173. const byte PAGE = 0x80;
  174. const byte JUMP = 0x90;
  175. const byte C_COUNT = 0xA0;
  176. const byte D_COUNT = 0xB0;
  177. const byte SKIP_IF = 0xC0;
  178. const byte CALL = 0xD0;
  179. const byte CALL_SUB = 0xE0;
  180. const byte CMD_BYTE = 0xF0;
  181. // debouncing with 100ms
  182. const byte DEBOUNCE = 100;
  183. // sub routines
  184. const byte subCnt = 7;
  185. word subs[subCnt];
  186. word addr;
  187. word page;
  188. #ifdef SPS_ENHANCEMENT
  189. const byte SAVE_CNT = 16;
  190. #else
  191. const byte SAVE_CNT = 1;
  192. #endif
  193. word saveaddr[SAVE_CNT];
  194. byte saveCnt;
  195. #ifdef SPS_ENHANCEMENT
  196. byte stack[SAVE_CNT];
  197. byte stackCnt;
  198. #endif
  199. unsigned long tmpValue;
  200. byte a, b, c, d;
  201. #ifdef SPS_ENHANCEMENT
  202. byte e, f;
  203. #endif
  204. #ifdef SPS_SERVO
  205. Servo servo1;
  206. Servo servo2;
  207. #endif
  208. byte prog = 0;
  209. byte data = 0;
  210. byte com = 0;
  211. void setup() {
  212. pinMode(Dout_0, OUTPUT);
  213. pinMode(Dout_1, OUTPUT);
  214. pinMode(Dout_2, OUTPUT);
  215. pinMode(Dout_3, OUTPUT);
  216. pinMode(PWM_1, OUTPUT);
  217. pinMode(PWM_2, OUTPUT);
  218. pinMode(Din_0, INPUT_PULLUP);
  219. pinMode(Din_1, INPUT_PULLUP);
  220. pinMode(Din_2, INPUT_PULLUP);
  221. pinMode(Din_3, INPUT_PULLUP);
  222. pinMode(SW_PRG, INPUT_PULLUP);
  223. pinMode(SW_SEL, INPUT_PULLUP);
  224. #ifdef SPS_USE_DISPLAY
  225. initDisplay();
  226. #endif
  227. // Serielle Schnittstelle einstellen
  228. #ifndef __AVR_ATtiny84__
  229. initDebug();
  230. #endif
  231. doReset();
  232. if (digitalRead(SW_PRG) == 0) {
  233. programMode();
  234. }
  235. #ifdef SPS_ENHANCEMENT
  236. if (digitalRead(SW_SEL) == 0) {
  237. serialPrg();
  238. }
  239. #endif
  240. }
  241. void doReset() {
  242. dbgOutLn("Reset");
  243. #ifdef SPS_SERVO
  244. servo1.detach();
  245. servo2.detach();
  246. #endif
  247. for (int i = 0; i < subCnt; i++) {
  248. subs[i] = 0;
  249. }
  250. readProgram();
  251. addr = 0;
  252. page = 0;
  253. saveCnt = 0;
  254. a = 0;
  255. b = 0;
  256. c = 0;
  257. d = 0;
  258. #ifdef SPS_ENHANCEMENT
  259. e = 0;
  260. f = 0;
  261. #endif
  262. }
  263. /*
  264. getting all addresses of sub programms
  265. */
  266. void readProgram() {
  267. dbgOutLn("Read program");
  268. word addr = 0;
  269. for ( addr = 0; addr <= E2END; addr++) {
  270. byte value = EEPROM.read(addr);
  271. #ifdef debug
  272. dbgOut2(value, HEX);
  273. if (((addr + 1) % 16) == 0) {
  274. dbgOutLn();
  275. }
  276. else {
  277. dbgOut(",");
  278. }
  279. #endif
  280. if (value == 0xFF) {
  281. // ende des Programms
  282. break;
  283. }
  284. byte cmd = (value & 0xF0);
  285. byte data = (value & 0x0F);
  286. dbgOut("(");
  287. dbgOut2(cmd, HEX);
  288. dbgOut2(data, HEX);
  289. dbgOut(")");
  290. if (cmd == CALL_SUB) {
  291. if (data >= 8) {
  292. data = data - 8;
  293. subs[data] = addr + 1;
  294. }
  295. }
  296. #ifdef SPS_SERVO
  297. if ((cmd == IS_A) && (data == 0x0B)) {
  298. if (!servo1.attached()) {
  299. dbgOutLn("attach Srv1");
  300. servo1.attach(SERVO_1);
  301. }
  302. } else if ((cmd == CMD_BYTE) && (data == 0x06)) {
  303. if (!servo1.attached()) {
  304. dbgOutLn("attach Srv1");
  305. servo1.attach(SERVO_1);
  306. }
  307. } else if ((cmd == IS_A) && (data == 0x0C)) {
  308. if (!servo2.attached()) {
  309. dbgOutLn("attach Srv2");
  310. servo2.attach(SERVO_2);
  311. }
  312. } else if ((cmd == CMD_BYTE) && (data == 0x07)) {
  313. if (!servo2.attached()) {
  314. dbgOutLn("attach Srv2");
  315. servo2.attach(SERVO_2);
  316. }
  317. }
  318. #endif
  319. }
  320. dbgOutLn();
  321. }
  322. /*
  323. main loop
  324. */
  325. void loop() {
  326. byte value = EEPROM.read(addr);
  327. byte cmd = (value & 0xF0);
  328. byte data = (value & 0x0F);
  329. dbgOut2(addr, HEX);
  330. dbgOut(":");
  331. dbgOut2(value, HEX);
  332. dbgOut(",");
  333. dbgOut2(cmd, HEX);
  334. dbgOut(",");
  335. dbgOut2(data, HEX);
  336. dbgOut(",a:");
  337. dbgOut2(a, HEX);
  338. dbgOut(",");
  339. dbgOut2(b, HEX);
  340. dbgOut(",");
  341. dbgOut2(c, HEX);
  342. dbgOut(",");
  343. dbgOut2(d, HEX);
  344. dbgOut(",");
  345. dbgOut2(e, HEX);
  346. dbgOut(",");
  347. dbgOut2(f, HEX);
  348. dbgOutLn();
  349. addr = addr + 1;
  350. switch (cmd) {
  351. case PORT:
  352. doPort(data);
  353. break;
  354. case DELAY:
  355. doDelay(data);
  356. break;
  357. case JUMP_BACK:
  358. doJumpBack(data);
  359. break;
  360. case SET_A:
  361. doSetA(data);
  362. break;
  363. case A_IS:
  364. doAIs(data);
  365. break;
  366. case IS_A:
  367. doIsA(data);
  368. break;
  369. case CALC:
  370. doCalc(data);
  371. break;
  372. case PAGE:
  373. doPage(data);
  374. break;
  375. case JUMP:
  376. doJump(data);
  377. break;
  378. case C_COUNT:
  379. doCCount(data);
  380. break;
  381. case D_COUNT:
  382. doDCount(data);
  383. break;
  384. case SKIP_IF:
  385. doSkipIf(data);
  386. break;
  387. case CALL:
  388. doCall(data);
  389. break;
  390. case CALL_SUB:
  391. doCallSub(data);
  392. break;
  393. case CMD_BYTE:
  394. doByte(data);
  395. break;
  396. default:
  397. ;
  398. }
  399. if (addr > E2END) {
  400. doReset();
  401. }
  402. }
  403. /*
  404. output to port
  405. */
  406. void doPort(byte data) {
  407. digitalWrite(Dout_0, (data & 0x01) > 0);
  408. digitalWrite(Dout_1, (data & 0x02) > 0);
  409. digitalWrite(Dout_2, (data & 0x04) > 0);
  410. digitalWrite(Dout_3, (data & 0x08) > 0);
  411. }
  412. /*
  413. delay in ms
  414. */
  415. void doDelay(byte data) {
  416. dbgOut("dly: ");
  417. dbgOutLn2(data, HEX);
  418. switch (data) {
  419. case 0:
  420. delay(1);
  421. break;
  422. case 1:
  423. delay(2);
  424. break;
  425. case 2:
  426. delay(5);
  427. break;
  428. case 3:
  429. delay(10);
  430. break;
  431. case 4:
  432. delay(20);
  433. break;
  434. case 5:
  435. delay(50);
  436. break;
  437. case 6:
  438. delay(100);
  439. break;
  440. case 7:
  441. delay(200);
  442. break;
  443. case 8:
  444. delay(500);
  445. break;
  446. case 9:
  447. delay(1000);
  448. break;
  449. case 10:
  450. delay(2000);
  451. break;
  452. case 11:
  453. delay(5000);
  454. break;
  455. case 12:
  456. delay(10000);
  457. break;
  458. case 13:
  459. delay(20000);
  460. break;
  461. case 14:
  462. delay(30000);
  463. break;
  464. case 15:
  465. delay(60000);
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. /*
  472. jump relative back
  473. */
  474. void doJumpBack(byte data) {
  475. addr = addr - data - 1;
  476. }
  477. /*
  478. a = data
  479. */
  480. void doSetA(byte data) {
  481. a = data;
  482. }
  483. /*
  484. a = somthing;
  485. */
  486. void doAIs(byte data) {
  487. switch (data) {
  488. case 1:
  489. a = b;
  490. break;
  491. case 2:
  492. a = c;
  493. break;
  494. case 3:
  495. a = d;
  496. break;
  497. case 4:
  498. a = digitalRead(Din_0) + (digitalRead(Din_1) << 1) + (digitalRead(Din_2) << 2) + (digitalRead(Din_3) << 3);
  499. break;
  500. case 5:
  501. a = digitalRead(Din_0);
  502. break;
  503. case 6:
  504. a = digitalRead(Din_1);
  505. break;
  506. case 7:
  507. a = digitalRead(Din_2);
  508. break;
  509. case 8:
  510. a = digitalRead(Din_3);
  511. break;
  512. #ifndef __AVR_ATtiny4313__
  513. case 9:
  514. tmpValue = analogRead(ADC_0);
  515. a = tmpValue / 64; //(Umrechnen auf 4 bit)
  516. break;
  517. case 10:
  518. tmpValue = analogRead(ADC_1);
  519. a = tmpValue / 64; //(Umrechnen auf 4 bit)
  520. break;
  521. #else
  522. case 9:
  523. a = digitalRead(ADC_0);
  524. break;
  525. case 10:
  526. a = digitalRead(ADC_1);
  527. break;
  528. #endif
  529. #ifdef SPS_RCRECEIVER
  530. case 11:
  531. tmpValue = pulseIn(RC_0, HIGH, 100000);
  532. if (tmpValue < 1000) {
  533. tmpValue = 1000;
  534. }
  535. if (tmpValue > 2000) {
  536. tmpValue = 2000;
  537. }
  538. a = (tmpValue - 1000) / 64; //(Umrechnen auf 4 bit)
  539. dbgOut("RC1:");
  540. dbgOut(tmpValue);
  541. dbgOut("=");
  542. dbgOutLn(a);
  543. break;
  544. case 12:
  545. tmpValue = pulseIn(RC_1, HIGH, 100000);
  546. if (tmpValue < 1000) {
  547. tmpValue = 1000;
  548. }
  549. if (tmpValue > 2000) {
  550. tmpValue = 2000;
  551. }
  552. a = (tmpValue - 1000) / 64; //(Umrechnen auf 4 bit)
  553. dbgOut("RC2:");
  554. dbgOut(tmpValue);
  555. dbgOut("=");
  556. dbgOutLn(a);
  557. break;
  558. #endif
  559. #ifdef SPS_ENHANCMENT
  560. case 13:
  561. a = e;
  562. break;
  563. case 14:
  564. a = f;
  565. break;
  566. case 15:
  567. if (stackCnt > 0) {
  568. stackCnt -= 1;
  569. a = stack[stackCnt];
  570. } else {
  571. a = 0;
  572. }
  573. break;
  574. #endif
  575. default:
  576. break;
  577. }
  578. }
  579. /*
  580. somthing = a;
  581. */
  582. void doIsA(byte data) {
  583. switch (data) {
  584. #ifdef SPS_ENHANCEMENT
  585. case 0:
  586. swap(a, b, byte);
  587. break;
  588. #endif
  589. case 1:
  590. b = a;
  591. break;
  592. case 2:
  593. c = a;
  594. break;
  595. case 3:
  596. d = a;
  597. break;
  598. case 4:
  599. doPort(a);
  600. break;
  601. case 5:
  602. digitalWrite(Dout_0, (a & 0x01) > 0);
  603. break;
  604. case 6:
  605. digitalWrite(Dout_1, (a & 0x01) > 0);
  606. break;
  607. case 7:
  608. digitalWrite(Dout_2, (a & 0x01) > 0);
  609. break;
  610. case 8:
  611. digitalWrite(Dout_3, (a & 0x01) > 0);
  612. break;
  613. case 9:
  614. tmpValue = a * 16;
  615. dbgOut("PWM1:");
  616. dbgOutLn(tmpValue);
  617. analogWrite(PWM_1, tmpValue);
  618. break;
  619. case 10:
  620. tmpValue = a * 16;
  621. dbgOut("PWM2:");
  622. dbgOutLn(tmpValue);
  623. analogWrite(PWM_2, tmpValue);
  624. break;
  625. #ifdef SPS_SERVO
  626. case 11:
  627. if (servo1.attached()) {
  628. tmpValue = (a * 10) + 10;
  629. dbgOut("Srv1:");
  630. dbgOutLn(tmpValue);
  631. servo1.write(tmpValue);
  632. }
  633. break;
  634. case 12:
  635. if (servo2.attached()) {
  636. tmpValue = (a * 10) + 10;
  637. dbgOut("Srv2:");
  638. dbgOutLn(tmpValue);
  639. servo2.write(tmpValue);
  640. }
  641. break;
  642. #endif
  643. #ifdef SPS_ENHANCEMENT
  644. case 13:
  645. e = a;
  646. break;
  647. case 14:
  648. f = a;
  649. break;
  650. case 15:
  651. if (stackCnt < SAVE_CNT) {
  652. stack[stackCnt] = a;
  653. stackCnt += 1;
  654. }
  655. else {
  656. for (int i = 1; i <= SAVE_CNT; i++) {
  657. stack[i - 1] = stack[i];
  658. }
  659. stack[stackCnt] = a;
  660. }
  661. break;
  662. #endif
  663. default:
  664. break;
  665. }
  666. }
  667. /*
  668. calculations
  669. */
  670. void doCalc(byte data) {
  671. switch (data) {
  672. case 1:
  673. a = a + 1;
  674. break;
  675. case 2:
  676. a = a - 1;
  677. break;
  678. case 3:
  679. a = a + b;
  680. break;
  681. case 4:
  682. a = a - b;
  683. break;
  684. case 5:
  685. a = a * b;
  686. break;
  687. case 6:
  688. a = a / b;
  689. break;
  690. case 7:
  691. a = a & b;
  692. break;
  693. case 8:
  694. a = a | b;
  695. break;
  696. case 9:
  697. a = a ^ b;
  698. break;
  699. case 10:
  700. a = !a;
  701. break;
  702. #ifdef SPS_ENHANCEMENT
  703. case 11:
  704. a = a % b;
  705. break;
  706. case 12:
  707. a = a + 16 * b;
  708. break;
  709. case 13:
  710. a = b - a;
  711. break;
  712. #endif
  713. default:
  714. break;
  715. }
  716. #ifndef SPS_ENHANCEMENT
  717. a = a & 15;
  718. #endif
  719. }
  720. /*
  721. setting page
  722. */
  723. void doPage(byte data) {
  724. page = data * 16;
  725. }
  726. /*
  727. jump absolute
  728. */
  729. void doJump(byte data) {
  730. #ifdef debug
  731. dbgOut("J");
  732. dbgOut2(page, HEX);
  733. dbgOutLn2(data, HEX);
  734. #endif
  735. addr = page + data;
  736. }
  737. /*
  738. counting with c register
  739. */
  740. void doCCount(byte data) {
  741. if (c > 0) {
  742. c -= 1;
  743. c = c & 0x0F;
  744. doJump(data);
  745. }
  746. }
  747. /*
  748. counting with d register
  749. */
  750. void doDCount(byte data) {
  751. if (d > 0) {
  752. d -= 1;
  753. d = d & 0x0F;
  754. doJump(data);
  755. }
  756. }
  757. /*
  758. simple comdition = true skip next command
  759. */
  760. void doSkipIf(byte data) {
  761. bool skip = false;
  762. switch (data) {
  763. #ifdef SPS_ENHANCEMENT
  764. case 0:
  765. skip = (a == 0);
  766. break;
  767. #endif
  768. case 1:
  769. skip = (a > b);
  770. break;
  771. case 2:
  772. skip = (a < b);
  773. break;
  774. case 3:
  775. skip = (a == b);
  776. break;
  777. case 4:
  778. skip = digitalRead(Din_0);
  779. break;
  780. case 5:
  781. skip = digitalRead(Din_1);
  782. break;
  783. case 6:
  784. skip = digitalRead(Din_2);
  785. break;
  786. case 7:
  787. skip = digitalRead(Din_3);
  788. break;
  789. case 8:
  790. skip = !digitalRead(Din_0);
  791. break;
  792. case 9:
  793. skip = !digitalRead(Din_1);
  794. break;
  795. case 10:
  796. skip = !digitalRead(Din_2);
  797. break;
  798. case 11:
  799. skip = !digitalRead(Din_3);
  800. break;
  801. case 12:
  802. skip = !digitalRead(SW_PRG);
  803. break;
  804. case 13:
  805. skip = !digitalRead(SW_SEL);
  806. break;
  807. case 14:
  808. skip = digitalRead(SW_PRG);
  809. break;
  810. case 15:
  811. skip = digitalRead(SW_SEL);
  812. break;
  813. default:
  814. break;
  815. }
  816. if (skip) {
  817. addr += 1;
  818. }
  819. }
  820. /*
  821. calling a subroutine
  822. */
  823. void doCall(byte data) {
  824. saveaddr[saveCnt] = addr;
  825. saveCnt++;
  826. addr = page + data;
  827. }
  828. /*
  829. calling a subroutine, calling return and restart
  830. */
  831. void doCallSub(byte data) {
  832. if (data == 0) {
  833. if (saveCnt < 0) {
  834. doReset();
  835. return;
  836. }
  837. saveCnt -= 1;
  838. addr = saveaddr[saveCnt];
  839. dbgOut("r:");
  840. dbgOutLn(addr);
  841. return;
  842. }
  843. #ifdef SPS_ENHANCEMENT
  844. if (data <= 7) {
  845. // call subroutine number
  846. doCall(addr);
  847. addr = subs[data - 1];
  848. dbgOut("c:");
  849. dbgOutLn(addr);
  850. return;
  851. }
  852. if (data == 0x0f) {
  853. doReset();
  854. }
  855. #endif
  856. }
  857. /*
  858. calling a byte methods
  859. */
  860. void doByte(byte data) {
  861. #ifdef SPS_ENHANCEMENT
  862. dbgOut("B ");
  863. switch (data) {
  864. case 0:
  865. tmpValue = analogRead(ADC_0);
  866. a = tmpValue >> 2; //(Umrechnen auf 8 bit)
  867. break;
  868. case 1:
  869. tmpValue = analogRead(ADC_1);
  870. a = tmpValue >> 2; //(Umrechnen auf 8 bit)
  871. break;
  872. #ifdef SPS_RCRECEIVER
  873. case 2:
  874. tmpValue = pulseIn(RC_0, HIGH, 100000);
  875. if (tmpValue < 1000) {
  876. tmpValue = 1000;
  877. }
  878. if (tmpValue > 2000) {
  879. tmpValue = 2000;
  880. }
  881. a = (tmpValue - 1000) / 4; //(Umrechnen auf 4 bit)
  882. dbgOut("RC1:");
  883. dbgOut(tmpValue);
  884. dbgOut("=");
  885. dbgOutLn(a);
  886. break;
  887. case 3:
  888. tmpValue = pulseIn(RC_1, HIGH, 100000);
  889. if (tmpValue < 1000) {
  890. tmpValue = 1000;
  891. }
  892. if (tmpValue > 2000) {
  893. tmpValue = 2000;
  894. }
  895. a = (tmpValue - 1000) / 4; //(Umrechnen auf 4 bit)
  896. dbgOut("RC2:");
  897. dbgOut(tmpValue);
  898. dbgOut("=");
  899. dbgOutLn(a);
  900. break;
  901. #endif
  902. case 4:
  903. tmpValue = a;
  904. dbgOut("PWM1:");
  905. dbgOutLn(a);
  906. analogWrite(PWM_1, a);
  907. break;
  908. case 5:
  909. tmpValue = a;
  910. dbgOut("PWM2:");
  911. dbgOutLn(a);
  912. analogWrite(PWM_2, a);
  913. break;
  914. #ifdef SPS_SERVO
  915. case 6:
  916. if (servo1.attached()) {
  917. dbgOut("Srv1:");
  918. tmpValue = map(a,0, 255,0,180);
  919. dbgOutLn(tmpValue);
  920. servo1.write(tmpValue);
  921. }
  922. break;
  923. case 7:
  924. if (servo2.attached()) {
  925. dbgOut("Srv2:");
  926. tmpValue = map(a,0, 255,0,180);
  927. dbgOutLn(tmpValue);
  928. servo2.write(tmpValue);
  929. }
  930. break;
  931. #endif
  932. #ifdef SPS_TONE
  933. case 8:
  934. if (a == 0) {
  935. dbgOutLn("Tone off");
  936. noTone(PWM_2);
  937. } else {
  938. if (between(a, MIDI_START, MIDI_START + MIDI_NOTES)) {
  939. word frequenz = pgm_read_word(a - MIDI_START + midiNoteToFreq);
  940. dbgOut("Tone on: midi ");
  941. dbgOut2(a, DEC);
  942. dbgOut(", ");
  943. dbgOut2(frequenz, DEC);
  944. dbgOutLn("Hz");
  945. tone(PWM_2, frequenz);
  946. }
  947. }
  948. break;
  949. #endif
  950. #ifdef __AVR_ATmega328P__
  951. case 13:
  952. digitalWrite(LED_BUILTIN, 0);
  953. break;
  954. case 14:
  955. digitalWrite(LED_BUILTIN, 1);
  956. break;
  957. #endif
  958. }
  959. #endif
  960. }