diff options
Diffstat (limited to 'radiator-plc/stm32/app/control.c')
-rw-r--r-- | radiator-plc/stm32/app/control.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/radiator-plc/stm32/app/control.c b/radiator-plc/stm32/app/control.c new file mode 100644 index 0000000..f3a35c9 --- /dev/null +++ b/radiator-plc/stm32/app/control.c @@ -0,0 +1,114 @@ +#include "project.h" + + +static uint32_t target = 25000; +static uint32_t value = ~0U; + +#define SHORT 200 +#define LONG 1200 +#define REPEAT 400 + +#define HYST 75UL + + +static void +show_temp (char t, uint32_t v, int y) +{ + char buf[16]; + uint32_t i, f; + + i = v / 1000; + f = v / 100 - (i * 10); + + if (v == ~0U) + { + sprintf (buf, "%c:error ", t); + } + else + { + sprintf (buf, "%c:%2d.%1d%cC", t, (int) i, (int) f, 0xdf); + } + + lcd_write (buf, 0, y); +} + +void +control_update (void) +{ + + show_temp ('P', value, 0); + show_temp ('T', target, 1); + + if (value == ~0U) + relay_off (); + else if (value < (target - HYST)) + relay_on (); + else if (value > (target + HYST)) + relay_off (); + + +} + +void +control_tick (void) +{ + static int down, up, update; + + if (!gpio_get (GPIOB, GPIO4)) + down++; + else + down = 0; + + if (!gpio_get (GPIOB, GPIO6)) + up++; + else + up = 0; + + if ((down == SHORT) || (down == LONG)) + { + if (target > 100) + target -= 100; + if (down == LONG) + down = LONG - REPEAT; + control_update (); + } + + if ((up == SHORT) || (up == LONG)) + { + if (target < 99900) + target += 100; + if (up == LONG) + up = LONG - REPEAT; + control_update (); + } + + update++; + + if (update == 1000) + { + control_update (); + update = 0; + } +} + +void +control_report (uint32_t v) +{ + + value = v; + control_update (); +} + +void +control_init (void) +{ + + gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, + GPIO4 | GPIO6); + gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, + GPIO5); + gpio_set (GPIOB, GPIO4 | GPIO6); + gpio_clear (GPIOB, GPIO5); + + +} |