Disitu tampak kalau ada variable resistor yang terpasang di 3 pin LCD yang diunakan untuk mengatur contras dari LCD yang dipakai. Konfigurasi tersebut bisa di rubah ke port lain dengan meng-edit di file hd44780.h. Konfigurasi pin dari LCD dan board STM32F4 adalah sebagai berikut:
- LCD_D4 >>> PORTE 14
- LCD_D5 >>> PORTE 13
- LCD_D6 >>> PORTE 11
- LCD_D7 >>> PORTE 12
- LCD_RS >>> PORTE 8
- LCD_RW >>> PORTE 7
- LCD_EN >>> PORTE 10
Terus terang, library ini bukan buatan saya sendiri, tapi saya copy dari hasil searching di internet, dan saya juga lupa dari mana saya mendapatkanya, berhubung banyaknya library LCD yang saya coba. Tapi berikut library yang saya peroleh:
//******************************************************************************
// THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY.
// AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
// OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
// FROM USE OF THIS SOFTWARE.
//******************************************************************************
//##############################################################################
// simpan file ini dengan nama hd44780.h
//##############################################################################
//****************************************************************************//
//#include "main.h"
#define LCD_GPIO GPIOE
#define LCD_CLK_LINE RCC_AHB1Periph_GPIOE
#define LCD_D4 GPIO_Pin_14
#define LCD_D5 GPIO_Pin_13
#define LCD_D6 GPIO_Pin_11
#define LCD_D7 GPIO_Pin_12
#define LCD_RS GPIO_Pin_8
#define LCD_RW GPIO_Pin_7
#define LCD_EN GPIO_Pin_10
//******************************************************************************//
#define HD44780_CLEAR 0x01
#define HD44780_HOME 0x02
#define HD44780_ENTRY_MODE 0x04
#define HD44780_EM_SHIFT_CURSOR 0
#define HD44780_EM_SHIFT_DISPLAY 1
#define HD44780_EM_DECREMENT 0
#define HD44780_EM_INCREMENT 2
#define HD44780_DISPLAY_ONOFF 0x08
#define HD44780_DISPLAY_OFF 0
#define HD44780_DISPLAY_ON 4
#define HD44780_CURSOR_OFF 0
#define HD44780_CURSOR_ON 2
#define HD44780_CURSOR_NOBLINK 0
#define HD44780_CURSOR_BLINK 1
#define HD44780_DISPLAY_CURSOR_SHIFT 0x10
#define HD44780_SHIFT_CURSOR 0
#define HD44780_SHIFT_DISPLAY 8
#define HD44780_SHIFT_LEFT 0
#define HD44780_SHIFT_RIGHT 4
#define HD44780_FUNCTION_SET 0x20
#define HD44780_FONT5x7 0
#define HD44780_FONT5x10 4
#define HD44780_ONE_LINE 0
#define HD44780_TWO_LINE 8
#define HD44780_4_BIT 0
#define HD44780_8_BIT 16
#define HD44780_CGRAM_SET 0x40
#define HD44780_DDRAM_SET 0x80
//##############################################################
void lcd_init(void);
void lcd_cls(void);
void lcd_str(unsigned char * text);
void lcd_strxy(unsigned char * text, unsigned char x, unsigned char y);
void lcd_locate(unsigned char x, unsigned char y);
void lcd_int(int n);
void lcd_intxy(int n, unsigned char x, unsigned char y);
//unsigned char* intToStr(int n);
//###############################################################
void lcd_writedata(unsigned char dataToWrite);
void lcd_writecommand(unsigned char commandToWrite);
void lcd_writebinary(unsigned int var, unsigned char bitCount);
void lcd_addchar (unsigned char chrNum, unsigned char n, const unsigned char *p);
Sedangkan Source dari library diatas adalah sebagai berikut:
/*simpan file ini dengan naman hd44780.c*/
#include "hd44780.h"
#include "stm32f4xx_gpio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stm32f4_discovery.h"
GPIO_InitTypeDef GPIO_InitStructure;
//-----------------------------------------------------------------------------
void lcd_writenibble(unsigned char nibbleToWrite)
{
GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET);
GPIO_WriteBit(LCD_GPIO, LCD_D4,(BitAction) (nibbleToWrite & 0x01));
GPIO_WriteBit(LCD_GPIO, LCD_D5,(BitAction)(nibbleToWrite & 0x02));
GPIO_WriteBit(LCD_GPIO, LCD_D6,(BitAction)(nibbleToWrite & 0x04));
GPIO_WriteBit(LCD_GPIO, LCD_D7,(BitAction)(nibbleToWrite & 0x08));
GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET);
}
//-----------------------------------------------------------------------------
unsigned char LCD_ReadNibble(void)
{
unsigned char tmp = 0;
GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET);
tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D4) << 0);
tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D5) << 1);
tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D6) << 2);
tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D7) << 3);
GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET);
return tmp;
}
//-----------------------------------------------------------------------------
unsigned char LCD_ReadStatus(void)
{
unsigned char status = 0;
GPIO_InitStructure.GPIO_Pin = LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(LCD_GPIO, &GPIO_InitStructure);
GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_SET);
GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET);
status |= (LCD_ReadNibble() << 4);
status |= LCD_ReadNibble();
GPIO_InitStructure.GPIO_Pin = LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(LCD_GPIO, &GPIO_InitStructure);
return status;
}
//-----------------------------------------------------------------------------
void lcd_writedata(unsigned char dataToWrite)
{
GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_RESET);
GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_SET);
lcd_writenibble(dataToWrite >> 4);
lcd_writenibble(dataToWrite & 0x0F);
while(LCD_ReadStatus() & 0x80);
}
//-----------------------------------------------------------------------------
void lcd_writecommand(unsigned char commandToWrite)
{
GPIO_WriteBit(LCD_GPIO, LCD_RW | LCD_RS, Bit_RESET);
lcd_writenibble(commandToWrite >> 4);
lcd_writenibble(commandToWrite & 0x0F);
while(LCD_ReadStatus() & 0x80);
}
//-----------------------------------------------------------------------------
void lcd_str(unsigned char * text)
{
while(*text)
lcd_writedata(*text++);
}
//-----------------------------------------------------------------------------
void lcd_locate(unsigned char x, unsigned char y)
{
lcd_writecommand(HD44780_DDRAM_SET | (x + (0x40 * y)));
}
//-----------------------------------------------------------------------------
void lcd_strxy(unsigned char * text, unsigned char x, unsigned char y)
{
lcd_locate(x,y);
while(*text)
lcd_writedata(*text++);
}
//-----------------------------------------------------------------------------
void lcd_writebinary(unsigned int var, unsigned char bitCount)
{
signed char i;
for(i = (bitCount - 1); i >= 0; i--)
{
lcd_writedata((var & (1 << i))?'1':'0');
}
}
//-----------------------------------------------------------------------------
void LCD_ShiftLeft(void)
{
lcd_writecommand(HD44780_DISPLAY_CURSOR_SHIFT | HD44780_SHIFT_LEFT | HD44780_SHIFT_DISPLAY);
}
//-----------------------------------------------------------------------------
void LCD_ShiftRight(void)
{
lcd_writecommand(HD44780_DISPLAY_CURSOR_SHIFT | HD44780_SHIFT_RIGHT | HD44780_SHIFT_DISPLAY);
}
//-----------------------------------------------------------------------------
void lcd_init(void)
{
volatile unsigned char i = 0;
volatile unsigned int delayCnt = 0;
RCC_AHB1PeriphClockCmd(LCD_CLK_LINE, ENABLE);
GPIO_InitStructure.GPIO_Pin = LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_RW|LCD_EN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(LCD_GPIO, &GPIO_InitStructure);
GPIO_ResetBits(LCD_GPIO, LCD_RS | LCD_EN | LCD_RW);
for(delayCnt = 0; delayCnt < 300000; delayCnt++);
for(i = 0; i < 3; i++) {
lcd_writenibble(0x03);
for(delayCnt = 0; delayCnt < 30000; delayCnt++);
}
lcd_writenibble(0x02);
for(delayCnt = 0; delayCnt < 6000; delayCnt++);
lcd_writecommand(HD44780_FUNCTION_SET |
HD44780_FONT5x7 |
HD44780_TWO_LINE |
HD44780_4_BIT);
lcd_writecommand(HD44780_DISPLAY_ONOFF |
HD44780_DISPLAY_OFF);
lcd_writecommand(HD44780_CLEAR);
lcd_writecommand(HD44780_ENTRY_MODE |
HD44780_EM_SHIFT_CURSOR |
HD44780_EM_INCREMENT);
lcd_writecommand(HD44780_DISPLAY_ONOFF |
HD44780_DISPLAY_ON |
HD44780_CURSOR_OFF |
HD44780_CURSOR_NOBLINK);
}
//-----------------------------------------------------------------------------
void lcd_addchar (unsigned char chrNum, unsigned char n, const unsigned char *p)
{
lcd_writecommand(HD44780_CGRAM_SET | chrNum * 8);
n *= 8;
do
lcd_writedata(*p++);
while (--n);
}
//-----------------------------------------------------------------------------
void lcd_cls(void){
lcd_writecommand(HD44780_CLEAR);
}
unsigned char* intToStr(int n)
{
int i = 0;
int j = 0;
char *tmp = (char*)malloc(sizeof(char));
unsigned char *ret = (unsigned char*)malloc(12);
if(n<0 font="">0>
*tmp = n%10+48;
n-=n%10;
n/=10;
tmp++;
i++;
}
*tmp = n+48;
i++;
while(i--){
ret[j++] = *tmp--;
}
return ret;
}
void lcd_int(int a){
unsigned short ch,first; //you need this to display 0 if there was any char
//Get first char
ch = a/10000;
if(ch){
lcd_writedata(48+ch);
first = 1;
}
//Get second char
ch = (a/1000)%10;
if(ch || first){
lcd_writedata(48+ch);
first = 1;
}
//Get third char
ch = (a/100)%10;
if(ch || first){
lcd_writedata(48+ch);
first = 1;
}
//Get fourth char
ch = (a/10)%10;
if(ch || first){
lcd_writedata(48+ch);
//first = 1; //q
}
//Get fifth char
ch = a%10;
//if(ch || first) //you dont need to check las one if ch is 0 then just display it, unless you dont want to then uncomment this line ("//q" line too)
lcd_writedata(48+ch);
// lcd_str(intToStr(n));
}
void lcd_intxy(int n, unsigned char x, unsigned char y)
{
lcd_locate(x,y);
lcd_int(n);
}
Dari sekian banyaknya fungsi, saya cuman baru make beberapa fungsi saja. Yang ditampilkan di postingan ini hanya untuk menampilkan karakter sdan string ke LCD saja. Pelan pelan saja, jangan buru buru. hehehe
Berikut Fungsi main.c dari project yang saya buat:
#include
#include
#include
#include
#include
int main(void)
{
lcd_init();
lcd_cls();
lcd_strxy("_ardiansm",0,0);
lcd_strxy("CircuitDrilling",0,1);
}
Sudah dulu ya, cukup begitu saja. Barangkali ada yang punya pikiran, kenapa g dijelasin proses komunikasi data antara LCD dan board STM32F4 nya. Karena saya punya pikiran, LCD 16x2 ini sudah banyak sekali yang membahas, dan sebagian besar aplikasi LCD ini hanya untuk display, hanya penting pada beberapa aplikasi HMI saja. Dan lagi pula, kebanyakan orang juga jarang ada yang mau baca detail komunikasinya, cukup copy paste library, udah deh selasai.
Ditunggu komen dan kritiknya yak.
2 komentar:
makasih mas sangat membantu..
Posting Komentar