SPS.ino 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. #endif
  67. #include <debug.h>
  68. #include <makros.h>
  69. #include <EEPROM.h>
  70. #include <avr/eeprom.h>
  71. #ifdef SPS_SERVO
  72. #include <Servo.h>
  73. #endif
  74. #ifdef SPS_ENHANCEMENT
  75. #include <avdweb_Switch.h>
  76. #endif
  77. #ifdef SPS_TONE
  78. #include "notes.h"
  79. #endif
  80. // Hardwareanbindung
  81. #ifdef __AVR_ATmega328P__
  82. // Arduino Hardware
  83. const byte Din_0 = 0;
  84. const byte Din_1 = 1;
  85. const byte Din_2 = 2;
  86. const byte Din_3 = 3;
  87. const byte Dout_0 = 4;
  88. const byte Dout_1 = 5;
  89. const byte Dout_2 = 6;
  90. const byte Dout_3 = 7;
  91. const byte ADC_0 = 0; //(15)
  92. const byte ADC_1 = 1; //(16)
  93. const byte PWM_1 = 9;
  94. const byte PWM_2 = 10;
  95. #ifdef SPS_RCRECEIVER
  96. const byte RC_0 = 18;
  97. const byte RC_1 = 19;
  98. #endif
  99. #ifdef SPS_SERVO
  100. const byte SERVO_1 = 9;
  101. const byte SERVO_2 = 10;
  102. #endif
  103. const byte SW_PRG = 8;
  104. const byte SW_SEL = 11;
  105. #ifdef SPS_USE_DISPLAY
  106. const byte DIGIT_DATA_IO = 12;
  107. const byte DIGIT_CLOCK = 13;
  108. #endif
  109. #endif
  110. #ifdef __AVR_ATtiny84__
  111. // ATTiny84 Hardware
  112. const byte Dout_0 = 6;
  113. const byte Dout_1 = 5;
  114. const byte Dout_2 = 4;
  115. const byte Dout_3 = 1;
  116. const byte Din_0 = 10;
  117. const byte Din_1 = 9;
  118. const byte Din_2 = 8;
  119. const byte Din_3 = 7;
  120. const byte ADC_0 = 0;
  121. const byte ADC_1 = 1;
  122. const byte PWM_1 = 2;
  123. const byte PWM_2 = 3;
  124. #ifdef SPS_RCRECEIVER
  125. const byte RC_0 = 10;
  126. const byte RC_1 = 9;
  127. #endif
  128. #ifdef SPS_SERVO
  129. const byte SERVO_1 = 2;
  130. const byte SERVO_2 = 3;
  131. #endif
  132. const byte SW_PRG = 0;
  133. const byte SW_SEL = 8;
  134. #ifdef SPS_USE_DISPLAY
  135. const byte DIGIT_DATA_IO = 4;
  136. const byte DIGIT_CLOCK = 5;
  137. #endif
  138. #endif
  139. #ifdef __AVR_ATtiny4313__
  140. // ATTiny4313 Hardware
  141. const byte Dout_0 = 0;
  142. const byte Dout_1 = 1;
  143. const byte Dout_2 = 2;
  144. const byte Dout_3 = 3;
  145. const byte Din_0 = 4;
  146. const byte Din_1 = 5;
  147. const byte Din_2 = 6;
  148. const byte Din_3 = 7;
  149. const byte ADC_0 = 13;
  150. const byte ADC_1 = 14;
  151. const byte PWM_1 = 11;
  152. const byte PWM_2 = 12;
  153. #ifdef SPS_RCRECEIVER
  154. const byte RC_0 = 15;
  155. const byte RC_1 = 16;
  156. #endif
  157. #ifdef SPS_SERVO
  158. const byte SERVO_1 = 11;
  159. const byte SERVO_2 = 12;
  160. #endif
  161. const byte SW_PRG = 9;
  162. const byte SW_SEL = 8;
  163. #endif
  164. // Befehle
  165. const byte PORT = 0x10;
  166. const byte DELAY = 0x20;
  167. const byte JUMP_BACK = 0x30;
  168. const byte SET_A = 0x40;
  169. const byte IS_A = 0x50;
  170. const byte A_IS = 0x60;
  171. const byte CALC = 0x70;
  172. const byte PAGE = 0x80;
  173. const byte JUMP = 0x90;
  174. const byte C_COUNT = 0xA0;
  175. const byte D_COUNT = 0xB0;
  176. const byte SKIP_IF = 0xC0;
  177. const byte CALL = 0xD0;
  178. const byte CALL_SUB = 0xE0;
  179. const byte CMD_BYTE = 0xF0;
  180. // debouncing with 100ms
  181. const byte DEBOUNCE = 100;
  182. // sub routines
  183. const byte subCnt = 7;
  184. word subs[subCnt];
  185. word addr;
  186. word page;
  187. #ifdef SPS_ENHANCEMENT
  188. const byte SAVE_CNT = 16;
  189. #else
  190. const byte SAVE_CNT = 1;
  191. #endif
  192. word saveaddr[SAVE_CNT];
  193. byte saveCnt;
  194. #ifdef SPS_ENHANCEMENT
  195. byte stack[SAVE_CNT];
  196. byte stackCnt;
  197. #endif
  198. unsigned long tmpValue;
  199. byte a, b, c, d;
  200. #ifdef SPS_ENHANCEMENT
  201. byte e, f;
  202. #endif
  203. #ifdef SPS_SERVO
  204. Servo servo1;
  205. Servo servo2;
  206. #endif
  207. byte prog = 0;
  208. byte data = 0;
  209. byte com = 0;
  210. void setup() {
  211. pinMode(Dout_0, OUTPUT);
  212. pinMode(Dout_1, OUTPUT);
  213. pinMode(Dout_2, OUTPUT);
  214. pinMode(Dout_3, OUTPUT);
  215. pinMode(PWM_1, OUTPUT);
  216. pinMode(PWM_2, OUTPUT);
  217. pinMode(Din_0, INPUT_PULLUP);
  218. pinMode(Din_1, INPUT_PULLUP);
  219. pinMode(Din_2, INPUT_PULLUP);
  220. pinMode(Din_3, INPUT_PULLUP);
  221. pinMode(SW_PRG, INPUT_PULLUP);
  222. pinMode(SW_SEL, INPUT_PULLUP);
  223. #ifdef SPS_USE_DISPLAY
  224. initDisplay();
  225. #endif
  226. // Serielle Schnittstelle einstellen
  227. #ifndef __AVR_ATtiny84__
  228. initDebug();
  229. #endif
  230. // writeProgram();
  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. }
  951. #endif
  952. }