8051 code library
example library using MicroC
/***************************************************************************
;
; This module is presented here only to serve as a sample for
; 8051 program development. BiPOM Electronics provides this module as is and
; does not guarantee its functionality or suitability for a particular purpose.
; Please e-mail any questions and suggestions to
info@bipom.com;
;
; Module: LCD.C
;
; Revision: 1.01
;
; Description: Interface routines for driving a alphanumeric
; Liquid Crystal Display module using 8051 micro-controller.
; LCD is driven by only 4 data lines ( 4-bit mode )
;
; This program is written using Micro-C Compiler from
; Dunfield Development Systems.
;
*/
#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>
#define READ P3.5 /*READ=SET & WRITE=RESET*/
#define STROBE P3.4 /*DATA STROBE (ENABLE)*/
#define CTRL P3.7 /*CONTROL BIT (CONTROL=RESET & DATA=SET)*/
void InitLCD();
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void WriteLCD( unsigned char* message );
void CursorHome();
void CursorSet( unsigned char pos );
main()
{
/* Initialize the LCD as the very first thing */
InitLCD();
/* Write a simple message to the LCD */
CursorHome();
WriteLCD( "Hello" );
/* Keep the message for 2 seconds ( 1 second on MINI-MAX/51-C ) */
delay(2000);
/* Clear the LC display */
CursorHome();
WriteLCD( " " );
/* Example of writing to anywhere on the display */
CursorSet(5);
WriteData( 'x' );
WriteData( 'y' );
WriteData( 'z' );
}
/***************************************************************************
; Function: InitLCD
;
; Description: Initializes the LCD
;
; Inputs: none
;
; Returns: nothing
;**************************************************************************/
void InitLCD()
{
/* Wait a bit after power-up */
delay(200);
/* Initialize the LCD to 4-bit mode */
WriteCtrl(3);
delay(50);
WriteCtrl(3);
delay(10);
WriteCtrl(3);
delay(10);
WriteCtrl(2);
delay(10);
/* Function Set */
WriteCtrl(2);
delay(10);
WriteCtrl(8);
delay(10);
/* Display OFF */
WriteCtrl(0);
delay(10);
WriteCtrl(8);
delay(10);
/* Display ON */
WriteCtrl(0);
delay(10);
WriteCtrl(0x0F);
delay(10);
/* Entry mode */
WriteCtrl(0);
delay(10);
WriteCtrl(6);
delay(10);
/* Clear Screen */
WriteCtrl(0);
delay(10);
WriteCtrl(1);
delay(100);
/* Cursor home */
WriteCtrl(0);
delay(10);
WriteCtrl(2);
delay(100);
}
void CursorSet( unsigned char pos )
{
WriteCtrl(8);
delay(1);
WriteCtrl(pos);
delay(1);
}
void CursorHome()
{
/* Cursor home */
WriteCtrl(0);
delay(1);
WriteCtrl(2);
delay(1);
}
void WriteLCD( unsigned char* message )
{
unsigned char i;
for( i=0; i<20; i++ )
{
if( !message
)
break;
WriteData(message);
}
}
void WriteCtrl( unsigned char value )
{
clrbit(CTRL);
WriteCommon( value );
}
void WriteData( unsigned char value )
{
setbit(CTRL);
WriteCommon( value >> 4 );
WriteCommon( value );
}
void WriteCommon( unsigned char value )
{
clrbit(READ);
value = value & 0x0F;
value = value << 2;
value |= 0xC3;
P1 = value;
setbit(STROBE);
delay(1);
clrbit(STROBE);
setbit(READ);
delay(1);
}
regards