Inital commit

This commit is contained in:
Jimmy 2020-12-06 20:26:32 +13:00
parent 05fa85c78d
commit 2281caedfc
6 changed files with 136 additions and 1 deletions

2
.gitignore vendored
View File

@ -32,3 +32,5 @@
*.out
*.app
bin/*
.vscode/*

View File

@ -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-

68
lcd.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <util/delay.h>
#include "lcd.h"
namespace lcd {
void command(unsigned char cmnd) {
LCD_Port = (LCD_Port & 0x0F) | (cmnd & 0xF0); /* sending upper nibble */
LCD_Port &= ~ (1<<LCD_RS); /* LCD_RS=0, command reg. */
LCD_Port |= (1<<LCD_EN); /* Enable pulse */
_delay_us(1);
LCD_Port &= ~ (1<<LCD_EN);
_delay_us(200);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4); /* sending lower nibble */
LCD_Port |= (1<<LCD_EN);
_delay_us(1);
LCD_Port &= ~ (1<<LCD_EN);
_delay_ms(2);
}
void put(unsigned char data) {
LCD_Port = (LCD_Port & 0x0F) | (data & 0xF0); /* sending upper nibble */
LCD_Port |= (1<<LCD_RS); /* LCD_RS=1, data reg. */
LCD_Port|= (1<<LCD_EN);
_delay_us(1);
LCD_Port &= ~ (1<<LCD_EN);
_delay_us(200);
LCD_Port = (LCD_Port & 0x0F) | (data << 4); /* sending lower nibble */
LCD_Port |= (1<<LCD_EN);
_delay_us(1);
LCD_Port &= ~ (1<<LCD_EN);
_delay_ms(2);
}
void init(void) {
LCD_DDR = (1<<LCD_EN)|(1<<LCD_RS)|0xF0; /* Make LCD port direction as o/p */
_delay_ms(20); /* LCD Power ON delay always >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

22
lcd.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <avr/io.h>
#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

18
main.cpp Normal file
View File

@ -0,0 +1,18 @@
#define F_CPU 8000000UL /* Define CPU Frequency e.g. here 8MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* 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);
}

21
makefile Normal file
View File

@ -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/*