📄 display.c
字号:
/************************************************************************************
* This is the source file for LCD Driver.
*
*
* (c) Copyright 2006, Freescale, Inc. All rights reserved.
*
* Freescale Confidential Proprietary
* Digianswer Confidential
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale.
************************************************************************************/
#include "EmbeddedTypes.h"
#include "Display.h"
#if (gLCDSupported_d == 1)
/******************************************************************************
*******************************************************************************
* Public Macros
*******************************************************************************
******************************************************************************/
/* None */
/******************************************************************************
*******************************************************************************
* Private prototypes
*******************************************************************************
******************************************************************************/
static void LCD_WriteChar( int8_t ch );
static void LCDWaitLong( int16_t w );
static void LCDWaitShort( int16_t w );
static void LCDLine( uint8_t line );
static void LCD_Write4bits( uint8_t bdata );
static char GetStrlen( const uint8_t *pString );
/******************************************************************************
*******************************************************************************
* Private type definitions
*******************************************************************************
******************************************************************************/
/* Wait long defines */
#define Wait5mSec 5
#define Wait3mSec 3
#define Wait2mSec 2
/* Wait short defines */
#define Wait75uSec 5
#define Wait60uSec 4
#define Wait30uSec 2
#define Wait15uSec 1
/* Line in display */
#define LineOne 0x80
#define LineTwo 0xC0
/******************************************************************************
*******************************************************************************
* Private Memory Declarations
*******************************************************************************
******************************************************************************/
/* None */
/******************************************************************************
*******************************************************************************
* Public functions
*******************************************************************************
******************************************************************************/
/******************************************************************************
* This function initialize the display
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCD_Init
(
void
)
{
LCDWaitLong( Wait5mSec );
/* This function setup Bits 6-7 as outputs (EN & RS) (PTEDD) */
Setup_EN_RS;
/* Setup the XX Port (4-7 data bits, 3 R/W ) (PTGDD)
data is output (default), r/w is output */
SetupDataBit;
/* Initialize data port */
InitDataPort;
/* Setup the R/W for writing (PTGD) */
Setup_R_W_Write;
/* Initialize EN and RS to 0 */
Init_EN_RS;
/* Send the reset sequence */
LCD_Write4bits( 0x30 );
LCDToggleEN;
LCDWaitLong( Wait5mSec );
LCD_Write4bits( 0x30 );
LCDToggleEN;
LCDWaitShort( Wait30uSec );
LCD_Write4bits( 0x30 );
LCDToggleEN;
LCDWaitShort( Wait30uSec );
LCD_Write4bits( 0x20 );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
/* Function, 4 bit data length */
LCD_Write4bits( 0x20 );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
/* 2 lines, 5x7 dot format */
LCD_Write4bits( 0x80 );
LCDToggleEN;
LCDWaitShort( Wait60uSec );
/* Entry Mode Inc, No Shift */
LCD_Write4bits( 0x00 );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
LCD_Write4bits( 0x60 );
LCDToggleEN;
LCDWaitShort( Wait75uSec );
/* Display ON/OFF Control - Display On, Cursor Off, Blink Off */
LCD_Write4bits( 0x00 );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
LCD_Write4bits( 0xC0 );
LCDToggleEN;
LCDWaitShort( Wait75uSec );
/* Display Clear */
LCD_ClearDisplay();
LCDLine(LineOne);
}
/******************************************************************************
* This function writes a string to the display
*
* Interface assumptions:
* The pstr must be zero-terminated.
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCD_WriteString
(
uint8_t line, /* IN: Line in display */
uint8_t *pstr /* Pointer to text string */
)
{
uint8_t len;
uint8_t x;
uint8_t *error = "Wrong line 1 & 2";
if( line == 2 ) {
LCDLine(LineTwo);
}
else if ( line == 1 ) {
LCDLine(LineOne);
}
else {
LCD_ClearDisplay();
LCDLine(LineOne);
pstr = error;
}
len = GetStrlen(pstr);
for ( x = 0; x < len; x++ ) {
LCD_WriteChar( pstr[x] );
}
/* Clear the rest of the line */
for ( ; x < gMAX_LCD_CHARS_c; x++ ) {
LCD_WriteChar( ' ' );
}
}
/******************************************************************************
* This function write a string and a value in decimal or hexdecimal
* to the display
*
* Interface assumptions:
* The pstr must be zero-terminated.
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCD_WriteStringValue
(
uint8_t *pstr, /* IN: Pointer to text string */
uint16_t value, /* IN: Value */
uint8_t line, /* IN: Line in display */
LCD_t numberFormat /* IN: Value to show in HEX or DEC */
)
{
int16_t divDec=10000, divHex=16;
uint8_t loop=5, i=0,counter=0, aH[6], aHex[6];
uint8_t aDec[6], aString[17], *phexValue="0123456789ABCDEF";
if(numberFormat == gLCD_DecFormat_c) {
if(value < 100) {
loop = 2;
divDec = 10;
}
else if(value >= 100 && value <1000) {
loop = 3;
divDec = 100;
}
else if(value >= 1000 && value <9999) {
loop = 4;
divDec = 1000;
}
for(i=0; i<loop; i++) {
if((value/divDec)!= 0) {
aDec[counter++] = (value/divDec) + 0x30;
value = value % divDec;
}
else {
aDec[counter++] = 0x30;
}
divDec = divDec/10;
}
aDec[counter]='\0';
counter=0;
while (*pstr != '\0' && counter <gMAX_LCD_CHARS_c ) {
aString[counter++]=*pstr;
pstr++;
}
i=0;
while (aDec[i] != '\0' && counter <gMAX_LCD_CHARS_c ) {
aString[counter++]=aDec[i++];
}
aString[counter]='\0';
LCD_WriteString( line, aString );
}
else if(numberFormat == gLCD_HexFormat_c) {
do{
aH[i]=phexValue[value % divHex];
value=value / divHex;
i++;
}
while(value > 15);
aH[i]=phexValue[value];
counter=0;
while(i > 0){
aHex[counter++]=aH[i--];
}
aHex[counter++]=aH[0];
aHex[counter]='\0';
counter=0;
while (*pstr != '\0' && counter <gMAX_LCD_CHARS_c ) {
aString[counter++]=*pstr;
pstr++;
}
i=0;
while (aHex[i] != '\0' && counter <gMAX_LCD_CHARS_c ) {
aString[counter++]=aHex[i++];
}
aString[counter]='\0';
LCD_WriteString( line, aString );
}
else {
LCD_WriteString( line, "Format unknow" );
}
}
/******************************************************************************
*******************************************************************************
* Private functions
*******************************************************************************
******************************************************************************/
/******************************************************************************
* This function writes a byte to the display
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCD_WriteChar
(
int8_t ch /* IN: Char */
)
{
SetupLCDClearBit;
LCD_Write4bits( ch );
LCDToggleEN;
LCD_Write4bits( ch << 4 );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
}
/******************************************************************************
* This function makes a long delay before next instruction to the display
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCDWaitLong
(
int16_t w /* IN: Value to wait */
)
{
int16_t x;
int16_t y;
for ( y = 0; y < w; y++ ) {
/* 1ms wait routine ((1/CLK) * W * 2047 = X s) */
for ( x = 0; x < 0x7FF; x++ );
}
}
/******************************************************************************
* This function makes a short delay before next instruction to the display
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCDWaitShort
(
int16_t w /* IN: Value to wait */
)
{
int16_t x;
int16_t y;
for ( y = 0; y < w; y++ ) {
for ( x = 0; x < 0x001F; x++ );
}
}
/******************************************************************************
* This function writes in the first line in the display
*
* Interface assumptions:
* Interface assumptions:
* line: 0x80 = line 1, 0xC0 = line 2
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCDLine
(
uint8_t line /* IN: Line 1 = 0x80, Line 2 = 0xC0 */
)
{
Setup_R_W_Write;
/* DD Ram Address Set - 1st Digit */
LCD_Write4bits( line );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
LCD_Write4bits( 0x00 );
LCDToggleEN;
LCDWaitShort( Wait60uSec );
}
/******************************************************************************
* This function clear line 1 & 2 in display
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCD_ClearDisplay
(
void
)
{
Setup_R_W_Write
LCD_Write4bits( 0x00 );
LCDToggleEN;
LCDWaitShort( Wait15uSec );
LCD_Write4bits( 0x10 );
LCDToggleEN;
LCDWaitLong( Wait3mSec );
}
/******************************************************************************
* This function write 4 Bits to display
*
* Interface assumptions:
*
*
* Return value:
* None
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
void LCD_Write4bits
(
uint8_t bdata /* IN: Data to write in display */
)
{
if ( bdata & 0x80 ) {
SetLCDDataBit3;
}
else {
ClearLCDDataBit3;
}
if ( bdata & 0x40 ) {
SetLCDDataBit2;
}
else {
ClearLCDDataBit2;
}
if ( bdata & 0x20 ) {
SetLCDDataBit1;
}
else {
ClearLCDDataBit1;
}
if ( bdata & 0x10 ) {
SetLCDDataBit0;
}
else {
ClearLCDDataBit0;
}
}
/******************************************************************************
* This function gets the lenght of a string and return the lenght
*
* Interface assumptions:
*
*
* Return value:
* char
*
*
* Revison history:
* date Author Comments
* ------ ------ --------
* 16.01.06 MMA Created
******************************************************************************/
char GetStrlen
(
const uint8_t *pString /* IN: Pointer to text string */
)
{
int8_t count=0, lenght=0;
while (*pString != '\0' && count <gMAX_LCD_CHARS_c ) {
count++;
lenght++;
pString++;
}
/* Check boundries */
if ( lenght > gMAX_LCD_CHARS_c ) {
lenght = gMAX_LCD_CHARS_c;
}
return lenght;
}
/******************************************************************************
*******************************************************************************
* Private Debug stuff
*******************************************************************************
******************************************************************************/
/* None */
#endif /* gLCDSupported_d */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -