1
Bus Pirate Development / Horrible "float"
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' );
}