#include "lcd.h"
#include <util/delay.h>
#define LCD_RS PA0
#define LCD_RW PA1
#define LCD_E PA2
#define LCD_D4 PA3
#define LCD_D5 PA4
#define LCD_D6 PA5
#define LCD_D7 PA6
const uint8_t char_S_capped[] = {
0b00100, // Ś
0b01110,
0b10001,
0b10000,
0b01110,
0b00001,
0b10001,
0b01110
};
const uint8_t char_e_tail[] = {
0b00000, // ę
0b00000,
0b01110,
0b10001,
0b11111,
0b10000,
0b01110,
0b00010
};
static void pulse_E() {
PORTA |= (1 << LCD_E);
_delay_us(1);
PORTA &= ~(1 << LCD_E);
_delay_us(50);
}
static void lcd_send_half(uint8_t data) {
PORTA &= ~((1 << LCD_D7) | (1 << LCD_D6) | (1 << LCD_D5) | (1 << LCD_D4));
if (data & (1 << 3)) PORTA |= (1 << LCD_D7);
if (data & (1 << 2)) PORTA |= (1 << LCD_D6);
if (data & (1 << 1)) PORTA |= (1 << LCD_D5);
if (data & (1 << 0)) PORTA |= (1 << LCD_D4);
pulse_E();
}
void lcd_send_byte(uint8_t data, uint8_t is_data) {
if (is_data) PORTA |= (1 << LCD_RS);
else PORTA &= ~(1 << LCD_RS);
PORTA &= ~(1 << LCD_RW); // RW = 0
lcd_send_half(data >> 4);
lcd_send_half(data & 0x0F);
_delay_ms(2);
}
void lcd_init(void) {
DDRA |= (1 << LCD_RS) | (1 << LCD_RW) | (1 << LCD_E) |
(1 << LCD_D4) | (1 << LCD_D5) | (1 << LCD_D6) | (1 << LCD_D7);
_delay_ms(15);
lcd_send_half(0x03); _delay_ms(5);
lcd_send_half(0x03); _delay_us(100);
lcd_send_half(0x03); _delay_us(100);
lcd_send_half(0x02); _delay_us(100);
lcd_send_byte(0x28, 0); // 4-bit, 2-linie
lcd_send_byte(0x0C, 0); // display ON
lcd_send_byte(0x06, 0); // inkrementacja
lcd_send_byte(0x01, 0); _delay_ms(2);
lcd_create_char(0, char_S_capped); // Ś pod indeksem 0
lcd_create_char(1, char_e_tail); // ę pod indeksem 1
}
void lcd_clear(void) {
lcd_send_byte(0x01, 0);
_delay_ms(2);
}
void lcd_gotoxy(uint8_t x, uint8_t y) {
uint8_t addr = (y == 0) ? 0x00 : 0x40;
lcd_send_byte(0x80 | (addr + x), 0);
}
void lcd_puts(const char* str) {
while (*str) lcd_send_byte(*str++, 1);
}
void lcd_show_loading(void) {
lcd_clear(); lcd_gotoxy(0, 0); lcd_puts("Loading..."); _delay_ms(1000); lcd_clear();
}
void lcd_show_startstop(uint8_t running) {
lcd_clear(); lcd_gotoxy(0, 0);
if (running) lcd_puts("Stopping...");
else lcd_puts("Starting...");
_delay_ms(1000); lcd_clear();
}
void lcd_create_char(uint8_t location, const uint8_t *charmap) {
location &= 0x07; // tylko 0-7
lcd_send_byte(0x40 | (location << 3), 0); // ustaw adres CGRAM
for (uint8_t i = 0; i < 8; i++) {
lcd_send_byte(charmap[i], 1);
}
}
Read 4 times, last 28 hours ago