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

Most of the low-level stuff reworked

parent c7201ed3
...@@ -21,37 +21,40 @@ Tento soubor vynikl upravou souboru "mojelib.h" jehoz autora bohuzel neznam. ...@@ -21,37 +21,40 @@ Tento soubor vynikl upravou souboru "mojelib.h" jehoz autora bohuzel neznam.
#include <util/delay.h> #include <util/delay.h>
// Inicializace LCD ve 4-bitovem modu // Inicializace LCD ve 4-bitovem modu
void HD44780::init(void) void HD44780::init(void)
{ {
for (auto &pin : data) register_select.off();
{
pin.on();
}
reset.on();
enable.on();
reset.off();
enable.off(); enable.off();
_delay_ms(16); _delay_ms(50);
data[DATA4].on(); data[DATA4].on();
data[DATA5].on(); data[DATA5].on();
len(); len();
_delay_ms(5); _delay_ms(5);
len(); len();
_delay_ms(6); _delay_us(150);
len(); len();
data[DATA4].off(); data[DATA4].off();
len(); len();
send_instruction(0x28); //send_instruction(0x28);
send_instruction(0x06); set_function_set(LINES_2 | DATA_4_BITS | FONT_5x8);
send_instruction(0x0c);
send_instruction(0x01); //send_instruction(0x06);
set_entry_mode(AUTO_INCREMENT);
//send_instruction(0x0c);
set_display_control(DISPLAY_ON | CURSOR_BLINK);
//send_instruction(0x01);
clear();
_delay_ms(3); _delay_ms(3);
} }
...@@ -142,7 +145,7 @@ void HD44780::write_text(char* text) ...@@ -142,7 +145,7 @@ void HD44780::write_text(char* text)
{ {
unsigned char i=0,temp; unsigned char i=0,temp;
while (temp = *(text+i)) while ((temp = *(text + i)))
{ {
write_char(temp); write_char(temp);
i++; i++;
...@@ -154,7 +157,7 @@ void HD44780::write_ftext(const char* text) ...@@ -154,7 +157,7 @@ void HD44780::write_ftext(const char* text)
{ {
unsigned char i=0,temp; unsigned char i=0,temp;
while (temp = pgm_read_byte(text+i)) while ((temp = pgm_read_byte(text + i)))
{ {
write_char(temp); write_char(temp);
i++; i++;
...@@ -164,23 +167,23 @@ void HD44780::write_ftext(const char* text) ...@@ -164,23 +167,23 @@ void HD44780::write_ftext(const char* text)
void HD44780::len(void) //impuls Enable do LCD void HD44780::len(void) //impuls Enable do LCD
{ {
enable.on(); enable.on();
_delay_us(1); _delay_us(2);
enable.off(); enable.off();
_delay_us(50); _delay_us(2);
} }
void HD44780::write(char c) void HD44780::write_data(char c)
{ {
//vystaveni hornich ctyrech bitu na spravne piny: //vystaveni hornich ctyrech bitu na spravne piny:
for (uint8_t i=4; i<8; i++) for (uint8_t i=4; i<8; i++)
{ {
(c & (1<<i)) ? data[4-i].on() : data[4-i].off(); (c & (1<<i)) ? data[i-4].on() : data[i-4].off();
} }
len(); //potvrdit (pulz EN) len(); //potvrdit (pulz EN)
//vystaveni dolni poloviny bytu na spravne piny //vystaveni dolni poloviny bytu na spravne piny
for (uint8_t i=0; i<5; i++) for (uint8_t i=0; i<4; i++)
{ {
(c & (1<<i)) ? data[i].on() : data[i].off(); (c & (1<<i)) ? data[i].on() : data[i].off();
} }
...@@ -191,15 +194,49 @@ void HD44780::write(char c) ...@@ -191,15 +194,49 @@ void HD44780::write(char c)
// Napis znak na LCD: // Napis znak na LCD:
void HD44780::write_char(unsigned char dat) void HD44780::write_char(unsigned char dat)
{ {
reset.on(); register_select.on();
write(dat); write_data(dat);
} }
// Posli instrukci do LCD: // Posli instrukci do LCD:
void HD44780::send_instruction(unsigned char ins) void HD44780::send_instruction(unsigned char ins)
{ {
reset.off(); register_select.off();
write(ins); write_data(ins);
}
void HD44780::set_entry_mode(uint8_t mode)
{
send_instruction(ENTRY_MODE_SET | mode);
}
void HD44780::set_display_control(uint8_t control)
{
send_instruction(DISPLAY_ON_OFF_CONTROL | control);
}
void HD44780::shift(uint16_t shift)
{
send_instruction(CURSOR_OR_DISPLAY_SHIFT | shift);
}
void HD44780::set_function_set(uint8_t set)
{
send_instruction(FUNCTION_SET | set);
}
void HD44780::set_CGRAM_address(uint8_t address)
{
if (address > 0x3F) address = 0x3F;
send_instruction(SET_CGRAM_ADDRESS | address);
}
void HD44780::set_DDRAM_address(uint8_t address)
{
if (address > 0x7F) address = 0x7F;
send_instruction(SET_DDRAM_ADDRESS | address);
} }
//eof //eof
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <avr/pgmspace.h> // pgm_read_byte() #include <avr/pgmspace.h> // pgm_read_byte()
#include <util/delay.h> #include <util/delay.h>
#include <avr/io.h>
#include "gpio.h" #include "gpio.h"
...@@ -19,24 +20,77 @@ struct HD44780_config ...@@ -19,24 +20,77 @@ struct HD44780_config
class HD44780 class HD44780
{ {
private: private:
output reset; output register_select;
output enable; output enable;
output data[4]; output data[4];
enum data_pins enum data_pins
{ {
DATA4, DATA4 = 0,
DATA5, DATA5 = 1,
DATA6, DATA6 = 2,
DATA7 DATA7 = 3
};
enum instructions
{
CLEAR_DISPLAY = 0x01,
RETURN_HOME = 0x02,
ENTRY_MODE_SET = 0x04,
DISPLAY_ON_OFF_CONTROL = 0x08,
CURSOR_OR_DISPLAY_SHIFT = 0x10,
FUNCTION_SET = 0x20,
SET_CGRAM_ADDRESS = 0x40,
SET_DDRAM_ADDRESS = 0x80
};
enum entry_modes
{
AUTO_DECREMENT = 0x00,
AUTO_INCREMENT = 0x02,
SHIFT_ENTIRE_DISPLAY = 0x01,
DONT_SHIFT_ENTIRE_DISPLAY = 0x00
};
enum display_controls
{
DISPLAY_ON = 0x01,
DISPLAF_OFF = 0x00,
CURSOR_OFF = 0x00,
CURSOR_ON = 0x02,
CURSOR_BLINK = 0x06
};
enum shift_modes
{
SHIFT_DISPLAY = 0x08,
SHIFT_CURSOR = 0x00,
SHIFT_RIGHT = 0x04,
SHIFT_LEFT = 0x00
};
enum function_set
{
DATA_4_BITS = 0x00,
DATA_8_BITS = 0x10,
LINES_1 = 0x00,
LINES_2 = 0x08,
FONT_5x10 = 0x04,
FONT_5x8 = 0x00
}; };
void len(); void len();
void write(char c);
void send_instruction(unsigned char ins); void send_instruction(unsigned char ins);
void set_entry_mode(uint8_t mode);
void set_display_control(uint8_t control);
void shift(uint16_t shift);
void set_function_set(uint8_t set);
void set_CGRAM_address(uint8_t address);
void set_DDRAM_address(uint8_t address);
void write_data(char c);
public: public:
HD44780(HD44780_config config): reset(config.port, config.reset), HD44780(HD44780_config config): register_select(config.port, config.reset),
enable(config.port, config.enable), enable(config.port, config.enable),
data{ data{
{config.port, config.data4}, {config.port, config.data4},
...@@ -44,7 +98,9 @@ class HD44780 ...@@ -44,7 +98,9 @@ class HD44780
{config.port, config.data6}, {config.port, config.data6},
{config.port, config.data7}, {config.port, config.data7},
} }
{ } {
}
void init(void); //inicializace void init(void); //inicializace
...@@ -70,25 +126,7 @@ class HD44780 ...@@ -70,25 +126,7 @@ class HD44780
void cursor_blink() { send_instruction(0x0f); }; //kurzor blika void cursor_blink() { send_instruction(0x0f); }; //kurzor blika
void shift_left() { send_instruction(0x18); }; //posun text doleva void shift_left() { send_instruction(0x18); }; //posun text doleva
void shift_right() { send_instruction(0x1c); }; //posun text doprava 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), void gotoxy(uint8_t lin, uint8_t col) { send_instruction(64*((lin)+1)+((col)-1)); }; //jdi na radek (lin)
void gotoxy_new(uint8_t line, uint8_t columm) { set_DDRAM_address( (line*0x40) + columm); };
}; };
// 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)
...@@ -24,7 +24,7 @@ enum directions ...@@ -24,7 +24,7 @@ enum directions
class gpio class gpio
{ {
private: private:
volatile PORT_t &port; PORT_t &port;
uint8_t pin_bm; uint8_t pin_bm;
public: public:
...@@ -77,7 +77,7 @@ class gpio ...@@ -77,7 +77,7 @@ class gpio
class output class output
{ {
private: private:
volatile PORT_t &port; PORT_t &port;
uint8_t pin_bm; uint8_t pin_bm;
public: public:
......
...@@ -14,13 +14,27 @@ int main() ...@@ -14,13 +14,27 @@ int main()
backlight.set_direction(OUTPUT); backlight.set_direction(OUTPUT);
backlight.on(); backlight.on();
HD44780_config lcd_config = {PORTD, PIN2_bm, PIN3_bm, PIN4_bm, PIN5_bm, PIN6_bm, PIN7_bm};
const HD44780_config lcd_config = {PORTD, PIN2_bm, PIN3_bm, PIN4_bm, PIN5_bm, PIN6_bm, PIN7_bm};
HD44780 lcd(lcd_config); HD44780 lcd(lcd_config);
lcd.init(); lcd.init();
lcd.clear(); lcd.clear();
lcd.gotoxy(1,1); for (uint8_t i = 0; i < 2; i++)
{
for (uint8_t j = 0; j < 40; j++)
{
lcd.gotoxy_new(i,j);
lcd.write_char('0');
}
}
//lcd.gotoxy(1,1);
lcd.gotoxy_new(0,0);
lcd.write_char('x'); lcd.write_char('x');
//lcd.gotoxy(2,1);
lcd.gotoxy_new(1,2);
lcd.write_text("Ahoj! :)"); lcd.write_text("Ahoj! :)");
for(;;) 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