编辑代码

#include <stdio.h>

#define V_REF               2.43

#define R_CURRENT           0.002

#define R_VOLTAGE           1881 

#define F_OSC               3579000


double hlw8012_current_multiplier;
double hlw8012_voltage_multiplier;
double hlw8012_power_multiplier;

volatile unsigned long hlw8012_voltage_pulse_width;
volatile unsigned long hlw8012_current_pulse_width; 
volatile unsigned long hlw8012_power_pulse_width;  
volatile unsigned long hlw8012_pulse_count;

double hlw8012_current;
unsigned int hlw8012_voltage;
unsigned int hlw8012_power;

int main () {    

    hlw8012_current_multiplier = ( (double)1000000 * 512 * V_REF / R_CURRENT / 24.0 / F_OSC );
    hlw8012_voltage_multiplier = ( (double)1000000 * 512 * V_REF * R_VOLTAGE / 2.0 / F_OSC );
    hlw8012_power_multiplier = ( (double)1000000 * 128 * V_REF * V_REF * R_VOLTAGE / R_CURRENT / 48.0 / F_OSC );   

    printf("%.4f\n", hlw8012_current_multiplier);
    printf("%.4f\n", hlw8012_voltage_multiplier);
    printf("%.4f\n", hlw8012_power_multiplier);


    hlw8012_current_pulse_width = 0x59DA;
    hlw8012_current = (hlw8012_current_pulse_width > 0) ? hlw8012_current_multiplier / hlw8012_current_pulse_width / 2 : 0;

    printf("%.4f\n", hlw8012_current);
}