Commit c6d5b0c7 authored by Martin Vítek's avatar Martin Vítek
Browse files

Add and classed HD44780 display lib

parent a44b424c
......@@ -36,6 +36,8 @@ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-Wl,--gc-sections -Wl,--relax -Os -mrela
set(SOURCE_FILES
main.cpp
gpio.h
HD44780.cpp
HD44780.h
)
add_avr_executable(safe-soldering-station ${SOURCE_FILES})
/*---------------------------------------------------------------------------
soubor: lcd.c
verze: 1.0
datum: 28.1.2013
popis:
Obsluzne rutiny pro displej 2x16 znaku s radicem HD44780
-Komunikace je ctyrbitova (pouzivaji se pouze piny displeje D4 - D7)
-Displej je v rezimu "pouze zapis" (signal "R/W" displeje je uzemen)
-Displej je pripojen k mikrokontroleru pouze signaly: RS, EN a D4 az D7.
-po��t� se i s jin�m vyu�it�m pro D4 a� D7 - p�ed ka�d�m pou�it�m se nastav� jako v�stupn� -> pokud nejsou zrovna pou��v�ny displejem, jsou voln� k dispozici.
Tento soubor vynikl upravou souboru "mojelib.h" jehoz autora bohuzel neznam.
---------------------------------------------------------------------------*/
#include "HD44780.h"
#include <util/delay.h>
// Inicializace LCD ve 4-bitovem modu
void HD44780::init(void)
{
for (auto &pin : data)
{
pin.on();
}
reset.on();
enable.on();
reset.off();
enable.off();
_delay_ms(16);
data[DATA4].on();
data[DATA5].on();
len();
_delay_ms(5);
len();
_delay_ms(6);
len();
data[DATA4].off();
len();
send_instruction(0x28);
send_instruction(0x06);
send_instruction(0x0c);
send_instruction(0x01);
_delay_ms(3);
}
//napi� neznam�nkov� int
void HD44780::write_int_d(unsigned int cislo, unsigned char znak, unsigned char pocet_mist)
{
unsigned char i;
unsigned char cislice;
unsigned int delitel=1;
if (pocet_mist == 0) return;
if (pocet_mist>5) pocet_mist=5;//max 5 m�st (int = 65535)
for (i=1; i<pocet_mist; i++)
{
delitel *= 10;//vznikne 10^(pocet_mist-1)
}
if (pocet_mist<5) cislo%=(delitel*10);//o��znut� zbyte�n�ch ��slic
cislice = cislo/delitel;
if (cislice==0)//pokod je tento ��d nulov�
{
if (pocet_mist==1)
{
write_char('0');//napi� nulu
}
else
{
if (znak)//pokud znak nen� NULL
{
write_char(znak); //zobraz uvodni znak
}
}
}
else //jinak
{
write_char('0'+ cislice);//zobraz ��slici
znak=('0'); //dalsi znak u� bude nula
}
write_int_d(cislo, znak, pocet_mist-1);
}
//napi� znam�nkov� int
void HD44780::write_int_s(int cislo, unsigned char znak, unsigned char pocet_mist)
{
if (cislo<0)//pokud je ��slo z�porn�
{
write_char('-');//napi� m�nus
write_int_d((-cislo), znak, pocet_mist);//p�eve� na kladn� a vypi� ��slo
}
else
{
write_char(' ');//napi� mezeru m�sto m�nus
write_int_d( cislo, znak, pocet_mist);//vypi� ��slo
}
}
void HD44780::write_hex8(unsigned char cislo)
{
unsigned char tmp;
tmp = cislo>>4; //horn� p�lbyte
if(tmp>=10) //pokud je ��slo v�t�� nebo rovno 10
write_char('A' - 10 + tmp);//napi� p�smeno
else
write_char('0' + tmp);//jinak napi� ��slici
tmp = cislo & 0x0f; //doln� p�lbyte
if(tmp>=10) //pokud je ��slo v�t�� nebo rovno 10
write_char('A' - 10 + tmp);//napi� p�smeno
else
write_char('0' + tmp);//jinak napi� ��slici
}
void HD44780::write_hex16(unsigned int cislo)
{
unsigned char *ptr;
ptr =(unsigned char*) &cislo;
write_hex8(*(ptr+1));
write_hex8(*ptr);
}
//napis string, umisteny v RAM
void HD44780::write_text(char* text)
{
unsigned char i=0,temp;
while (temp = *(text+i))
{
write_char(temp);
i++;
}
}
//napis string, umisteny ve FLASH
void HD44780::write_ftext(const char* text)
{
unsigned char i=0,temp;
while (temp = pgm_read_byte(text+i))
{
write_char(temp);
i++;
}
}
void HD44780::len(void) //impuls Enable do LCD
{
enable.on();
_delay_us(1);
enable.off();
_delay_us(50);
}
void HD44780::write(char c)
{
//vystaveni hornich ctyrech bitu na spravne piny:
for (uint8_t i=4; i<8; i++)
{
(c & (1<<i)) ? data[4-i].on() : data[4-i].off();
}
len(); //potvrdit (pulz EN)
//vystaveni dolni poloviny bytu na spravne piny
for (uint8_t i=0; i<5; i++)
{
(c & (1<<i)) ? data[i].on() : data[i].off();
}
len(); //potvrdit (pulz EN)
}
// Napis znak na LCD:
void HD44780::write_char(unsigned char dat)
{
reset.on();
write(dat);
}
// Posli instrukci do LCD:
void HD44780::send_instruction(unsigned char ins)
{
reset.off();
write(ins);
}
//eof
//(c) OK1ZKV 2012 && Martin Vítek 2016
#pragma once
#include <avr/pgmspace.h> // pgm_read_byte()
#include <util/delay.h>
#include "gpio.h"
struct HD44780_config
{
PORT_t port;
uint8_t reset;
uint8_t enable;
uint8_t data4;
uint8_t data5;
uint8_t data6;
uint8_t data7;
};
class HD44780
{
private:
output reset;
output enable;
output data[4];
enum data_pins
{
DATA4,
DATA5,
DATA6,
DATA7
};
void len();
void write(char c);
void send_instruction(unsigned char ins);
public:
HD44780(HD44780_config config): reset(config.port, config.reset),
enable(config.port, config.enable),
data{
{config.port, config.data4},
{config.port, config.data5},
{config.port, config.data6},
{config.port, config.data7},
}
{ }
void init(void); //inicializace
void write_int_d (unsigned int cislo, unsigned char znak, unsigned char pocet_mist);//napise int dekadicky na dany pocet cifer
void write_int_s ( int cislo, unsigned char znak, unsigned char pocet_mist);//vypise znamenkovy int
void write_hex8(unsigned char cislo); //vyp�e byt hexadecim�ln�
void write_hex16(unsigned int cislo); //vyp�e int hexadecim�ln�
void write_char (unsigned char dat); //napis znak
void write_text (char* text); //napis string, umisteny v RAM
void write_ftext(const char* text); //napis string, umisteny ve FLASH
void write_num(unsigned int cislo) { write_int_d(cislo, ' ', 5); };
void write_num_s(int cislo) { write_int_s(cislo, ' ', 5); };
void write_int(int cislo) { write_int_s(cislo, ' ', 5); };
void write_byt(char cislo) { write_int_d((cislo),' ',3); };
void write_byt_s(char cislo) { write_int_s((cislo),' ',3); };
void clear() { send_instruction(0x01); _delay_ms(3); }; //vymaz displej
void cursor_off() { send_instruction(0x0c); }; //vypni kurzor
void cursor_on() { send_instruction(0x0e); }; //zapni kurzor
void cursor_blink() { send_instruction(0x0f); }; //kurzor blika
void shift_left() { send_instruction(0x18); }; //posun text doleva
void shift_right() { send_instruction(0x1c); }; //posun text doprava
void gotoxy(uint8_t lin, uint8_t col) { send_instruction(64*((lin)+1)+((col)-1)); }; //jdi na radek (lin),
};
// Funkce pro displej:
// Makra pro displej:
#define lcd_num(cislo) write_int_d((cislo),' ',5)//vyp�e ��slo 0..65535 bez po��te�n�ch nul
#define lcd_num_s(cislo) write_int_s((cislo),' ',5)//vyp�e ��slo -32768..32767 bez po��te�n�ch nul
#define lcd_int(cislo) write_int_s((cislo),' ',5)//vyp�e ��slo -32768..32767 bez po��te�n�ch nul
#define lcd_byt(cislo) write_int_d((cislo),' ',3)//vyp�e ��slo 0..999 bez pocatecnich nul
#define lcd_byt_s(cislo) write_int_s((cislo),' ',3)//vyp�e ��slo -999..999 bez pocatecnich nul
#define lcd_TEXT(text) write_ftext(PSTR(text)); //text je v programov� pam�ti a nep�ek�� v RAMce
#define lcd_cls() send_instruction(0x01);wait_ms(3)
......@@ -2,13 +2,26 @@
#include <util/delay.h>
#include "gpio.h"
#include "HD44780.h"
int main()
{
gpio led(PORTD, PIN0_bm);
led.set_direction(OUTPUT);
//on<&PORTD, PIN0_bm>();
gpio backlight(PORTD, PIN1_bm);
backlight.set_direction(OUTPUT);
backlight.on();
HD44780_config lcd_config = {PORTD, PIN2_bm, PIN3_bm, PIN4_bm, PIN5_bm, PIN6_bm, PIN7_bm};
HD44780 lcd(lcd_config);
lcd.init();
lcd.clear();
lcd.gotoxy(1,1);
lcd.write_char('x');
lcd.write_text("Ahoj! :)");
for(;;)
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment