prgmode.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. value = readbyte(1);
  19. if (value == 0xFF) {
  20. for (byte i = 0; i < sizeof(demoPrg); i++) {
  21. writebyte(i, demoPrg[i]);
  22. }
  23. }
  24. }
  25. }
  26. void programMode() {
  27. // checking if advance programmer board connected?
  28. #ifdef SPS_ENHANCEMENT
  29. if (digitalRead(SW_SEL) == 0) {
  30. advancePrg();
  31. }
  32. else {
  33. #endif
  34. dbgOutLn("PrgMode");
  35. // light up all LEDs
  36. doPort(0x08);
  37. while (digitalRead(SW_PRG) == 0) {
  38. // waiting for PRG to release
  39. }
  40. blinkAll();
  41. prgMode = ADDRESS;
  42. addr = 0;
  43. do {
  44. blinkD1();
  45. dbgOut("Adr:");
  46. dbgOutLn(addr);
  47. // LoNibble Adresse anzeigen
  48. doAddr(addr);
  49. //delay(SHOW_DELAY);
  50. blinkD2();
  51. // HiNibble Adresse anzeigen
  52. data = (addr & 0xf0) >> 4; //Adresse anzeigen
  53. doAddr(data);
  54. //delay(SHOW_DELAY);
  55. byte Eebyte = readbyte(addr);
  56. data = Eebyte & 15;
  57. cmd = Eebyte >> 4;
  58. blinkD3();
  59. prgMode = COMMAND;
  60. dbgOutLn("cmd");
  61. doPort(cmd); //show command
  62. do {
  63. if (digitalRead(SW_SEL) == 0) {
  64. delay(KEY_DELAY);
  65. cmd += 1;
  66. cmd = cmd & 0x0F;
  67. doPort(cmd);
  68. }
  69. }
  70. while (digitalRead(SW_PRG) == 1);
  71. delay(DEBOUNCE);
  72. blinkD4();
  73. prgMode = DATA;
  74. dbgOutLn("dat");
  75. doPort(data); //show data
  76. do {
  77. if (digitalRead(SW_SEL) == 0) {
  78. delay(KEY_DELAY);
  79. data += 1;
  80. data = data & 0x0F;
  81. doPort(data);
  82. }
  83. }
  84. while (digitalRead(SW_PRG) == 1); // S2 = 1
  85. delay(DEBOUNCE);
  86. byte newValue = (cmd << 4) + data;
  87. if (newValue != Eebyte) {
  88. writebyte(addr, newValue); // Writeeeprom Eebyte , Addr
  89. blinkAll();
  90. }
  91. addr += 1;
  92. }
  93. while (true);
  94. #ifdef SPS_ENHANCEMENT
  95. }
  96. #endif
  97. }
  98. void blinkAll() {
  99. blinkNull();
  100. doPort(0x0F);
  101. delay(BLINK_DELAY);
  102. }
  103. void blinkD1() {
  104. blinkNull();
  105. doPort(0x01);
  106. delay(BLINK_DELAY);
  107. blinkNull();
  108. }
  109. void blinkD2() {
  110. blinkNull();
  111. doPort(0x02);
  112. delay(BLINK_DELAY);
  113. blinkNull();
  114. }
  115. void blinkD3() {
  116. blinkNull();
  117. doPort(0x04);
  118. delay(BLINK_DELAY);
  119. blinkNull();
  120. }
  121. void blinkD4() {
  122. blinkNull();
  123. doPort(0x08);
  124. delay(BLINK_DELAY);
  125. blinkNull();
  126. }
  127. void blinkNull() {
  128. doPort(0x00);
  129. delay(BLINK_DELAY);
  130. }
  131. void doAddr(byte value) {
  132. for (byte i = ADDR_LOOP; i > 0; i--) {
  133. doPort(value);
  134. delay(19);
  135. doPort(0x0F);
  136. delay(1);
  137. }
  138. }