Advertisement
Guest User

Untitled

a guest
Jun 7th, 2020
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. //calibration
  2. #define SENDDELAY 23500 //delay between datasets. capture must be 164ms https://i.imgur.com/LXj3fqW.png
  3. #define BITTIME 58.5 // bit duration. adjust for start bit in 0xFF for 417µs length https://i.imgur.com/IWQ1Sz1.png
  4. #define BITDELAY 1.04 // bit length modifier. capture must be (0xFF, first 2 bits for reference) 3337µs https://i.imgur.com/xPBU9pW.png
  5. #define PARITYDELAY 0.968 //parity bit length modifier. capture must be 417µs https://i.imgur.com/W9EwHV5.png
  6. #define ENDBITDELAY 0.9843 // end bit length must be 0.4165ms aswell https://i.imgur.com/it98XEF.png
  7.  
  8. //to check data, capture on pin PB0 and decode in saleae logic for Serial, 2400 BAUD, 8 bits, 1 stop bit, even parity. LSB, Non inverted, no special mode.
  9.  
  10.  
  11.  
  12. //default data. 100% 19.15°C 38V 400Wh 1024mA
  13. byte data[] = {0xFF, 0xFF, 0x0E, 0x06, 0x64, 0x19, 0x01, 0x7F, 0x02, 0xF8, 0x2A, 0xF8, 0x01, 0x18, 0x11, 0xF0, 0x04, 0x00, 0x00};
  14.  
  15. void setup() {
  16. pinMode(0,OUTPUT); //TX
  17. analogReference(INTERNAL);
  18. }
  19.  
  20. byte chksum() {
  21. int _t = data[0];
  22. for (char i = 17; i > 0; i--) {
  23. _t = _t - data[i];
  24. } return ((_t % 256) + 256);
  25. }
  26.  
  27. //PORTB &= B11111110; // set pin 0 high
  28. //PORTB |= B00000001; //set pin 0 low
  29.  
  30. const byte averages = 40;
  31.  
  32. void sendChar(char c){
  33. noInterrupts();
  34. byte bits = 0;
  35. PORTB &= B11111110;
  36. delayMicroseconds(BITTIME);
  37. for (char i=0; i<8;i++){
  38. byte bitr = bitRead(c,i);
  39. if(bitr == 1){bits++;}
  40. if(bitr > 0){
  41. PORTB |= B00000001;
  42. }else{
  43. PORTB &= B11111110;
  44. }
  45. delayMicroseconds(BITTIME/BITDELAY);
  46. }
  47. if((bits % 2) == 0 && bits != 1){
  48. PORTB &= B11111110;
  49. }else{
  50. PORTB |= B00000001;
  51. }
  52. bits = 0;
  53. delayMicroseconds(BITTIME/PARITYDELAY);
  54. PORTB |= B00000001;
  55. interrupts();
  56. delayMicroseconds(BITTIME/ENDBITDELAY);
  57. }
  58.  
  59. long avg = 0;
  60. int sample = 0;
  61. byte ctr = 0;
  62. char batt_percent = 100;
  63. void loop() {
  64. delayMicroseconds(SENDDELAY);
  65. sample = analogRead(A1);
  66. if(sample>=900){sample=900;}
  67. if(sample<=720){sample=720;}
  68. //avg = (((avg<<2) - avg + sample) + 2) >> 2;
  69. avg = avg + sample;
  70. ctr++;
  71. if(ctr == averages){
  72. batt_percent = (char)map((avg/averages),720,900,0,100);
  73. ctr = 0;
  74. avg = 0;
  75. }
  76.  
  77. if(batt_percent<=5){batt_percent=5;}
  78. if(batt_percent>=100){batt_percent=100;}
  79.  
  80. data[4] = batt_percent;
  81. data[18] = chksum();
  82.  
  83. for (char i = 0; i <= 18; i++) {
  84. sendChar(data[i]);
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement