drivers/device/LCD.c

changeset 0
267b5a25932f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/device/LCD.c	Fri Aug 13 10:41:29 2010 +0100
     1.3 @@ -0,0 +1,241 @@
     1.4 +/****************************************************************************
     1.5 +**
     1.6 +**  Name: LCD.c
     1.7 +**
     1.8 +**  Description:
     1.9 +**       Implements functions for manipulating a 
    1.10 +**       dot-matrix LCD
    1.11 +**
    1.12 +**  $Revision: $
    1.13 +**
    1.14 +** Disclaimer:
    1.15 +**
    1.16 +**   This source code is intended as a design reference which
    1.17 +**   illustrates how these types of functions can be implemented.  It
    1.18 +**   is the user's responsibility to verify their design for
    1.19 +**   consistency and functionality through the use of formal
    1.20 +**   verification methods.  Lattice Semiconductor provides no warranty
    1.21 +**   regarding the use or functionality of this code.
    1.22 +**
    1.23 +** --------------------------------------------------------------------
    1.24 +**
    1.25 +**                     Lattice Semiconductor Corporation
    1.26 +**                     5555 NE Moore Court
    1.27 +**                     Hillsboro, OR 97214
    1.28 +**                     U.S.A
    1.29 +**
    1.30 +**                     TEL: 1-800-Lattice (USA and Canada)
    1.31 +**                          (503)268-8001 (other locations)
    1.32 +**
    1.33 +**                     web:   http://www.latticesemi.com
    1.34 +**                     email: techsupport@latticesemi.com
    1.35 +**
    1.36 +** --------------------------------------------------------------------------
    1.37 +**
    1.38 +**  Change History (Latest changes on top)
    1.39 +**
    1.40 +**  Ver    Date        Description
    1.41 +** --------------------------------------------------------------------------
    1.42 +**
    1.43 +**  3.0   Mar-25-2008  Added Header
    1.44 +**
    1.45 +**---------------------------------------------------------------------------
    1.46 +*****************************************************************************/
    1.47 +
    1.48 +#include "LCD.h"
    1.49 +#include "MicoUtils.h"
    1.50 +
    1.51 +void LCD_WriteData(volatile unsigned int *pAddress, unsigned int data)
    1.52 +{
    1.53 +	unsigned int iData = data;
    1.54 +
    1.55 +	/* first output the data to write */
    1.56 +	*pAddress = iData;
    1.57 +
    1.58 +	/* strobe enable while maintaining data */
    1.59 +	iData |= 0x400;
    1.60 +	*pAddress = iData;
    1.61 +	
    1.62 +	/* remove strobe while maintaining data */
    1.63 +	iData &= ~0x400;
    1.64 +	*pAddress = iData;
    1.65 +
    1.66 +	/* all done */
    1.67 +	return;
    1.68 +}
    1.69 +
    1.70 +
    1.71 +void LCD_DisplayOnOff(MicoGPIOCtx_t *ctx, unsigned int bOn)
    1.72 +{
    1.73 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
    1.74 +	unsigned int value;
    1.75 +
    1.76 +	if(bOn == 0)	/* turn display off */
    1.77 +		value = 0x8;
    1.78 +	else			/* turn display on */
    1.79 +		value = 0xc;
    1.80 +
    1.81 +	LCD_WriteData(pAddress, value);
    1.82 +	MicoSleepMilliSecs(10);
    1.83 +}
    1.84 +
    1.85 +
    1.86 +void LCD_CursorOnOff(MicoGPIOCtx_t *ctx, unsigned int bOn)
    1.87 +{
    1.88 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
    1.89 +	/* cursor on-off control is valid only if the display is turned on */
    1.90 +	unsigned int iValue;
    1.91 +	
    1.92 +	if(bOn == 0)
    1.93 +		iValue = 0x0c;
    1.94 +	else
    1.95 +		iValue = 0x0e;
    1.96 +
    1.97 +	LCD_WriteData(pAddress, iValue);
    1.98 +	MicoSleepMilliSecs(10);
    1.99 +
   1.100 +}
   1.101 +
   1.102 +
   1.103 +void LCD_BlinkOnOff(MicoGPIOCtx_t *ctx, unsigned int bOn)
   1.104 +{
   1.105 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.106 +	/* character-blink on/off is valid only if the display is turned on */
   1.107 +	unsigned int iValue;
   1.108 +
   1.109 +	if(bOn == 0)
   1.110 +		iValue = 0xc;
   1.111 +	else
   1.112 +		iValue = 0xd;
   1.113 +
   1.114 +	LCD_WriteData(pAddress, iValue);
   1.115 +	MicoSleepMilliSecs(10);
   1.116 +}
   1.117 +
   1.118 +
   1.119 +/* clears LCD display */
   1.120 +void LCD_ClearDisplay(MicoGPIOCtx_t *ctx)
   1.121 +{
   1.122 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.123 +	LCD_WriteData(pAddress, 0x1);
   1.124 +	MicoSleepMilliSecs(10);
   1.125 +}
   1.126 +
   1.127 +
   1.128 +/* sets LCD function (#lines for the display, with 8-bit interface */
   1.129 +void LCD_SetFunction(MicoGPIOCtx_t *ctx, unsigned int iNumLines)
   1.130 +{
   1.131 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.132 +	unsigned int value;
   1.133 +
   1.134 +	/* always sets an 8-bit interface */
   1.135 +	if(iNumLines == 2)
   1.136 +		value = 0x38;
   1.137 +	else
   1.138 +		value = 0x00;
   1.139 +
   1.140 +	LCD_WriteData(pAddress, value);
   1.141 +	MicoSleepMilliSecs(10);
   1.142 +}
   1.143 +
   1.144 +/* sets cursor-move mode */
   1.145 +void LCD_SetCursorMoveMode(MicoGPIOCtx_t *ctx, unsigned int bIncrement)
   1.146 +{
   1.147 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.148 +	unsigned int value;
   1.149 +	if(bIncrement != 0)
   1.150 +		value = 0x6;
   1.151 +	else
   1.152 +		value = 0x4;
   1.153 +	LCD_WriteData(pAddress, value);
   1.154 +	MicoSleepMilliSecs(10);
   1.155 +}
   1.156 +
   1.157 +
   1.158 +/* shifts display to left (!= 0) or right (== 0) */
   1.159 +void LCD_ShiftDisplay(MicoGPIOCtx_t *ctx, unsigned int bLeft)
   1.160 +{
   1.161 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.162 +	unsigned int value;
   1.163 +	if(bLeft == 0)	/* shift to right */
   1.164 +		value = 0x18;
   1.165 +	else 			/* shift to left */
   1.166 +		value = 0x1c;
   1.167 +	LCD_WriteData(pAddress, value);
   1.168 +	MicoSleepMilliSecs(10);
   1.169 +}
   1.170 +
   1.171 +
   1.172 +/* shifts cursor to left (!= 0) or right (== 0) */
   1.173 +void LCD_ShiftCursor(MicoGPIOCtx_t *ctx, unsigned int bLeft)
   1.174 +{
   1.175 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.176 +	unsigned int value;
   1.177 +	if(bLeft == 0)	/* shift to right */
   1.178 +		value = 0x10;
   1.179 +	else			/* shift to left */
   1.180 +		value = 0x14;
   1.181 +	LCD_WriteData(pAddress, value);
   1.182 +	MicoSleepMilliSecs(10);
   1.183 +}
   1.184 +
   1.185 +
   1.186 +void LCD_WriteChar(MicoGPIOCtx_t *ctx, unsigned char character)
   1.187 +{
   1.188 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.189 +	unsigned int value = (unsigned int)character;
   1.190 +	value |= 0x200;
   1.191 +	LCD_WriteData(pAddress, value);
   1.192 +	MicoSleepMilliSecs(10);
   1.193 +}
   1.194 +
   1.195 +
   1.196 +void LCD_SetCursorPos(MicoGPIOCtx_t *ctx, unsigned int iLine, unsigned int iCol)
   1.197 +{
   1.198 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.199 +	unsigned int value;
   1.200 +	if(iLine == 0)	/* first line */
   1.201 +		value = 0x80;
   1.202 +	else			/* second line */
   1.203 +		value = 0xc0;
   1.204 +
   1.205 +	value += iCol;
   1.206 +
   1.207 +	LCD_WriteData(pAddress, value);
   1.208 +	MicoSleepMilliSecs(10);
   1.209 +}
   1.210 +
   1.211 +
   1.212 +void LCD_Init(MicoGPIOCtx_t *ctx, unsigned int iLines)
   1.213 +{
   1.214 +	volatile unsigned int *pAddress = (volatile unsigned int *)(ctx->base);
   1.215 +	/* - wait for power-stabilization */
   1.216 +	MicoSleepMilliSecs(40);
   1.217 +
   1.218 +	*pAddress = 0x0;
   1.219 +
   1.220 +	/* - set data-width (8 bits) and lines in display (2) */
   1.221 +	LCD_SetFunction(ctx, iLines);
   1.222 +	LCD_SetFunction(ctx, iLines);
   1.223 +	LCD_SetFunction(ctx, iLines);
   1.224 +
   1.225 +
   1.226 +	/* turn off the display */
   1.227 +	LCD_DisplayOnOff(ctx, 0);
   1.228 +
   1.229 +	/* - clear display */
   1.230 +	LCD_ClearDisplay(ctx);
   1.231 +			
   1.232 +	/* turn-on the display */
   1.233 +	LCD_DisplayOnOff(ctx, 1);
   1.234 +
   1.235 +	/* turn on the blinking of the cursor-position */
   1.236 +	LCD_BlinkOnOff(ctx, 0);
   1.237 +
   1.238 +	/* set cursor move-mode to increment */
   1.239 +	LCD_SetCursorMoveMode(ctx, 1);
   1.240 +
   1.241 +	/* all done */
   1.242 +	return;
   1.243 +}
   1.244 +