drivers/device/LCD.c

Fri, 13 Aug 2010 10:41:29 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Fri, 13 Aug 2010 10:41:29 +0100
changeset 0
267b5a25932f
permissions
-rw-r--r--

Initial commit, GPIO v3.1

     1 /****************************************************************************
     2 **
     3 **  Name: LCD.c
     4 **
     5 **  Description:
     6 **       Implements functions for manipulating a 
     7 **       dot-matrix LCD
     8 **
     9 **  $Revision: $
    10 **
    11 ** Disclaimer:
    12 **
    13 **   This source code is intended as a design reference which
    14 **   illustrates how these types of functions can be implemented.  It
    15 **   is the user's responsibility to verify their design for
    16 **   consistency and functionality through the use of formal
    17 **   verification methods.  Lattice Semiconductor provides no warranty
    18 **   regarding the use or functionality of this code.
    19 **
    20 ** --------------------------------------------------------------------
    21 **
    22 **                     Lattice Semiconductor Corporation
    23 **                     5555 NE Moore Court
    24 **                     Hillsboro, OR 97214
    25 **                     U.S.A
    26 **
    27 **                     TEL: 1-800-Lattice (USA and Canada)
    28 **                          (503)268-8001 (other locations)
    29 **
    30 **                     web:   http://www.latticesemi.com
    31 **                     email: techsupport@latticesemi.com
    32 **
    33 ** --------------------------------------------------------------------------
    34 **
    35 **  Change History (Latest changes on top)
    36 **
    37 **  Ver    Date        Description
    38 ** --------------------------------------------------------------------------
    39 **
    40 **  3.0   Mar-25-2008  Added Header
    41 **
    42 **---------------------------------------------------------------------------
    43 *****************************************************************************/
    45 #include "LCD.h"
    46 #include "MicoUtils.h"
    48 void LCD_WriteData(volatile unsigned int *pAddress, unsigned int data)
    49 {
    50 	unsigned int iData = data;
    52 	/* first output the data to write */
    53 	*pAddress = iData;
    55 	/* strobe enable while maintaining data */
    56 	iData |= 0x400;
    57 	*pAddress = iData;
    59 	/* remove strobe while maintaining data */
    60 	iData &= ~0x400;
    61 	*pAddress = iData;
    63 	/* all done */
    64 	return;
    65 }
    68 void LCD_DisplayOnOff(MicoGPIOCtx_t *ctx, unsigned int bOn)
    69 {
    70 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
    71 	unsigned int value;
    73 	if(bOn == 0)	/* turn display off */
    74 		value = 0x8;
    75 	else			/* turn display on */
    76 		value = 0xc;
    78 	LCD_WriteData(pAddress, value);
    79 	MicoSleepMilliSecs(10);
    80 }
    83 void LCD_CursorOnOff(MicoGPIOCtx_t *ctx, unsigned int bOn)
    84 {
    85 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
    86 	/* cursor on-off control is valid only if the display is turned on */
    87 	unsigned int iValue;
    89 	if(bOn == 0)
    90 		iValue = 0x0c;
    91 	else
    92 		iValue = 0x0e;
    94 	LCD_WriteData(pAddress, iValue);
    95 	MicoSleepMilliSecs(10);
    97 }
   100 void LCD_BlinkOnOff(MicoGPIOCtx_t *ctx, unsigned int bOn)
   101 {
   102 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   103 	/* character-blink on/off is valid only if the display is turned on */
   104 	unsigned int iValue;
   106 	if(bOn == 0)
   107 		iValue = 0xc;
   108 	else
   109 		iValue = 0xd;
   111 	LCD_WriteData(pAddress, iValue);
   112 	MicoSleepMilliSecs(10);
   113 }
   116 /* clears LCD display */
   117 void LCD_ClearDisplay(MicoGPIOCtx_t *ctx)
   118 {
   119 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   120 	LCD_WriteData(pAddress, 0x1);
   121 	MicoSleepMilliSecs(10);
   122 }
   125 /* sets LCD function (#lines for the display, with 8-bit interface */
   126 void LCD_SetFunction(MicoGPIOCtx_t *ctx, unsigned int iNumLines)
   127 {
   128 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   129 	unsigned int value;
   131 	/* always sets an 8-bit interface */
   132 	if(iNumLines == 2)
   133 		value = 0x38;
   134 	else
   135 		value = 0x00;
   137 	LCD_WriteData(pAddress, value);
   138 	MicoSleepMilliSecs(10);
   139 }
   141 /* sets cursor-move mode */
   142 void LCD_SetCursorMoveMode(MicoGPIOCtx_t *ctx, unsigned int bIncrement)
   143 {
   144 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   145 	unsigned int value;
   146 	if(bIncrement != 0)
   147 		value = 0x6;
   148 	else
   149 		value = 0x4;
   150 	LCD_WriteData(pAddress, value);
   151 	MicoSleepMilliSecs(10);
   152 }
   155 /* shifts display to left (!= 0) or right (== 0) */
   156 void LCD_ShiftDisplay(MicoGPIOCtx_t *ctx, unsigned int bLeft)
   157 {
   158 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   159 	unsigned int value;
   160 	if(bLeft == 0)	/* shift to right */
   161 		value = 0x18;
   162 	else 			/* shift to left */
   163 		value = 0x1c;
   164 	LCD_WriteData(pAddress, value);
   165 	MicoSleepMilliSecs(10);
   166 }
   169 /* shifts cursor to left (!= 0) or right (== 0) */
   170 void LCD_ShiftCursor(MicoGPIOCtx_t *ctx, unsigned int bLeft)
   171 {
   172 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   173 	unsigned int value;
   174 	if(bLeft == 0)	/* shift to right */
   175 		value = 0x10;
   176 	else			/* shift to left */
   177 		value = 0x14;
   178 	LCD_WriteData(pAddress, value);
   179 	MicoSleepMilliSecs(10);
   180 }
   183 void LCD_WriteChar(MicoGPIOCtx_t *ctx, unsigned char character)
   184 {
   185 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   186 	unsigned int value = (unsigned int)character;
   187 	value |= 0x200;
   188 	LCD_WriteData(pAddress, value);
   189 	MicoSleepMilliSecs(10);
   190 }
   193 void LCD_SetCursorPos(MicoGPIOCtx_t *ctx, unsigned int iLine, unsigned int iCol)
   194 {
   195 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   196 	unsigned int value;
   197 	if(iLine == 0)	/* first line */
   198 		value = 0x80;
   199 	else			/* second line */
   200 		value = 0xc0;
   202 	value += iCol;
   204 	LCD_WriteData(pAddress, value);
   205 	MicoSleepMilliSecs(10);
   206 }
   209 void LCD_Init(MicoGPIOCtx_t *ctx, unsigned int iLines)
   210 {
   211 	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   212 	/* - wait for power-stabilization */
   213 	MicoSleepMilliSecs(40);
   215 	*pAddress = 0x0;
   217 	/* - set data-width (8 bits) and lines in display (2) */
   218 	LCD_SetFunction(ctx, iLines);
   219 	LCD_SetFunction(ctx, iLines);
   220 	LCD_SetFunction(ctx, iLines);
   223 	/* turn off the display */
   224 	LCD_DisplayOnOff(ctx, 0);
   226 	/* - clear display */
   227 	LCD_ClearDisplay(ctx);
   229 	/* turn-on the display */
   230 	LCD_DisplayOnOff(ctx, 1);
   232 	/* turn on the blinking of the cursor-position */
   233 	LCD_BlinkOnOff(ctx, 0);
   235 	/* set cursor move-mode to increment */
   236 	LCD_SetCursorMoveMode(ctx, 1);
   238 	/* all done */
   239 	return;
   240 }