-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTempUmid.h
93 lines (81 loc) · 2.56 KB
/
TempUmid.h
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
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
BaseSequentialStream *chpTU = NULL;
#define ICUUsed PAL_LINE(GPIOA, 0) //A0
#define LINE_VENTOLA PAL_LINE(GPIOA, 6) //D12
/* CONFIGURAZIONE ICU */
#define MCU_REQUEST_WIDTH 18000
#define DHT_ERROR_WIDTH 200
#define DHT_START_BIT_WIDTH 80
#define DHT_LOW_BIT_WIDTH 28
#define DHT_HIGH_BIT_WIDTH 70
#define ICU_FREQUENCY 1000000
static uint8_t TEMP, HR, tmp = 0, bit_counter = 0;
static icucnt_t widths[40];
//Callback
static void icuwidthcb(ICUDriver *icup) {
icucnt_t width = icuGetWidthX(icup);
if (width >= DHT_START_BIT_WIDTH) {
/* starting bit resetting the bit counter */
bit_counter = 0;
}
else {
widths[bit_counter] = width;
if (width > DHT_LOW_BIT_WIDTH) {
tmp |= (1 << (7 - (bit_counter % 8)));
}
else {
tmp &= ~(1 << (7 - (bit_counter % 8)));
}
if (bit_counter == 7)
HR = tmp;
if (bit_counter == 23)
TEMP = tmp;
bit_counter++;
}
//chSysUnlock();
}
static ICUConfig icucfg = {ICU_INPUT_ACTIVE_HIGH,
ICU_FREQUENCY,
NULL, icuwidthcb, NULL, ICU_CHANNEL_1, 0U,
0xFFFFFFFFU};
/* CONFIGURAZIONE componente 2 */
/* THD main */
//seleziona waADC_Light, poi tasto sx>refactor>Rename ; stessa cosa con ADC_Light per cambiare il nome
static THD_WORKING_AREA(waICU_TempUmid,512);
static THD_FUNCTION(ICU_TempUmid, arg) {
(void)arg;
while (1) {
/*
* Making a request
*/
palSetPadMode(GPIOA, GPIOC_PIN0, PAL_MODE_OUTPUT_PUSHPULL);
palWritePad(GPIOA, GPIOC_PIN0, PAL_LOW);
chThdSleepMicroseconds(MCU_REQUEST_WIDTH);
palWritePad(GPIOA, GPIOC_PIN0, PAL_HIGH);
/*
* Initializes the ICU driver 1.
* GPIOA8 is the ICU input.
*/
palSetPadMode(GPIOA, 0, PAL_MODE_ALTERNATE(2));
icuStart(&ICUD5, &icucfg);
icuStartCapture(&ICUD5);
icuEnableNotifications(&ICUD5);
chThdSleepMilliseconds(700);
icuStopCapture(&ICUD5);
icuStop(&ICUD5);
//chprintf(chpTU, "Temperature: %d C, Humidity Rate: %d %% \n\r", TEMP, HR);
if (TEMP >= 29)
palSetLine(LINE_VENTOLA);
else
palClearLine(LINE_VENTOLA);
chThdSleepMilliseconds(1000);
}
}
void StartTempUmid(BaseSequentialStream *Tempchp) {
chpTU = Tempchp;
palSetLineMode(LINE_VENTOLA, PAL_MODE_OUTPUT_PUSHPULL);
chThdCreateStatic(waICU_TempUmid, sizeof(waICU_TempUmid), NORMALPRIO + 1,
ICU_TempUmid, NULL);
}