Dangerous Prototypes

Dangerous Prototypes => Bus Pirate Development => Topic started by: jlvern on November 17, 2012, 06:49:18 pm

Title: Horrible "float"
Post by: jlvern on November 17, 2012, 06:49:18 pm
Hello,
I just discovered "Bus Pirate" and I think it's a good project.
But ... reading the code I discover horrible "float" in two functions: void bpPWM (void) void bpWvolts (unsigned int a).
The utisation to "float" is contrary to my religion (unless I have a gun to his head), so I can provide you with a few lines of code using integers to replace those horrible ineffective  "float"...

Best regards,
Jean-Louis Vern

1) In bpPWM
#define FCY   16000000UL
   unsigned int PWM_pd;
   unsigned long PWM_freq_Hz;
...
   // no prescaler
   T2CONbits.TCKPS0=0;
   T2CONbits.TCKPS1=0;
   // PWM freq in Hz
   PWM_freq_Hz  = __builtin_muluu( PWM_freq, 1000 );
   // PWM period in cycles (rounded)
   PWM_period    = (FCY + (PWM_freq_Hz/2)) / PWM_freq_Hz;
   // rounded duty cycle
   PWM_dutycycle = __builtin_divud( __builtin_muluu( PWM_period, PWM_pd)+50, 100);

   //assign pin with PPS
   AUXPIN_RPOUT = OC5_IO;

   OC5R = PWM_dutycycle;
   OC5RS = PWM_dutycycle;
   OC5CON = 0x6;         
   PR2   = PWM_period - 1;   
...

2) In bpWvolts()

#define   R_LOW      10UL
#define   R_HIGH      10UL
#define V_REF      3300UL
#define ADC_MAX      1024UL

#define R_DIV(x)   ((((x)*(R_LOW+R_HIGH))+(R_LOW/2))/R_LOW)
#define ADC_VOLTS(x)   (((unsigned long)(x)*V_REF)/ADC_MAX)

#include <stdlib.h> /* for div, div_t */
void bpWvolts(unsigned int a)
{
   unsigned int v;
   div_t div_res;
   // value with V_REF resolution (1mV)
   v = (unsigned int)ADC_VOLTS( R_DIV(a) );
   // rounding to 10mV
   v = (v+5)/10;
   // integer part (V) in div_res.quot, fract (1/100V) in div_res.rem
   div_res = div( v, 100 );
   bpWdec( div_res.quot );
   UART1TX('.');
   // remainder (1/100V, 00..99)
   div_res = div( div_res.rem, 10 );
   UART1TX( div_res.quot + '0' );
   UART1TX( div_res.rem  + '0' );
}
Title: Re: Horrible "float"
Post by: jlvern on November 18, 2012, 01:27:58 pm
Oups ! Mistake !
#define ADC_MAX 1023UL
// for correct rounded mV value :
#define ADC_VOLTS(x)   ((((unsigned long)(x)*V_REF)+ADC_MAX/2)/ADC_MAX)
Title: Re: Horrible "float"
Post by: ian on December 05, 2012, 08:31:03 am
Thanks so much, I added it to the tracker and will test for the next release.

( ! ) Fatal error: Uncaught exception 'Elk_Exception' with message 'Please try again. If you come back to this error screen, report the error to an administrator.' in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
( ! ) Elk_Exception: Please try again. If you come back to this error screen, report the error to an administrator. in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
Call Stack
#TimeMemoryFunctionLocation
10.00962044472session_write_close ( )...(null):0
20.00992176048ElkArte\sources\subs\SessionHandler\DatabaseHandler->write( )...(null):0
30.00992176824Database_MySQL->query( ).../DatabaseHandler.php:119
40.05232315544Database_MySQL->error( ).../Db-mysql.class.php:273