drivers/device/LCD.c

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