📄 lcd_twi.c
字号:
/*C**************************************************************************
* NAME: lcd_twi.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: cc03-demo-adc-0_0_1
* REVISION: 1.16
*----------------------------------------------------------------------------
* PURPOSE: This file contains the functions to control the LCD.
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
#include "lcd_twi.h"
#include <string.h>
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N S ______________________________________________*/
/*F**************************************************************************
* NAME: lcd_ddram_addr
*----------------------------------------------------------------------------
* PARAMS:
* uc_line: line number on the LCD
* return: uc_address: DDRAM address
*----------------------------------------------------------------------------
* PURPOSE:
* This function returns the DDRAM address corresponding
* to the define line number.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Uchar code lcd_ddram_addr[]={0,LCD_LINE_1,LCD_LINE_2,LCD_LINE_3,LCD_LINE_4};
/*_____ D E C L A R A T I O N S ____________________________________________*/
char code sz_blank_line[]=BLANK_LINE;
/*F**************************************************************************
* NAME: lcd_init
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Init the LCD 4 bits interface (see algo P46 in HITACHI datasheet)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit lcd_init(void)
{
tempo(TIMER_40_MS);
lcd_cmd4_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_INIT);
tempo(TIMER_5_MS);
lcd_cmd4_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_INIT);
tempo(TIMER_1_MS);
lcd_cmd4_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_INIT);
lcd_cmd4_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_INIT_DL);
lcd_cmd_wr (LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_4_DATA|LCD_2_LINE|LCD_FONT_5_10);
lcd_cmd_wr (LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_DISP_ON|LCD_CURS_OFF|LCD_BLINK_OFF);
lcd_cmd_wr (LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_CLR );
tempo(TIMER_5_MS); // clear screen
lcd_cmd_wr (LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_INC|LCD_S_0);
return TRUE;
}
/*F**************************************************************************
* NAME: lcd_cmd_wr
*----------------------------------------------------------------------------
* PARAMS:
* *para1: Uchar uc_value
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Send a data value on 8 bits with 2 commands TWI (4 bits mode)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void lcd_cmd_wr(Uchar value)
{
Byte uc_msb,uc_lsb;
/*MSB +LSB in 4 bits mode*/
uc_msb = (value >> 4); /* 4 highest bit of value + RS=RW=0 */
uc_lsb = value & 0x0F; /* 4 lowest bit of value + RS=RW=0 */
twi_putchar(TWI_ADDR_LCD,uc_msb|LCD_E_1|LCD_RW_0|LCD_RS_0); // MSB
twi_putchar(TWI_ADDR_LCD,uc_msb|LCD_E_0|LCD_RW_0|LCD_RS_0);
twi_putchar(TWI_ADDR_LCD,uc_lsb|LCD_E_1|LCD_RW_0|LCD_RS_0); // LSB
twi_putchar(TWI_ADDR_LCD,uc_lsb|LCD_E_0|LCD_RW_0|LCD_RS_0);
}
/*F**************************************************************************
* NAME: lcd_cmd4_wr
*----------------------------------------------------------------------------
* PARAMS:
* *para1: Uchar uc_value
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Send a data value on 4 bits with 1 commands TWI (4 bits mode)
* Only the LSB part is sent.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void lcd_cmd4_wr(Uchar uc_value)
{
uc_value &= 0x0F;
twi_putchar(TWI_ADDR_LCD,uc_value|LCD_E_1|LCD_RW_0|LCD_RS_0);
twi_putchar(TWI_ADDR_LCD,uc_value|LCD_E_0|LCD_RW_0|LCD_RS_0);
}
/*F**************************************************************************
* NAME: lcd_putchar
*----------------------------------------------------------------------------
* PARAMS:
* uc_wr_byte: byte to write on display
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Write a byte on the display
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void lcd_putchar(Uchar uc_wr_byte)
{
unsigned char uc_msb, uc_lsb;
// MSB + LSB in 4 bits mode
uc_msb = (uc_wr_byte >> 4); // 4 highest bit of value + RS=RW=0
uc_lsb = uc_wr_byte & 0x0F; // 4 lowest bit of value + RS=RW=0
twi_putchar(TWI_ADDR_LCD,uc_msb|LCD_E_1|LCD_RW_0|LCD_RS_1); // MSB
twi_putchar(TWI_ADDR_LCD,uc_msb|LCD_E_0|LCD_RW_0|LCD_RS_1);
twi_putchar(TWI_ADDR_LCD,uc_lsb|LCD_E_1|LCD_RW_0|LCD_RS_1); // LSB
twi_putchar(TWI_ADDR_LCD,uc_lsb|LCD_E_0|LCD_RW_0|LCD_RS_1);
}
/*F**************************************************************************
* NAME: lcd_gotoxy
*----------------------------------------------------------------------------
* PARAMS:
* x: horizontal move 1..x, left to right
* y: vertical move 1..y, top to bottom
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Absolute move of virtual cursor: the next lcd_putchar will place the
* character at position x,y.
*----------------------------------------------------------------------------
* EXAMPLE:
* lcd_gotoxy(0,0); // goto home <=> lcd_gotoxy(1,1)
*----------------------------------------------------------------------------
* NOTE:
* x=0 => beginning of line
*----------------------------------------------------------------------------
* REQUIREMENTS:
* VT100 compatibility, except if x=0
*****************************************************************************/
void lcd_gotoxy (Byte x, Byte y)
{
if (x) x--;
if (!y) y=1;
lcd_set_ddram(lcd_ddram_addr[y]+x);
}
/*F**************************************************************************
* NAME: lcd_clrscr
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* This function clears the entire display.
* This function uses a controller command.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void lcd_clrscr (void)
{
lcd_cmd_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_CLR);
tempo(TIMER_5_MS); // clear screen
}
#ifdef ENABLE_SPLASH_SCREEN
Byte xdata state_lcd_splash=1;
Uchar xdata uc_buffer_scroll[ROW_LENGTH + 1];
/*F**************************************************************************
* NAME: lcd_splash
*----------------------------------------------------------------------------
* PARAMS:
* return: TRUE if terminated
*----------------------------------------------------------------------------
* PURPOSE:
* Display the LCD Splash screen
*----------------------------------------------------------------------------
* EXAMPLE:
* while (!lcd_splash());
*----------------------------------------------------------------------------
* REQUIREMENTS:
* Define ENABLE_SPLASH_SCREEN in config.h to be able to compile the functions
* necessary for lcd_splash
*****************************************************************************/
bit lcd_splash (void)
{
switch(state_lcd_splash)
{
case 1 : lcd_scrolling_line(1, "ATMEL", LCD_SCROLL_INC); break;
case 2 : lcd_scrolling_line(3, __DATE__ , LCD_SCROLL_DEC); break;
default: return TRUE;
}
state_lcd_splash++;
return FALSE;
}
/*F**************************************************************************
* NAME: lcd_wr_str
*----------------------------------------------------------------------------
* PARAMS:
* uc_line: Line number on the display (1, 2, 3 or 4)
* *psz: pointer on the string to write
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Send a string on the display.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* Define ENABLE_SPLASH_SCREEN in config.h to use this function
*****************************************************************************/
void lcd_wr_str(Uchar uc_line,Uchar *psz)
{
lcd_cmd_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_DISP_ON|LCD_CURS_OFF|LCD_BLINK_OFF);
lcd_cmd_wr(LCD_E_0|LCD_RW_0|LCD_RS_0|LCD_CURS_MOV|LCD_RIGHT_SHIFT);
lcd_ddram_init(uc_line);
for(;*psz;psz++) lcd_putchar(*psz);
}
/*F**************************************************************************
* NAME: lcd_scrolling_line
*----------------------------------------------------------------------------
* PARAMS:
* uc_line: line number
* pt_string: string to display and shift
* uc_direction: select a right shift (LCD_SCROLL_INC)or
* a left one LCD_SCROLL_INC.
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -