WCS1800电流传感器用stm32F1控制 有没有大佬写过的stm32F1控制使用程序呀?说明书带Ardudio程序,不是很能看懂,求stm库函数代码

2019-07-21 05:20发布

附WCS1800电流传感器说明书
Arduino程序:
#define ELECTRICITY_SENSOR A0 //
float amplitude_current; //
float effective_value; //
void setup()
{
Serial.begin(9600);
pins_init();
}
void loop()
{
int sensor_max;
sensor_max = getMaxValue();
Serial.print("sensor_max = ");
Serial.println(sensor_max);
//the VCC on the Grove interface of the sensor is 5v
amplitude_current=(float)sensor_max/1024*5/200*1000000;
effective_value=amplitude_current/1.414;
//Minimum current value can be detected=1/1024*5/200*1000000/1.414=24.4(mA)
//Only for sinusoidal alternating current
Serial.println("The amplitude of the current is(in mA)");
Serial.println(amplitude_current,1);//Only one number after the decimal point
Serial.println("The effective value of the current is(in mA)");
Serial.println(effective_value,1);
}
void pins_init()
{
pinMode(ELECTRICITY_SENSOR, INPUT);
}
/*Function: Sample for 1000ms and get the maximum value from the SIG pin*/
int getMaxValue()
{
int sensorValue; //value read from the sensor
int sensorMax = 0;
uint32_t start_time = millis();
while((millis()-start_time) < 1000)//sample for 1000ms
{
sensorValue = analogRead(ELECTRICITY_SENSOR);
if (sensorValue > sensorMax)
{
/*record the maximum sensor value*/
sensorMax = sensorValue;
}
}
return sensorMax;

}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。