<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div>Nikolay,<div class=""><br class=""></div><div class="">Please take care with RAM usage. It is very very tight on these little microcontrollers.</div><div class=""><br class=""></div><div class="">I think the simplest way for yours is something like this:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">(((unsigned long)(can_databuffer[4)<<16) +</div><div class="">((unsigned long)(can_databuffer[5)<<8) +</div><div class="">((unsigned long)(can_databuffer[6)) +</div><div class="">5000UL) / 10000UL</div></blockquote><div class=""><br class=""></div><div class="">Not sure why you are getting a rounding error. Are you sure that the SOC hasn’t dropped between logging the data and plugging in OVMS?</div><div class=""><br class=""></div><div class="">Here is what I get:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">$ echo 'ibase=16; scale=2; 07DCC0/02710' | bc</div></div><div class=""><div class="">51.52</div></div></blockquote><div class=""><br class=""></div><div class="">Regards, Mark.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 25 Jun, 2015, at 7:35 am, Nikolay Shishkov <<a href="mailto:nshishkov@yahoo.com" class="">nshishkov@yahoo.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div style="background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 16px;" class=""><div id="yui_3_16_0_1_1435153954547_32533" class=""><span class="">Thanks again...</span></div><div id="yui_3_16_0_1_1435153954547_32535" class=""><span class="">I seem to not been able to grasp the idea and would appreciate a code example.</span></div><div id="yui_3_16_0_1_1435153954547_32087" class="">For example byte1=0x07, byte2=0xDC, byte3=0xC0</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="">If I put these in excel and calculate 7*256*256 + 220*256 + 192 I get 520264.</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="">Then when I divide this by 10000, I get 52.0264. And this matches the 52% shown by the car.</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class=""><br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="">Currently my code for this conversion is:</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv1 = (unsigned long)can_databuffer[4];</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv1 = sv1*65536UL;<br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv2 = (unsigned long)can_databuffer[5];</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv2 = sv2*256UL;<br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv3 = (unsigned long)can_databuffer[6];</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv3 = sv3;<br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> </div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> car_SOC = (char)((unsigned long)(sv1 + sv2 + sv3 + 5000UL)/10000UL);<br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""><br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">And the result for car_SOC is 49. </div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">How is this possible?</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">And what would be the correct way to put these 3 bytes together and divide them by 10000 to get to the final value in char?</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""><br class=""></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">Thanks in advance,</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">Nikolay</div><div dir="ltr" class="" style="" id="yui_3_16_0_1_1435153954547_33497"><br class="" style=""></div> <div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""><br class="" style=""></div><br class=""><div class="qtdSeparateBR"><br class=""><br class=""></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" class=""> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" class=""> <div dir="ltr" class=""> <font size="2" face="Arial" class=""> On Monday, June 22, 2015 3:58 PM, Mark Webb-Johnson <<a href="mailto:mark@webb-johnson.net" class="">mark@webb-johnson.net</a>> wrote:<br class=""> </font> </div> <br class=""><br class=""> <div class="y_msg_container"><div id="yiv7764699889" class=""><div class=""><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div>Division is kind of nasty in 8bit. Actually very nasty. Unless you are dividing by 2, 4, 8, 16, etc. Divide by 2^N is the same as >>N, and that is the shortcut you want to do if at all possible.<div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">That said, your solution seems ok, except the value[2] should be <<8, and you probably want to add 5000 just before divide by 10000, to round correctly.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">If you don’t need too much precision, you might find an inaccurate kludge may be good enough.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">For example:</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><blockquote class="yiv7764699889" style="margin:0 0 0 40px;border:none;padding:0px;"><div class="yiv7764699889">205/2048 = 0.1000976562</div></blockquote><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">and that is pretty close to 0.1. So multiply by 205, then shirt right 11 spaces is pretty close to divide by 10. Multiplication is much less expensive than division.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">If you look at MiFromKm and KmFromMi in utils.c, you can see similar sorts of approximations to avoid big divisions.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">Regards, Mark.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"><div class=""><blockquote class="yiv7764699889" type="cite"><div class="yiv7764699889yqt8228485529" id="yiv7764699889yqt72105"><div class="yiv7764699889">On 22 Jun, 2015, at 4:30 pm, Nikolay Shishkov <<a rel="nofollow" shape="rect" class="yiv7764699889" ymailto="mailto:nshishkov@yahoo.com" target="_blank" href="mailto:nshishkov@yahoo.com">nshishkov@yahoo.com</a>> wrote:</div><br clear="none" class="yiv7764699889Apple-interchange-newline"><div class="yiv7764699889"><div class="yiv7764699889"><div class="yiv7764699889" style="background-color:rgb(255, 255, 255);font-family:HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;font-size:16px;"><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5352">I have a situation where I need to convert a value that is stored as 3 bytes to a char.</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">The can be for example 7ED95, which is reported in 3 consequtive bytes 07, ED, 95.</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">This value in decimal is 519573, which corresponds to a 51.9% SOC. </div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">Is the proper way to do this conversion (from the 3 bytes to the value of 51) like this:</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">char soc = (char)(((unsigned long)value[1]<<16 + (unsigned long)value[2]<<16 + (unsigned long)value[3])/10000);</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">I have been doing stuff like that before with unions, but can't remember how either.</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">Thanks,</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">Nikolay</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353" style=""> </div></div></div>_______________________________________________<br clear="none" class="yiv7764699889">OvmsDev mailing list<br clear="none" class="yiv7764699889"><a rel="nofollow" shape="rect" class="yiv7764699889" ymailto="mailto:OvmsDev@lists.teslaclub.hk" target="_blank" href="mailto:OvmsDev@lists.teslaclub.hk">OvmsDev@lists.teslaclub.hk</a><br clear="none" class="yiv7764699889"><a href="http://lists.teslaclub.hk/mailman/listinfo/ovmsdev" class="">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev</a><br clear="none" class="yiv7764699889"></div></div></blockquote></div><br clear="none" class="yiv7764699889"></div></div></div><br class=""><div class="yqt8228485529" id="yqt18226">_______________________________________________<br clear="none" class="">OvmsDev mailing list<br clear="none" class=""><a shape="rect" ymailto="mailto:OvmsDev@lists.teslaclub.hk" href="mailto:OvmsDev@lists.teslaclub.hk" class="">OvmsDev@lists.teslaclub.hk</a><br clear="none" class=""><a shape="rect" href="http://lists.teslaclub.hk/mailman/listinfo/ovmsdev" target="_blank" class="">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev</a><br clear="none" class=""></div><br class=""><br class=""></div> </div> </div> </div></div></div>_______________________________________________<br class="">OvmsDev mailing list<br class=""><a href="mailto:OvmsDev@lists.teslaclub.hk" class="">OvmsDev@lists.teslaclub.hk</a><br class="">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev<br class=""></div></blockquote></div><br class=""></div></body></html>