Загрузил(а) файлы в ''
This commit is contained in:
parent
bd4858ccb7
commit
a7b8634193
86
Ledv.ino
Normal file
86
Ledv.ino
Normal file
@ -0,0 +1,86 @@
|
||||
#include <Wire.h>
|
||||
#include "lcd_i2c.h"
|
||||
|
||||
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);
|
||||
|
||||
struct DisplayData {
|
||||
char topLine[64];
|
||||
int value1;
|
||||
int value2;
|
||||
int value3;
|
||||
};
|
||||
|
||||
struct TextCounter {
|
||||
unsigned long startTime;
|
||||
int incrementValue;
|
||||
};
|
||||
|
||||
TextCounter textCounter;
|
||||
|
||||
void setup() {
|
||||
lcd.begin(16, 2);
|
||||
textCounter.startTime = millis(); // Запоминаем время запуска программы
|
||||
}
|
||||
|
||||
void loop() {
|
||||
unsigned long currentTime = millis(); // Текущее время
|
||||
|
||||
// Проверяем, прошло ли 500 мс с момента последнего увеличения incrementValue
|
||||
if (currentTime - textCounter.startTime >= 500) {
|
||||
textCounter.incrementValue++; // Увеличиваем incrementValue на 1
|
||||
textCounter.startTime = currentTime; // Обновляем время
|
||||
}
|
||||
|
||||
DisplayData displayData;
|
||||
strncpy(displayData.topLine, "we are responsible for those who have been tame ", sizeof(displayData.topLine) - 1);
|
||||
displayData.topLine[sizeof(displayData.topLine) - 1] = '\0';
|
||||
displayData.value1 = 500;
|
||||
displayData.value2 = 800;
|
||||
displayData.value3 = 855;
|
||||
|
||||
// Буферы для заполнения данных
|
||||
char buffer1[17];
|
||||
char buffer2[17];
|
||||
|
||||
// Заполнение буфера 1
|
||||
fillBuffer1(displayData.topLine, buffer1, sizeof(buffer1), textCounter.incrementValue);
|
||||
|
||||
// Заполнение буфера 2
|
||||
fillBuffer2(displayData.value1, displayData.value2, displayData.value3, buffer2, sizeof(buffer2));
|
||||
|
||||
// Создание массива для вывода на дисплей
|
||||
char displayArray[32];
|
||||
strncpy(displayArray, buffer1, 16); // Копирование первых 16 символов из buffer1 в displayArray
|
||||
strncpy(displayArray + 16, buffer2, 16); // Копирование первых 16 символов из buffer2 в displayArray, начиная с позиции 16
|
||||
|
||||
// Вывод данных на экран
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.print(displayArray);
|
||||
|
||||
lcd.setCursor(0, 1);
|
||||
lcd.print(displayArray + 16); // Вывод второй половины displayArray
|
||||
}
|
||||
|
||||
void fillBuffer1(const char * source, char * buffer, size_t bufferSize, int incrementValue) {
|
||||
int startIndex = incrementValue % strlen(source); // Определяем начальный индекс на основе incrementValue
|
||||
int endIndex = startIndex + 16;
|
||||
|
||||
if (endIndex > strlen(source)) {
|
||||
// Если endIndex превышает длину строки source, переносим его на начало строки
|
||||
endIndex = endIndex - strlen(source);
|
||||
|
||||
// Копируем символы с конца строки source
|
||||
strncpy(buffer, source + startIndex, strlen(source) - startIndex);
|
||||
|
||||
// Копируем оставшиеся символы с начала строки source
|
||||
strncat(buffer, source, endIndex);
|
||||
} else {
|
||||
strncpy(buffer, source + startIndex, endIndex - startIndex);
|
||||
}
|
||||
|
||||
buffer[endIndex - startIndex] = '\0'; // Установка нулевого символа в конце буфера
|
||||
}
|
||||
|
||||
void fillBuffer2(int value1, int value2, int value3, char * buffer, size_t bufferSize) {
|
||||
snprintf(buffer, bufferSize, "%d.%d.%d", value1, value2, value3);
|
||||
}
|
76
lcd_i2c.cpp
Normal file
76
lcd_i2c.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include <Arduino.h>
|
||||
#include "lcd_i2c.h"
|
||||
#include <Wire.h>
|
||||
|
||||
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t address, uint8_t cols, uint8_t rows) {
|
||||
_address = address;
|
||||
begin(cols, rows);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t rows) {
|
||||
Wire.begin();
|
||||
command(0x33);
|
||||
command(0x32);
|
||||
command(0x28);
|
||||
command(0x0C);
|
||||
command(0x06);
|
||||
command(0x01);
|
||||
delay(5);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::clear() {
|
||||
command(0x01);
|
||||
delay(5);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::home() {
|
||||
command(0x02);
|
||||
delay(5);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row) {
|
||||
uint8_t row_offsets[] = {0x00, 0x40, 0x14, 0x54};
|
||||
if (row >= 2) {
|
||||
row = 1;
|
||||
}
|
||||
command(0x80 | (col + row_offsets[row]));
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::print(const char* str) {
|
||||
while (*str) {
|
||||
print(*str++);
|
||||
}
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::print(char c) {
|
||||
send(c, 1);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::command(uint8_t value) {
|
||||
send(value, 0);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
|
||||
write4bits(value, mode);
|
||||
write4bits(value << 4, mode);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::write4bits(uint8_t value, uint8_t mode) {
|
||||
uint8_t valueToSend = value | _backlight | mode;
|
||||
Wire.beginTransmission(_address);
|
||||
Wire.write(valueToSend);
|
||||
pulseEnable(valueToSend);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::write8bits(uint8_t value, uint8_t mode) {
|
||||
write4bits(value >> 4, mode);
|
||||
write4bits(value, mode);
|
||||
}
|
||||
|
||||
void LiquidCrystal_I2C::pulseEnable(uint8_t value) {
|
||||
Wire.write(value | 0x04); // Assuming En is bit 2
|
||||
delayMicroseconds(1);
|
||||
Wire.write((value & ~0x04) | _backlight); // Assuming En is bit 2
|
||||
delayMicroseconds(50);
|
||||
}
|
29
lcd_i2c.h
Normal file
29
lcd_i2c.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef LCD_I2C_H
|
||||
#define LCD_I2C_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LCD_ADDRESS 0x27
|
||||
|
||||
class LiquidCrystal_I2C {
|
||||
public:
|
||||
LiquidCrystal_I2C(uint8_t address, uint8_t cols, uint8_t rows);
|
||||
void begin(uint8_t cols, uint8_t rows);
|
||||
void clear();
|
||||
void home();
|
||||
void setCursor(uint8_t col, uint8_t row);
|
||||
void print(const char* str);
|
||||
void print(char c);
|
||||
void command(uint8_t value);
|
||||
|
||||
private:
|
||||
void send(uint8_t value, uint8_t mode);
|
||||
void write4bits(uint8_t value, uint8_t mode);
|
||||
void write8bits(uint8_t value, uint8_t mode);
|
||||
void pulseEnable(uint8_t value);
|
||||
|
||||
uint8_t _address;
|
||||
uint8_t _backlight;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user