📄 lcd.c
字号:
/******************************************************************************/
/* Renesas Technology America, Inc. Legal Disclaimer */
/******************************************************************************/
/* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY */
/* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR */
/* PURPOSE. */
/* */
/* BY USING THE SOFTWARE HEREIN, YOU (AND YOUR COMPANY) AGREE TO BE BOUND BY */
/* THE TERMS AND CONDITIONS OF RENESAS TECHNOLOGY AMERICA, INC.'S SOURCE CODE */
/* LICENSE AGREEMENT. PLEASE READ THIS AGREEMENT CAREFULLY. IF YOU (OR YOUR */
/* COMPANY) DO NOT AGREE TO ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT, */
/* DO NOT INSTALL OR USE THIS SOFTWARE AND ASSOCIATED DOCUMENTATION. */
/* */
/* Copyright (c) 2003, 2004, 2005 and 2006 Renesas Technology America, Inc. */
/**********************************************************************************
FILE NAME LCD.C
DESCRIPTION CAN Demonstration and Development Kit source file.
Driver for ACM0802C LCD Module (8 characters by 2 lines )
This taken from
Copyright : 2006 Renesas Technology Europe Ltd.
Copyright : 2006 Renesas Technology Corporation.
All Rights Reserved
***********************************************************************************
tab space = 4
***********************************************************************************
Revision history:
V1.0 RSK Can D-Kit Release, Feb 14, 2006.
V1.0 RSK Can D-Kit A-D, Apr 13, 2006.V1.0 RSK Can D-Kit A-D, Apr 13, 2006.
**********************************************************************************/
/**********************************************************************************
System Includes
***********************************************************************************/
/**********************************************************************************
User Includes
***********************************************************************************/
#include "lcd.h"
#include "sfr_r823.h"
/**********************************************************************************
User Program Code
***********************************************************************************/
/*****************************************************************************
Name: InitDisplay
Parameters: none
Returns: none
Description: Intializes the LCD display.
*****************************************************************************/
void InitDisplay(void){
/* Initial port directions */
prc2=1; /* unprotect as Port 9 is used */
PORT_DDR = PORT_DDR_VALUE;
EN_PIN = HIGH;
EN_PIN_DDR = HIGH; /* set port that controls EN as output */
RS_PIN = HIGH;
RS_PIN_DDR = HIGH; /* set port that controls RS as output */
EN_PIN = LOW;
LCD_write(CTRL_WR,0x33);
DisplayDelay(20);
LCD_write(CTRL_WR,0x32);
DisplayDelay(20);
LCD_write(CTRL_WR,FUNCTION_SET); /* reset sequence */
LCD_write(CTRL_WR,FUNCTION_SET);
LCD_write(CTRL_WR,LCD_CURSOR_OFF);
LCD_write(CTRL_WR,LCD_CLEAR);
LCD_write(CTRL_WR,LCD_HOME_L1);
}
/*****************************************************************************
Name: DisplayString
Parameters: position Line number of display
string Pointer to data to be written to display.
Last character should be null.
Returns: none
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")
*****************************************************************************/
void DisplayString(unsigned char position, const char far * 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 < LINE2_1STCH)
{
/* 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 - LINE2_1STCH) );
}
next_pos = position; /* set position index to known value */
}
do
{
LCD_write(DATA_WR,*string++);
next_pos++; /* increment position index */
}
while(*string);
}
/*****************************************************************************
Name: LCD_write
Parameters: value - the value to write
data_or_ctrl - To write value as DATA or CONTROL
1 = DATA
0 = CONTROL
Returns: none
Description: Writes data to display. Sends command to display.
*****************************************************************************/
void LCD_write(unsigned char data_or_ctrl, unsigned char value){
RS_PIN = data_or_ctrl; /* RS SELECT (HIGH=DATA, LOW=CTRL */
/* Write upper nibble first */
DATA_PORT &= 0xF0; /* Clear lower part of port */
DATA_PORT |= (value & 0xF0)>>4; /* OR in upper nibble */
EN_PIN = HIGH; /* EN enable chip (HIGH) */
DisplayDelay(0); /* We only need a very little delay */
EN_PIN = LOW; /* Latch data by dropping EN */
DisplayDelay(0); /* We only need a very little delay */
if(data_or_ctrl == CTRL_WR)
DisplayDelay(1); /* Extra delay needed for control writes */
/* Write lower nibble second */
DATA_PORT &= 0xF0; /* Clear lower part of port */
DATA_PORT |= (value & 0x0F) ; /* write to port */
EN_PIN = HIGH;
DisplayDelay(0); /* We only need a very little delay */
EN_PIN = LOW; /* Latch data by dropping EN */
DisplayDelay(1); /* Needed to put delay in between writes. */
if(data_or_ctrl == CTRL_WR)
DisplayDelay(40); /* Extra delay needed for control writes */
}
/*****************************************************************************
Name: DisplayDelay
Parameters: unit
Returns: none
Description: Delay routine for LCD display.
*****************************************************************************/
void DisplayDelay(unsigned long int units){
unsigned long int counter = units * DELAY_TIMING;
while(counter--)
{
asm ("NOP");
}
}
/*****************************************************************************
Name: lcd_show_4dig_hex
Parameters: data_msb, data_lsb, LCD-display line 1 or 2
Returns: -
Description: convert two bytes to 4 ascii chars and show them.
*****************************************************************************/
void lcd_show_4dig_hex( unsigned char data_msb,
unsigned char data_lsb,
unsigned char position)
{
unsigned int intdata;
char stringdata[3];
intdata = ((unsigned int) data_msb)<<8;
intdata = intdata + (unsigned int)data_lsb;
/* Convert data to string for LCD. */
IntToAsciiHex( stringdata, /* Pointer to where to put string */
4, /* Min nr characters output string will have */
intdata ); /* Value */
DisplayString(position, stringdata);
}
/*****************************************************************************
Name: lcd_show_2dig_hex
Parameters: data, LCD-display line 1 or 2
Returns: -
Description: convert one byte to 2 ascii chars and show them.
*****************************************************************************/
void lcd_show_2dig_hex(unsigned char data,
unsigned char position)
{
char stringdata[2];
/* Convert data to string for LCD. */
IntToAsciiHex( stringdata, /* Pointer to where to put string */
2, /* Min nr characters output string will have */
/*int*/data ); /* Value */
DisplayString(position, stringdata);
}
/*****************************************************************************
Name: IntToAsciiDec
Parameters: dest_string
Pointer to a buffer will the string will reside
min_digits
Specifies the minimum number of characters the output string will
have. Leading zeros will be written as '0' characters.
Returns: A pointer to the string's NULL character in the string that was just
created.
Does: Converts a passed unsigned int into a ASCII string represented in
base 10 decimal format.
*****************************************************************************/
char* IntToAsciiDec(char* dest_string,
int min_digits,
unsigned int value)
{
const unsigned long base10[] = {1,10,100,1000,10000,100000};
unsigned int tmp;
unsigned int i, total_digits = 0;
char buff[5];
for(i=0; i<5; i++)
{
tmp = (int)( value % base10[i+1] );
value -= tmp;
buff[i] = (char)( tmp / base10[i] );
buff[i] += '0';
if(buff[i] != '0')
total_digits = i+1;
}
if( total_digits < min_digits)
total_digits = min_digits;
i = total_digits;
while(i)
{
*dest_string++ = buff[i-1];
i--;
}
*dest_string = 0;
return dest_string;
}
/*****************************************************************************
Name: IntToAsciiHex
Parameters: dest_string
Pointer to a buffer will the string will reside
min_digits
Specifies the minimum number of characters the output string will
have. Leading zeros will be written as '0' characters.
Returns: A pointer to the string's NULL character in the string that was just
created.
Does: Converts a passed unsigned int into a ASCII string represented in
Hexidecimal format.
*****************************************************************************/
char* IntToAsciiHex( char* dest_string,
int min_digits,
unsigned int value )
{
unsigned int i, total_digits = 0;
char buff[4];
for(i=0;i<4;i++)
{
buff[i] = (char)(value & 0x0F);
value = value >> 4;
if( buff[i] <= 9)
buff[i] += '0';
else
buff[i] = (char)(buff[i] - 0xA + 'A');
if(buff[i] != '0')
total_digits = i+1;
}
if( total_digits < min_digits)
total_digits = min_digits;
i = total_digits;
while(i)
{
*dest_string++ = buff[i-1];
i--;
}
*dest_string = 0;
return dest_string;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -