summaryrefslogtreecommitdiff
path: root/Year_3/LSM/ex_20200127.c
blob: e3c33486b050366e538cc5c56e6fbb0672f4a02f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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);
    }
}