Added processing of the temperature sensor

This commit is contained in:
Stanislav N Mikhailov
2025-01-12 21:11:47 +03:00
parent 1726b92ae4
commit 14a1a0fcf5
6 changed files with 101 additions and 6 deletions
+2
View File
@@ -2,6 +2,8 @@
#include <math.h>
uint16_t frame_buffer[WIDTH * HEIGHT]; // Буфер для экрана
float humidity, temperature;
// Установка пикселя в буфере (с проверкой границ)
unsigned short reverse(unsigned short x)
+1
View File
@@ -10,6 +10,7 @@
#define COLOR_WHITE 0xFFFF
extern uint16_t frame_buffer[WIDTH * HEIGHT];
extern float humidity, temperature;
float bernstein(int i, int n, float t);
void draw_bezier(const int *points_x, const int *points_y, size_t num_points, uint16_t color);
+1
View File
@@ -13,6 +13,7 @@ target_link_libraries(${PROJECT_NAME}
pico_stdlib
hardware_spi
hardware_dma
hardware_timer
pico_multicore
)
+81 -1
View File
@@ -4,12 +4,20 @@
#include "pico/multicore.h"
#include "BackBuffer.h"
struct repeating_timer timer;
//uint16_t frame_buffer1[WIDTH * HEIGHT]; // Буфер для экрана1
void coreEntry(){
st7789_init(); // Initialize SPI and GPIO
gpio_put(PIN_BL, 1); // Подсветка
gpio_pull_up(DHT_PIN);
if (!add_repeating_timer_ms(2000, repeating_timer_callback, NULL, &timer)) {
printf("Failed to add timer\n");
while (true); // Остановите выполнение, если ошибка
}
multicore_fifo_push_blocking(0); //Экран 0 свободен
//multicore_fifo_push_blocking(1); //Экран 1 свободен
@@ -150,4 +158,76 @@ void st7789_send_framebuffer(uint16_t *buffer) {
spi_write_blocking(SPI_PORT, (uint8_t *)buffer, WIDTH * HEIGHT * 2);
gpio_put(PIN_CS, 1); // Завершить передачу
}
void send_start_signal() {
gpio_init(DHT_PIN);
gpio_set_dir(DHT_PIN, GPIO_OUT);
// Низкий уровень на 1 мс
gpio_put(DHT_PIN, 0);
sleep_ms(1);
// Высокий уровень на 20-40 мкс
gpio_put(DHT_PIN, 1);
sleep_us(40);
}
bool wait_for_signal(uint32_t timeout_us, bool level) {
uint32_t start_time = time_us_32();
while (gpio_get(DHT_PIN) != level) {
if (time_us_32() - start_time > timeout_us) {
return false; // Таймаут
}
}
return true;
}
void read_dht_data(uint8_t *data) {
for (int i = 0; i < 40; i++) {
// Ожидаем начала высокого уровня
if (!wait_for_signal(80, 1)) return;
// Замер длительности высокого уровня
uint32_t start_time = time_us_32();
if (!wait_for_signal(100, 0)) return;
uint32_t pulse_length = time_us_32() - start_time;
// Сохраняем бит (1, если > 50 мкс)
data[i / 8] <<= 1;
if (pulse_length > 50) {
data[i / 8] |= 1;
}
}
}
bool dht_read(float *humidity, float *temperature) {
uint8_t data[5] = {0};
send_start_signal();
gpio_set_dir(DHT_PIN, GPIO_IN);
// Ожидаем ответа от датчика
if (!wait_for_signal(100, 0)) return false; // Ждём низкого уровня
if (!wait_for_signal(100, 1)) return false; // Ждём высокого уровня
// Читаем данные
read_dht_data(data);
// Проверяем контрольную сумму
if (data[4] != (data[0] + data[1] + data[2] + data[3])) return false;
// Расчёт значений
*humidity = ((data[0] << 8) | data[1]) / 10.0;
*temperature = ((data[2] << 8) | data[3]) / 10.0;
return true;
}
bool repeating_timer_callback(struct repeating_timer *t) {
if (dht_read(&humidity, &temperature)) {
} else {
humidity=-0.0;
temperature=-0.0;
}
}
+13 -2
View File
@@ -1,7 +1,11 @@
#ifndef THREADS_H
#define THREADS_H
#include <stdbool.h>
#include <stdio.h>
#include "hardware/timer.h"
#define SPI_PORT spi0
#define PIN_MISO -1
#define PIN_MOSI 19
@@ -10,14 +14,21 @@
#define PIN_DC 22
#define PIN_RST 13
#define PIN_BL 12
#define DHT_PIN 2 // GPIO для подключения DHT22
extern struct repeating_timer timer;
bool repeating_timer_callback(struct repeating_timer *t);
void st7789_send_command(uint8_t cmd);
void st7789_send_data(uint8_t data);
void st7789_send_framebuffer(uint16_t *buffer);
void coreEntry();
void st7789_init();
void send_start_signal();
bool wait_for_signal(uint32_t timeout_us, bool level);
void read_dht_data(uint8_t *data);
bool dht_read(float *humidity, float *temperature);
#endif
+3 -3
View File
@@ -21,6 +21,7 @@ int main() {
float x=0.0;
float freq=1.0;
bool minmax=false;
wchar_t buffer[100];
while (x<500)
{
data = multicore_fifo_pop_blocking();
@@ -28,9 +29,8 @@ int main() {
fillBufer(frame_buffer,reverse(0x4A69));//
grid(20,20,40,0x634d);
generate_sine_wave_points(num_points, 50, freq, 0, HEIGHT / 2,x);
draw_string(20,110,L"Кириллица работает!!!",0b1111111111111111); //
draw_string(50,130,L"Does Latin work?",0b0000000000011111); //Does Latin work?
draw_string(20,150,L"Проверка 0123456789!",0b1111100000000000); //Does Latin work?
swprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), L"Value of pi: %.2f", temperature);
draw_string(20,110,buffer,0b1111111111111111); //
multicore_fifo_push_blocking(0); //Экран 0 нарисован
x+=0.03;
if (minmax)