2025-08-12 12:25:03 +03:00
2025-08-12 12:25:03 +03:00
2025-08-12 12:25:03 +03:00
2025-08-12 12:25:03 +03:00
2025-08-12 12:25:03 +03:00

FreeRTOS based AVR library for liquid crystal display module 1602(4)A via I2C connection (PCF8574)

Features

  1. Support of 16 LCD 160X on one bus.

Connection

1602(4)A PCF8574
RS P0
E P2
D4 P4
D5 P5
D6 P6
D7 P7

Dependencies

  1. zh_avr_free_rtos
  2. zh_avr_vector
  3. zh_avr_common
  4. zh_avr_i2c
  5. zh_avr_pcf8574

Using

In an existing project, run the following command to install the components:

cd ../your_project/components
git clone http://git.zh.com.ru/avr_libraries/zh_avr_free_rtos
git clone http://git.zh.com.ru/avr_libraries/zh_avr_vector
git clone http://git.zh.com.ru/avr_libraries/zh_avr_i2c
git clone http://git.zh.com.ru/avr_libraries/zh_avr_common
git clone http://git.zh.com.ru/avr_libraries/zh_avr_pcf8574
git clone http://git.zh.com.ru/avr_libraries/zh_160x_i2c

In the application, add the component:

#include "zh_avr_160x_i2c.h"

Examples

One LCD on bus:

#include "avr/io.h"
#include "stdio.h"
#include "zh_avr_160x_i2c.h"

#define BAUD_RATE 9600
#define BAUD_PRESCALE (F_CPU / 16 / BAUD_RATE - 1)

int usart(char byte, FILE *stream)
{
    while ((UCSR0A & (1 << UDRE0)) == 0)
    {
    }
    UDR0 = byte;
    return 0;
}
FILE uart = FDEV_SETUP_STREAM(usart, NULL, _FDEV_SETUP_WRITE);

zh_avr_pcf8574_handle_t lcd_160x_handle = {0};

void lcd_160x_example_task(void *pvParameters)
{
    zh_avr_i2c_master_init(false);
    zh_avr_pcf8574_init_config_t pcf8574_init_config = ZH_AVR_PCF8574_INIT_CONFIG_DEFAULT();
    pcf8574_init_config.i2c_address = 0x38; // 27
    zh_avr_pcf8574_init(&pcf8574_init_config, &lcd_160x_handle);
    zh_avr_160x_init(&lcd_160x_handle, ZH_LCD_16X2); // For LCD 16X2.
    // zh_avr_160x_init(&lcd_160x_handle, ZH_LCD_16X4); // For LCD 16X4.
    for (;;)
    {
        zh_avr_160x_set_cursor(&lcd_160x_handle, 0, 0);
        zh_avr_160x_print_char(&lcd_160x_handle, "LCD 160X");
        zh_avr_160x_set_cursor(&lcd_160x_handle, 1, 0);
        zh_avr_160x_print_char(&lcd_160x_handle, "Hello World!");
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        zh_avr_160x_set_cursor(&lcd_160x_handle, 0, 0); // For LCD 16X2.
        // zh_avr_160x_set_cursor(&lcd_160x_handle, 2, 0); // For LCD 16X4.
        zh_avr_160x_print_char(&lcd_160x_handle, "Progress: ");
        for (uint8_t i = 0; i <= 100; ++i)
        {
            zh_avr_160x_set_cursor(&lcd_160x_handle, 0, 10); // For LCD 16X2.
            // zh_avr_160x_set_cursor(&lcd_160x_handle, 2, 10); // For LCD 16X4.
            zh_avr_160x_print_int(&lcd_160x_handle, i);
            zh_avr_160x_print_char(&lcd_160x_handle, "%");
            zh_avr_160x_print_progress_bar(&lcd_160x_handle, 1, i); // For LCD 16X2.
            // zh_avr_160x_print_progress_bar(&lcd_160x_handle, 3, i); // For LCD 16X4.
            vTaskDelay(100 / portTICK_PERIOD_MS);
        }
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        zh_avr_160x_lcd_clear(&lcd_160x_handle);
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        printf("Task Remaining Stack Size %d.\n", uxTaskGetStackHighWaterMark(NULL));
    }
    vTaskDelete(NULL);
}
int main(void)
{
    UBRR0H = (BAUD_PRESCALE >> 8);
    UBRR0L = BAUD_PRESCALE;
    UCSR0B = (1 << RXEN0) | (1 << TXEN0);
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
    stdout = &uart;
    xTaskCreate(lcd_160x_example_task, "lcd 160x example task", 124, NULL, tskIDLE_PRIORITY, NULL);
    vTaskStartScheduler();
    return 0;
}

void zh_avr_pcf8574_event_handler(zh_avr_pcf8574_event_on_isr_t *event) // Do not delete! Required for zh_avr_pcf8574 library work.
{
}
Description
FreeRTOS based AVR library for liquid crystal display module 1602(4)A via I2C connection (PCF8574).
Readme 36 KiB
Languages
C 100%