Guest User

Untitled

a guest
May 24th, 2020
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //calibration
  2. #define SENDDELAY 198000 //delay between datasets. capture must be 164ms  https://i.imgur.com/LXj3fqW.png
  3. #define BITTIME 490 // bit duration. adjust for start bit in 0xFF for 417µs length https://i.imgur.com/IWQ1Sz1.png
  4. #define PARITYDELAY 0.97 //parity bit length modifier. capture must be 417µs https://i.imgur.com/W9EwHV5.png
  5. #define BITDELAY 1.038 // bit length modifier. capture must be (0xFF, first 2 bits for reference) 3337µs https://i.imgur.com/xPBU9pW.png
  6.  
  7. //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.
  8.  
  9.  
  10.  
  11. //default data. 100% 19.15°C 38V 400Wh 1024mA
  12. byte data[] = {0xFF, 0xFF, 0x0E, 0x06,   0x64,   0x19,   0x01, 0x7F,   0x02, 0xF8,   0x2A,   0xF8, 0x01,   0x18, 0x11, 0xF0,   0x04, 0x00,   0x00};
  13.  
  14. void setup() {
  15.   pinMode(0,OUTPUT); //TX
  16.   analogReference(INTERNAL);
  17. }
  18.  
  19. byte chksum() {
  20.   int _t = data[0];
  21.   for (char i = 17; i > 0; i--) {
  22.     _t = _t - data[i];
  23.   } return ((_t % 256) + 256);
  24. }
  25.  
  26. //PORTB &= B11111110; // set pin 0 high
  27. //PORTB |= B00000001; //set pin 0 low
  28.  
  29. const byte averages = 40;
  30.  
  31. void sendChar(char c){
  32.   noInterrupts();
  33.   delayMicroseconds(BITTIME);          
  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. }
  57.  
  58. long avg = 0;
  59. int sample = 0;
  60. byte ctr = 0;
  61. char batt_percent = 100;
  62. void loop() {
  63.   delayMicroseconds(SENDDELAY);
  64.   sample = analogRead(A1);
  65.   if(sample>=900){sample=900;}
  66.   if(sample<=720){sample=720;}
  67.   //avg = (((avg<<2) - avg + sample) + 2) >> 2;
  68.   avg = avg + sample;
  69.   ctr++;
  70.   if(ctr == averages){
  71.       batt_percent = (char)map((avg/averages),720,900,0,100);
  72.       ctr = 0;
  73.       avg = 0;
  74.   }
  75.  
  76.   if(batt_percent<=5){batt_percent=5;}
  77.   if(batt_percent>=100){batt_percent=100;}
  78.  
  79.   data[4] = batt_percent;
  80.   data[18] = chksum();
  81.  
  82.   for (char i = 0; i <= 18; i++) {
  83.     sendChar(data[i]);
  84.   }
  85.  
  86. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×