serialprg.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifdef __AVR_ATtiny84__
  2. #define BAUDRATE 9600
  3. #endif
  4. #ifdef __AVR_ATmega328P__
  5. #define BAUDRATE 57600
  6. #endif
  7. void serialPrg() {
  8. int value1, value2, value3, value4;
  9. byte value;
  10. bool endOfPrg = false;
  11. addr = 0;
  12. Serial.end();
  13. Serial.begin(BAUDRATE);
  14. Serial.println("serPrgStart");
  15. while (!endOfPrg) {
  16. while (Serial.available() > 0) {
  17. // look for the next valid integer in the incoming serial stream:
  18. char myChar = Serial.read();
  19. if (myChar == 'd') {
  20. for (byte i = 0; i < 8; i++) {
  21. char c;
  22. while (!Serial.available()) {
  23. }
  24. do {
  25. c = Serial.read();
  26. } while (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'))));
  27. value = hexToByte(c);
  28. while (!Serial.available()) {
  29. }
  30. do {
  31. c = Serial.read();
  32. } while (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'))));
  33. value = value * 16 + hexToByte(c);
  34. EEPROM.write(addr + i, value);
  35. }
  36. Serial.print("w:");
  37. for (byte i = 0; i < 8; i++) {
  38. value = EEPROM.read(addr + i);;
  39. Serial.print(value, HEX);
  40. if (i < 7) {
  41. Serial.print(",");
  42. }
  43. }
  44. Serial.println();
  45. addr = addr + 8;
  46. }
  47. if (myChar == '*') {
  48. endOfPrg = true;
  49. }
  50. }
  51. }
  52. Serial.println("end");
  53. Serial.end();
  54. doReset();
  55. }
  56. byte hexToByte (char c) {
  57. if ( (c >= '0') && (c <= '9') ) {
  58. return c - '0';
  59. }
  60. if ( (c >= 'A') && (c <= 'F') ) {
  61. return (c - 'A') + 10;
  62. }
  63. }