prgmode.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. entering the programming mode
  3. */
  4. #define BLINK_DELAY 500
  5. #define SHOW_DELAY 1000
  6. #define KEY_DELAY 250
  7. #define ADDR_LOOP 50
  8. const byte demoPrg[] = { 0x4F, 0x59, 0x1F, 0x29, 0x10, 0x29, 0x5A, 0x40,
  9. 0x59, 0x64, 0x54, 0x29, 0x4F, 0x59, 0x10, 0xCD,
  10. 0x11, 0x28, 0xCC, 0x18, 0x28, 0x4F, 0x59, 0x5A,
  11. 0x72, 0x26, 0xC0, 0x35, 0x80, 0x90, 0xFF
  12. };
  13. enum PROGRAMMING_MODE {ADDRESS, COMMAND, DATA};
  14. PROGRAMMING_MODE prgMode;
  15. void prgDemoPrg() {
  16. byte value = readbyte(0);
  17. if (value == 0xFF) {
  18. dbgOutLn("recreate demo program");
  19. value = readbyte(1);
  20. if (value == 0xFF) {
  21. for (byte i = 0; i < sizeof(demoPrg); i++) {
  22. writebyte(i, demoPrg[i]);
  23. }
  24. }
  25. }
  26. }
  27. void programMode() {
  28. // checking if advance programmer board connected?
  29. #ifdef SPS_ENHANCEMENT
  30. if (digitalRead(SW_SEL) == 0) {
  31. advancePrg();
  32. }
  33. else {
  34. #endif
  35. dbgOutLn("PrgMode");
  36. // light up all LEDs
  37. doPort(0x08);
  38. while (digitalRead(SW_PRG) == 0) {
  39. // waiting for PRG to release
  40. }
  41. blinkAll();
  42. prgMode = ADDRESS;
  43. addr = 0;
  44. do {
  45. blinkD1();
  46. dbgOut("Adr:");
  47. dbgOutLn(addr);
  48. // LoNibble Adresse anzeigen
  49. doAddr(addr);
  50. //delay(SHOW_DELAY);
  51. blinkD2();
  52. // HiNibble Adresse anzeigen
  53. data = (addr & 0xf0) >> 4; //Adresse anzeigen
  54. doAddr(data);
  55. //delay(SHOW_DELAY);
  56. byte Eebyte = readbyte(addr);
  57. data = Eebyte & 15;
  58. cmd = Eebyte >> 4;
  59. blinkD3();
  60. prgMode = COMMAND;
  61. dbgOutLn("cmd");
  62. doPort(cmd); //show command
  63. do {
  64. if (digitalRead(SW_SEL) == 0) {
  65. delay(KEY_DELAY);
  66. cmd += 1;
  67. cmd = cmd & 0x0F;
  68. doPort(cmd);
  69. }
  70. }
  71. while (digitalRead(SW_PRG) == 1);
  72. delay(DEBOUNCE);
  73. if (digitalRead(SW_SEL) == 0) {
  74. break;
  75. }
  76. blinkD4();
  77. prgMode = DATA;
  78. dbgOutLn("dat");
  79. doPort(data); //show data
  80. do {
  81. if (digitalRead(SW_SEL) == 0) {
  82. delay(KEY_DELAY);
  83. data += 1;
  84. data = data & 0x0F;
  85. doPort(data);
  86. }
  87. }
  88. while (digitalRead(SW_PRG) == 1); // S2 = 1
  89. delay(DEBOUNCE);
  90. if (digitalRead(SW_SEL) == 0) {
  91. break;
  92. }
  93. byte newValue = (cmd << 4) + data;
  94. if (newValue != Eebyte) {
  95. writebyte(addr, newValue); // Writeeeprom Eebyte , Addr
  96. blinkAll();
  97. }
  98. addr += 1;
  99. }
  100. while (true);
  101. #ifdef SPS_ENHANCEMENT
  102. }
  103. #endif
  104. dbgOutLn("save program data");
  105. store();
  106. }
  107. void blinkAll() {
  108. blinkNull();
  109. doPort(0x0F);
  110. delay(BLINK_DELAY);
  111. }
  112. void blinkD1() {
  113. blinkNull();
  114. doPort(0x01);
  115. delay(BLINK_DELAY);
  116. blinkNull();
  117. }
  118. void blinkD2() {
  119. blinkNull();
  120. doPort(0x02);
  121. delay(BLINK_DELAY);
  122. blinkNull();
  123. }
  124. void blinkD3() {
  125. blinkNull();
  126. doPort(0x04);
  127. delay(BLINK_DELAY);
  128. blinkNull();
  129. }
  130. void blinkD4() {
  131. blinkNull();
  132. doPort(0x08);
  133. delay(BLINK_DELAY);
  134. blinkNull();
  135. }
  136. void blinkNull() {
  137. doPort(0x00);
  138. delay(BLINK_DELAY);
  139. }
  140. void doAddr(byte value) {
  141. for (byte i = ADDR_LOOP; i > 0; i--) {
  142. doPort(value);
  143. delay(19);
  144. doPort(0x0F);
  145. delay(1);
  146. }
  147. }