📄 lcd.c
字号:
/***********************************************************************************
FILE NAME : lcd.c
DESCRIPTION : LCD Module utility functions.
Written for KS0066u compatible LCD Module.
(8 characters by 2 lines)
Copyright : 2006 Renesas Technology Europe Ltd.
Copyright : 2006 Renesas Technology Corporation.
All Rights Reserved
***********************************************************************************/
/***********************************************************************************
Revision History
DD.MM.YYYY OSO-UID Description
24.05.2006 RTE-VNA First Release
***********************************************************************************/
/**********************************************************************************
System Includes
***********************************************************************************/
/* Follwoing header file provides access to intrinsic functions to control interrupt
levels. */
#include <machine.h>
/**********************************************************************************
User Includes
***********************************************************************************/
/* Follwoing header file provides a structure to access all of the device registers. */
#include "iodefine.h"
/* Follwoing header file provides common defines for widely used items. */
#include "rsk38347def.h"
/* Follwoing header file provides prototype for lcd controlling function. */
#include "lcd.h"
/**********************************************************************************
User Program Code
***********************************************************************************/
/***********************************************************************************
Function Name : InitialiseDisplay
Description : Intializes the LCD display.
Parameters : none
Return Value : none
***********************************************************************************/
void InitialiseDisplay(void)
{
/* Power Up Delay for LCD Module */
EN_PIN = SET_BIT_HIGH;
DisplayDelay(70);
EN_PIN = SET_BIT_LOW;
/* Display initialises in 8 bit mode - so send one write (seen as 8 bit)
to set to 4 bit mode. */
/* Function Set */
LCD_nibble_write(CTRL_WR,0x03);
LCD_nibble_write(CTRL_WR,0x03);
DisplayDelay(39);
/* Configure display */
LCD_nibble_write(CTRL_WR,0x03);
LCD_nibble_write(CTRL_WR,0x02);
LCD_nibble_write(CTRL_WR,(LCD_DISPLAY_ON | LCD_TWO_LINE ));
LCD_nibble_write(CTRL_WR,(LCD_DISPLAY_ON | LCD_TWO_LINE ));
DisplayDelay(39);
/* Display ON/OFF control */
LCD_write(CTRL_WR,LCD_CURSOR_OFF);
DisplayDelay(39);
/* Display Clear */
LCD_write(CTRL_WR,LCD_CLEAR);
DisplayDelay(1530);
/* Entry Mode Set */
LCD_write(CTRL_WR,0x06);
LCD_write(CTRL_WR,LCD_HOME_L1);
}
/***********************************************************************************
End of function InitialiseDisplay
***********************************************************************************/
/***********************************************************************************
Function Name : DisplayString
Description : This function controls LCD writes to line 1 or 2 of the LCD.
You need to use the defines LCD_LINE1 and LCD_LINE2 in order
to specify the starting position.
For example, to start at the 2nd position on line 1...
DisplayString(LCD_LINE1 + 1, "Hello")
Parameters : position Line number of display
string Pointer to data to be written to display.
Last character should be null.
Return Value : none
***********************************************************************************/
void DisplayString(unsigned char position, char * string)
{
static unsigned char next_pos = 0xFF;
/* Set line position if needed. We don't want to if we don't need
to because LCD control operations take longer than LCD data
operations. */
if( next_pos != position)
{
if(position < LCD_LINE2)
{
/* Display on Line 1 */
LCD_write(CTRL_WR, (unsigned char)(LCD_HOME_L1 + position) );
}
else
{
/* Display on Line 2 */
LCD_write(CTRL_WR, (unsigned char)(LCD_HOME_L2 + position - LCD_LINE2) );
}
/* set position index to known value */
next_pos = position;
}
do
{
LCD_write(DATA_WR,(unsigned char)*string++);
/* increment position index */
next_pos++;
}
while(*string);
}
/***********************************************************************************
End of function DisplayString
***********************************************************************************/
/***********************************************************************************
Function Name : LCD_write
Description : Writes data to display. Sends command to display.
Parameters : value - the value to write
data_or_ctrl - To write value as DATA or CONTROL
1 = DATA
0 = CONTROL
Return Value : none
***********************************************************************************/
void LCD_write(unsigned char data_or_ctrl, unsigned char value)
{
/* Write upper nibble first */
LCD_nibble_write(data_or_ctrl, (unsigned char)((value & 0xF0) >> 4));
/* Write lower nibble second */
LCD_nibble_write(data_or_ctrl, (unsigned char)(value & 0x0F));
}
/***********************************************************************************
End of function LCD_write
***********************************************************************************/
/***********************************************************************************
Function Name : LCD_nibble_write
Description : Writes data to display. Sends command to display.
Parameters : value - the value to write
data_or_ctrl - To write value as DATA or CONTROL
1 = DATA
0 = CONTROL
Return Value : none
***********************************************************************************/
void LCD_nibble_write(unsigned char data_or_ctrl, unsigned char value)
{
unsigned char ucStore;
/* Set Register Select pin high for Data */
if (data_or_ctrl == DATA_WR)
{
RS_PIN = SET_BIT_HIGH;
}
else
{
RS_PIN = SET_BIT_LOW;
}
/* There must be 40ns between RS write and EN write */
DisplayDelay(1);
/* EN enable chip (HIGH) */
EN_PIN = SET_BIT_HIGH;
/* Tiny delay */
DisplayDelay(1);
/* Clear port bits used */
ucStore = DATA_PORT;
ucStore &= (unsigned char) ~DATA_PORT_MASK;
/* OR in data */
ucStore |= (unsigned char)((value << DATA_PORT_SHIFT) & DATA_PORT_MASK );
/* Write data to port */
DATA_PORT = ucStore;
/* write delay while En High */
DisplayDelay(20);
/* Latch data by dropping EN */
EN_PIN = SET_BIT_LOW;
/* Data hold delay */
DisplayDelay(20);
if (data_or_ctrl == CTRL_WR)
{
/* Extra delay needed for control writes */
DisplayDelay(0x0001);
}
}
/***********************************************************************************
End of function LCD_nibble_write
***********************************************************************************/
/***********************************************************************************
Function Name : DisplayDelay
Description : Delay routine for LCD display.
Parameters : units - Approximately in microseconds
Return Value : none
***********************************************************************************/
void DisplayDelay(unsigned long int units)
{
unsigned long counter = units * DELAY_TIMING;
/* Delay. */
while(counter--)
{
nop();
}
}
/***********************************************************************************
End of function DisplayDelay
***********************************************************************************/
/***********************************************************************************
Function Name : Convert_Number_ToString
Description : Converts the ADC result to string format.
Parameters : ADC result of unsigned integer type and character buffer of 8 bytes.
Return Value : none
***********************************************************************************/
void Convert_Number_ToString(unsigned short c,char* buffer)
{
unsigned int a;
buffer[0] = '=';
buffer[1] = 'H';
buffer[2] = '\'';
a = (char)((c & 0x0F00)>> 8);
buffer[3] = (char)((a < 0x0A) ? (a+0x30):(a+0x37));
a = (char)((c & 0x00F0)>> 4);
buffer[4] = (char)((a < 0x0A) ? (a+0x30):(a+0x37));
a = (char)(c & 0x000F);
buffer[5] = (char)((a < 0x0A) ? (a+0x30):(a+0x37));
/* Add Spaces and a string terminating character. */
buffer[6] = ' ';
buffer[7] = ' ';
buffer[8] = '\0';
}
/***********************************************************************************
End of function Convert_Number_ToString
***********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -