add example code to use PZEM library
This commit is contained in:
parent
599027ac39
commit
1923e2af4b
31
main.c
31
main.c
@ -39,6 +39,7 @@
|
||||
#include "global.h" // board definitions
|
||||
#include "usart.h" // USART utilities
|
||||
#include "usb_cdcacm.h" // USB CDC ACM utilities
|
||||
#include "sensor_pzem.h" // PZEM electricity meter utilities
|
||||
|
||||
/** @defgroup main_flags flag set in interrupts to be processed in main task
|
||||
* @{
|
||||
@ -164,6 +165,13 @@ void main(void)
|
||||
ticks_time = rtc_get_counter_val(); // get time/date from internal RTC
|
||||
printf("current time: %02lu:%02lu:%02lu\n", ticks_time/(60*60), (ticks_time%(60*60))/60, (ticks_time%60)); // display time
|
||||
|
||||
// setup PZEM electricity meter
|
||||
printf("setup PZEM-004 electricity meter: ");
|
||||
sensor_pzem_setup(); // setup PZEM electricity meter
|
||||
printf("OK\n");
|
||||
|
||||
sensor_pzem_measurement_request(0xc0a80101, VOLTAGE);
|
||||
|
||||
// main loop
|
||||
printf("command input: ready\n");
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
@ -200,6 +208,27 @@ void main(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
while (sensor_pzem_measurement_received) { // measurement from electricity meter received
|
||||
struct sensor_pzem_measurement_t measurement = sensor_pzem_measurement_decode(); // decode measurement
|
||||
if (measurement.valid) { // only show valid measurement
|
||||
switch (measurement.type) {
|
||||
case VOLTAGE:
|
||||
printf("voltage: %.01f V\n", measurement.value.voltage);
|
||||
break;
|
||||
case CURRENT:
|
||||
printf("current: %.02f A\n", measurement.value.current);
|
||||
break;
|
||||
case POWER:
|
||||
printf("power: %.00f W\n", measurement.value.power);
|
||||
break;
|
||||
case ENERGY:
|
||||
printf("energy: %lu Wh\n", measurement.value.energy);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (button_flag) { // user pressed button
|
||||
action = true; // action has been performed
|
||||
printf("button pressed\n");
|
||||
@ -234,5 +263,5 @@ void rtc_isr(void)
|
||||
{
|
||||
rtc_clear_flag(RTC_SEC); // clear flag
|
||||
rtc_internal_tick_flag = true; // notify to show new time
|
||||
//led_toggle(); // blink every second to show the firmware is running
|
||||
led_toggle(); // blink every second to show the firmware is running
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user