Welcome, Guest
Username Password: Remember me

Character LCD 16x2 (HD44780)
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Character LCD 16x2 (HD44780)

Character LCD 16x2 (HD44780) 20 Aug 2012 04:05 #230

  • heitor
  • OFFLINE
  • Fresh Boarder
  • Posts: 6
Hi,
Following are my code to drive a commom character LCD 16x2.
I hope to help the beginners (like me) with this code.
To create a new projet in CoIDE you need to choose NXP -> LPC1769 -> Repository options:
  • CMSIS core
  • Commom_Header
  • CMSIS_boot
  • CLKPWR
  • GPIO


So you need to coppy the code for the 3 files:
main.c (CoIDE will create this file automatic)
lcd.h (create a new file in lpc17xxlib -> include)
lcd.c (create a new file in lpc17xxlib -> source)

After that Build (F7), Download code to Flash and enjoy.

file: main.c
/* This program demonstrates a use of (HD44780) 16x2 Character Display on SimpleCortex board.
* The LCD is used in 4 bit configuration and following are the hardware connections.
* ---LCD--- --SimpleCortex--
* (2)VCC 5V
* (1)GND gnd
* (4)RS p2.3
* (5)RW p2.4
* (6)EN p2.5
* (11)DB4 p0.4
* (12)DB5 p0.5
* (13)DB6 p0.6
* (14)DB7 p0.7
* @ Author: Heitor Mercaldi
* @ Date Aug. 2012
* @ Version 1.0
*/
#include "LPC17xx.h"
#include "lpc_types.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_gpio.h"
#include "lcd.h"

int main(void)
{
SystemInit(); /* initialize clocks */
init_lcd();
//lcd_backlight_on();
lcd_putstring(0,"SimpleCortex 1.3");
lcd_putstring(1," LPC1769 (nxp) ");
while(1)
{
}
}

file lcd.h
#ifndef _LCD_H
#define _LCD_H

#include "lpc17xx.h"
#include "lpc_types.h"

#define MAX_CHAR_IN_ONE_LINE 16

#define LCD_BACK_LIGHT_TIMEOUT 1000

#define LCD_BACKLIGHT (1 << 29)

#define LCD_BACK_LIGHT_DIR LPC_GPIO4->FIODIR
#define LCD_BACK_LIGHT_SET LPC_GPIO4->FIOSET
#define LCD_BACK_LIGHT_CLR LPC_GPIO4->FIOCLR

#define LCD_DATA_DIR LPC_GPIO0->FIODIR
#define LCD_DATA_SET LPC_GPIO0->FIOSET
#define LCD_DATA_CLR LPC_GPIO0->FIOCLR

#define LCD_CTRL_DIR LPC_GPIO2->FIODIR
#define LCD_CTRL_SET LPC_GPIO2->FIOSET
#define LCD_CTRL_CLR LPC_GPIO2->FIOCLR

#define LCDRS (1 << 3)
#define LCDRW (1 << 4)
#define LCDEN (1 << 5)

#define LCD_D4 (1 << 4)
#define LCD_D5 (1 << 5)
#define LCD_D6 (1 << 6)
#define LCD_D7 (1 << 7)

#define LCD_DATA_MASK (LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7)
#define LCD_BUSY_FLAG LCD_D7

//#define LCD_CONTROL_MASK 0x01C00000

enum ROW_NUMBERS
{
LINE1,
LINE2
};

void init_lcd(void);
void lcd_putstring(unsigned char line, char *string);
void lcd_clear(void);
void lcd_backlight_on(void);
void lcd_backlight_off(void);
int lcd_gotoxy(unsigned int x, unsigned int y);
void lcd_putchar(int c);

#endif

file: lcd.c
#include "lcd.h"

/****************************************************************************
Function Name : delay()
Description :This function suspends the tasks for specified ticks.
Input : ticks:no of ticks in multiple of 1 usec
task: task to be suspended
Output : void
Note :
*****************************************************************************/
void delay(int count)
{
int j=0,i=0;
for(j=0;j<count;j++)
{
/* At 60Mhz, the below loop introduces delay of 10 us */
for(i=0;i<35;i++);
}
}

/*******************************************************************************************
Function Name : wait_lcd()
Description :
Input :
Output : Void
Note :
****************************************************************************************/
void wait_lcd( void )
{
LCD_DATA_DIR &= ~LCD_DATA_MASK;
LCD_CTRL_CLR |= LCDRS;
LCD_CTRL_SET |= LCDRW |LCDEN;
while(LPC_GPIO0->FIOPIN & LCD_BUSY_FLAG); /* wait for busy flag to become low */

LCD_CTRL_CLR |= LCDEN | LCDRW;
LCD_DATA_DIR |= LCD_DATA_MASK;

delay(100);
}

