summaryrefslogtreecommitdiff
path: root/Year_3
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-02-01 16:17:17 +0100
committerSanto Cariotti <santo@dcariotti.me>2022-02-01 16:17:17 +0100
commit656ff2de2737d005c1de0b80cd293b6672bce8d8 (patch)
tree5b89d57b102d1b41931c10d3222dc888c14920ad /Year_3
parentd31b26a705d0e4b6a75c174968fccca3590c2cc7 (diff)
add exam
Diffstat (limited to 'Year_3')
-rw-r--r--Year_3/LSM/ex_20200127.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/Year_3/LSM/ex_20200127.c b/Year_3/LSM/ex_20200127.c
new file mode 100644
index 0000000..e3c3348
--- /dev/null
+++ b/Year_3/LSM/ex_20200127.c
@@ -0,0 +1,98 @@
+#include "stm32_unict_lib.h"
+#include <string.h>
+
+typedef enum {
+ IDLE,
+ RAIN
+} status_t;
+
+status_t status = IDLE;
+int freq = 100;
+float dim;
+float tank = 0;
+
+void
+init()
+{
+ DISPLAY_init();
+ GPIO_init(GPIOB);
+ GPIO_config_input(GPIOB, 10);
+ GPIO_config_EXTI(GPIOB, EXTI10);
+ EXTI_enable(EXTI10, FALLING_EDGE);
+
+ GPIO_config_output(GPIOB, 0);
+
+ ADC_init(ADC1, ADC_RES_8, ADC_ALIGN_RIGHT);
+ ADC_channel_config(ADC1, GPIOC, 1, 11);
+ ADC_channel_config(ADC1, GPIOC, 0, 10);
+ ADC_on(ADC1);
+
+ TIM_init(TIM2);
+ TIM_set(TIM2, 0);
+ TIM_on(TIM2);
+ TIM_config_timebase(TIM2, 8400, 1000);
+ TIM_enable_irq(TIM2, IRQ_UPDATE);
+
+ CONSOLE_init();
+}
+
+int
+main()
+{
+ init();
+ char s[4];
+ int value;
+
+ for (;;) {
+ if (status == RAIN) {
+ if (tank > 30)
+ tank = 0;
+ ADC_sample_channel(ADC1, 11);
+ ADC_start(ADC1);
+ while (!ADC_completed(ADC1)) { }
+ value = ADC_read(ADC1);
+ freq = (value * 70 / 255 + 10) * 100;
+
+ ADC_sample_channel(ADC1, 10);
+ ADC_start(ADC1);
+ while (!ADC_completed(ADC1)) { }
+ value = ADC_read(ADC1);
+ dim = (value * 48 / 255.0 + 2) / 10;
+
+ sprintf(s, "%f", tank);
+ } else {
+ sprintf(s, "SUN ");
+ }
+ DISPLAY_puts(0, s);
+ }
+ return 0;
+}
+
+void
+EXTI15_10_IRQHandler(void)
+{
+ if (EXTI_isset(EXTI10)) {
+ if (status == IDLE) {
+ GPIO_write(GPIOB, 0, 1);
+ status = RAIN;
+ } else {
+ GPIO_write(GPIOB, 0, 0);
+ status = IDLE;
+ }
+
+ EXTI_clear(EXTI10);
+ }
+}
+
+void
+TIM2_IRQHandler(void)
+{
+ if (TIM_update_check(TIM2)) {
+ TIM2->ARR = freq;
+
+ if (status == RAIN) {
+ tank += dim;
+ }
+ TIM_update_clear(TIM2);
+ }
+}