prgmode.ino 2.8 KB

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