SPS.ino 21 KB

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