diff --git a/.gitignore b/.gitignore index e257658..8fa9acc 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ *.out *.app +bin/* +.vscode/* diff --git a/README.md b/README.md index bc4791c..fa6aa64 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ -# lcd +# AVR LCD Library + +Original source + +https://www.electronicwings.com/avr-atmega/interfacing-lcd-16x2-in-4-bit-mode-with-atmega-16-32- diff --git a/lcd.cpp b/lcd.cpp new file mode 100644 index 0000000..110c87b --- /dev/null +++ b/lcd.cpp @@ -0,0 +1,68 @@ +#include +#include "lcd.h" + +namespace lcd { + void command(unsigned char cmnd) { + LCD_Port = (LCD_Port & 0x0F) | (cmnd & 0xF0); /* sending upper nibble */ + LCD_Port &= ~ (1<15ms */ + command(0x02); /* send for 4 bit initialization of LCD */ + command(0x28); /* 2 line, 5*7 matrix in 4-bit mode */ + command(0x0c); /* Display on cursor off*/ + command(0x06); /* Increment cursor (shift cursor to right)*/ + command(0x01); /* Clear display screen*/ + _delay_ms(2); + } + + + void string (char *str) { + int i; + for(i=0;str[i]!=0;i++) { /* Send each char of string till the NULL */ + put(str[i]); + } + } + + void string_xy (char row, char pos, char *str) { + if (row == 0 && pos<16) + command((pos & 0x0F)|0x80); /* Command of first row and required position<16 */ + else if (row == 1 && pos<16) + command((pos & 0x0F)|0xC0); /* Command of first row and required position<16 */ + string(str); /* Call LCD string function */ + } + + void clear() { + command (0x01); /* Clear display */ + _delay_ms(2); + command (0x80); /* Cursor at home position */ + } +} // namespace lcd diff --git a/lcd.h b/lcd.h new file mode 100644 index 0000000..a394f04 --- /dev/null +++ b/lcd.h @@ -0,0 +1,22 @@ +#pragma once +#include + + +#define LCD_Dir DDRA /* Define LCD data port direction */ +#define LCD_DDR DDRA /* Define LCD data port direction */ +#define LCD_Port PORTA /* Define LCD data port */ +#define LCD_Port PORTA /* Define LCD data port */ +#define LCD_RS PA2 /* Define Register Select pin */ +#define LCD_EN PA3 /* Define Enable signal pin */ + + +namespace lcd { + void command(unsigned char cmnd); + void put(unsigned char data); + void init(); + void string(char *str); + void string_xy (char row, char pos, char *str); + void clear(); + + +} // namespace lcd diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9af9a69 --- /dev/null +++ b/main.cpp @@ -0,0 +1,18 @@ +#define F_CPU 8000000UL /* Define CPU Frequency e.g. here 8MHz */ +#include /* Include AVR std. library file */ +#include /* Include Delay header file */ +#include "lcd.h" + + + + +int main() +{ + + lcd::init(); /* Initialization of LCD*/ + + lcd::string("CVR"); /* Write string on 1st line of LCD*/ + lcd::command(0xC0); /* Go to 2nd line*/ + lcd::string("Hello World"); /* Write string on 2nd line*/ + while(1); +} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..4238502 --- /dev/null +++ b/makefile @@ -0,0 +1,21 @@ +MCU=atmega32 +F_CPU=8000000 +PROG=dragon_jtag +CC=avr-g++ +OBJCOPY=avr-objcopy +CFLAGS=-Wall -g -mmcu=${MCU} -DF_CPU=${F_CPU} -I. +TARGET=main +SRCS=*.cpp + +default: build flash + +build: + ${CC} ${CFLAGS} -o bin/${TARGET}.bin ${SRCS} + #${CC} ${CFLAGS} -o bin/${TARGET}.elf ${SRCS} + ${OBJCOPY} -j .text -j .data -O ihex bin/${TARGET}.bin bin/${TARGET}.hex + +flash: + avrdude -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex + +clean: + rm -f bin/*