/******************************************************************************************
Function Name : lcd_command_write()
Description :
Input :
Output : Void
Note :
****************************************************************************************/
#define DATA_POS 4
#define DELAY_COUNT 100
void lcd_command_write( unsigned char command )
{
unsigned char temp=0;
unsigned int temp1=0;

temp=command;
temp=(temp>>4)&0x0F;
temp1=(temp<<DATA_POS)&LCD_DATA_MASK;

LCD_CTRL_CLR = LCDRS;
LCD_CTRL_SET = LCDEN;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
delay(DELAY_COUNT);
LCD_CTRL_CLR = LCDEN;

temp=command;
temp&=0x0F;
temp1=(temp<<DATA_POS)&LCD_DATA_MASK;
delay(DELAY_COUNT);

LCD_CTRL_CLR |= LCDRS;
LCD_CTRL_SET |= LCDEN;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
delay(DELAY_COUNT);
LCD_CTRL_CLR |= LCDEN;
wait_lcd();
}

/*******************************************************************************************
Function Name : set_lcd_port_output()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
void set_lcd_port_output( void )
{
LCD_CTRL_DIR |= ( LCDEN | LCDRS | LCDRW | LCD_BACKLIGHT );
LCD_CTRL_CLR |= ( LCDEN | LCDRS | LCDRW | LCD_BACKLIGHT );
LCD_DATA_DIR |= LCD_DATA_MASK;
}

/*****************************************************************************************
Function Name : lcd_clear()
Description :
Input :
Output : Void
Note :
*****************************************************************************************/
void lcd_clear( void)
{
lcd_command_write( 0x01 );
}

/*******************************************************************************************
Function Name : lcd_gotoxy()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
int lcd_gotoxy( unsigned int x, unsigned int y)
{
int retval = 0;

if( (x > 1) && (y > 15) )
{
retval = -1;
} else {
if( x == 0 )
{
lcd_command_write( 0x80 + y ); /* command - position cursor at 0x00 (0x80 + 0x00 ) */
} else if( x==1 ){
lcd_command_write( 0xC0 + y ); /* command - position cursor at 0x40 (0x80 + 0x00 ) */
}
}
return retval;
}

/*******************************************************************************************
Function Name : lcd_data_write()
Description :
Input :
Output : Void
Note :
********************************************************************************************/
void lcd_data_write( unsigned char data )
{
unsigned char temp=0;
unsigned int temp1=0;

temp=data;
temp=(temp>>4)&0x0F;
temp1=(temp<<DATA_POS)&LCD_DATA_MASK;

LCD_CTRL_SET |= LCDEN|LCDRS;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
LCD_CTRL_CLR |= LCDEN;

temp=data;
temp&=0x0F;
temp1=(temp<<DATA_POS)&LCD_DATA_MASK;

LCD_CTRL_SET |= LCDEN|LCDRS;
LCD_DATA_CLR = LCD_DATA_MASK;
LCD_DATA_SET = temp1;
LCD_CTRL_CLR |= LCDEN;
wait_lcd();
}

/**********************************************************************************************
Function Name : lcd_putchar()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
void lcd_putchar( int c )
{
lcd_data_write( c );
}

/**********************************************************************************************
Function Name : lcd_putstring()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
void lcd_putstring( unsigned char line, char *string )
{
unsigned char len = MAX_CHAR_IN_ONE_LINE;

lcd_gotoxy( line, 0 );
while(*string != '\0' && len--)
{
lcd_putchar( *string );
string++;
}
}

/**********************************************************************************************
Function Name : lcd_backlight_on()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
void lcd_backlight_on()
{
LCD_BACK_LIGHT_DIR |= LCD_BACKLIGHT;
LCD_BACK_LIGHT_SET |= LCD_BACKLIGHT;
}

/*********************************************************************************************
Function Name : turn_off_lcd_back_light()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
void lcd_backlight_off(void)
{
LCD_BACK_LIGHT_DIR |= LCD_BACKLIGHT;
LCD_BACK_LIGHT_CLR |= LCD_BACKLIGHT;
}

/**********************************************************************************************
Function Name : init_lcd()
Description :
Input :
Output : Void
Note :
*********************************************************************************************/
void init_lcd( void )
{
set_lcd_port_output();
lcd_command_write(0x28); /* 4-bit interface, two line, 5X7 dots. */
// lcd_clear() ; /* LCD clear */
lcd_command_write(0x02); /* cursor home */
lcd_command_write(0x06); /* cursor move direction */
lcd_command_write(0x0C) ; /* display on */
lcd_gotoxy(0, 0);
lcd_clear();
}

Re: Character LCD 16x2 (HD44780) 20 Aug 2012 12:55 #232

  • cornelis
  • OFFLINE
  • Administrator
  • Posts: 125
Nice work!

Can you email me the project or upload it in the attachment?
Last Edit: 20 Aug 2012 12:57 by cornelis.

Re: Character LCD 16x2 (HD44780) 20 Aug 2012 15:54 #233

  • heitor
  • OFFLINE
  • Fresh Boarder
  • Posts: 6
I don't have you e-mail so i upload the project:
www.streamfile.com/myid/fltToUEdcfaH
Its a time limited storage service (24 hours).

Re: Character LCD 16x2 (HD44780) 20 Aug 2012 22:18 #235

  • rik
  • OFFLINE
  • Administrator
  • Posts: 105
Great, I uploaded it again so it won't disappear after 24 hours.
Thanks
Attachments:
  • Page:
  • 1
Time to create page: 0.75 seconds
Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics