📄 tg_4bitlcd.c
字号:
/* HIDE -> Cursor invisible */
/*------------------------------------------------------------------*/
/* Returnvalue : none */
/*------------------------------------------------------------------*/
/* History : 06/99 V1.0 Basic routine */
/* 09/99 V1.1 Calculation of control modified */
/* (because of compiler otim. bug) */
/* */
/********************************************************************/
void control_LCD(unsigned char dsp,unsigned char blink,unsigned char cursor)
{
unsigned char control; // variable to generate instruction byte
control = (0x08 + blink + cursor + dsp); // Cursor control
EA = 0; // Disable all interrupts
LCD_RS = CTRL_REG; // Switch to instruction register
// Set LCD_DATA to high nibble of Display On/Off Control
LCD_DATA = (LCD_DATA&0x0F)|0x00;
LCD_E = 1; LCD_E = 0; // Write data to display
// Set LCD_DATA to lower nibble of Display On/Off Control
LCD_DATA = (LCD_DATA&0x0F)|((control<<4)&0xF0);
LCD_E = 1; LCD_E = 0; // Write data to display
EA = 1; // Enable all interrupts
delay(4); // Wait 400祍
return;
}
/********************************************************************/
/* Function : gotoxy(x,y) SUB */
/*------------------------------------------------------------------*/
/* Description : Sets the write position of the LCD display */
/* */
/* (1,1) (16,1) */
/* | | */
/* ################ -> line 1 */
/* ################ -> line 2 */
/* | | */
/* (1,2) (16,2) */
/*------------------------------------------------------------------*/
/* Author : Thorsten Godau NT8 */
/*------------------------------------------------------------------*/
/* Input : unsigned char x -> x position (horizontal) */
/* unsigned char y -> y position (vertical) */
/*------------------------------------------------------------------*/
/* Returnvalue : none */
/*------------------------------------------------------------------*/
/* History : 06/99 V1.0 Basic routine */
/* */
/********************************************************************/
void gotoxy(unsigned char x,unsigned char y)
{
EA = 0; // Disable all interrupts
LCD_RS = CTRL_REG; // Switch to instruction register
// Set LCD_DATA to high nibble of position table value
LCD_DATA = (LCD_DATA&0x0F)|((LOCATION[y-1][x-1])&0xF0);
LCD_E = 1; LCD_E = 0; // Write data to display
// Set LCD_DATA to lower nibble of position table value
LCD_DATA = (LCD_DATA&0x0F)|(((LOCATION[y-1][x-1])<<4)&0xF0);
LCD_E = 1; LCD_E = 0; // Write data to display
EA = 1; // Enable all interrupts
delay(4); // Wait 400祍
return;
}
/********************************************************************/
/* Function : clrscr() SUB */
/*------------------------------------------------------------------*/
/* Description : Clears LCD display, and sets position to (1,1) */
/*------------------------------------------------------------------*/
/* Author : Thorsten Godau NT8 */
/*------------------------------------------------------------------*/
/* Input : none */
/*------------------------------------------------------------------*/
/* Returnvalue : none */
/*------------------------------------------------------------------*/
/* History : 06/99 V1.0 Basic routine */
/* 09/99 V1.1 Timing correction */
/* */
/********************************************************************/
void clrscr(void)
{
EA = 0; // Disable all interrupts
LCD_RS = CTRL_REG; // Switch to instruction register
// Set LCD_DATA to high nibble of Clear Screen
LCD_DATA = (LCD_DATA&0x0F)|0x00;
LCD_E = 1; LCD_E = 0; // Write data to display
// Set LCD_DATA to lower nibble of Clear Screen
LCD_DATA = (LCD_DATA&0x0F)|0x10;
LCD_E = 1; LCD_E = 0; // Write data to display
EA = 1; // Enable all interrupts
delay(41); // Wait 4.1ms
return;
}
/********************************************************************/
/* Function : LCD_putchar(value) SUB */
/*------------------------------------------------------------------*/
/* Description : Writes the character value to the display */
/*------------------------------------------------------------------*/
/* Author : Thorsten Godau NT8 */
/*------------------------------------------------------------------*/
/* Input : unsigned char value -> character to be written */
/*------------------------------------------------------------------*/
/* Returnvalue : none */
/*------------------------------------------------------------------*/
/* History : 06/99 V1.0 Basic routine */
/* */
/********************************************************************/
void LCD_putchar(unsigned char value)
{
EA = 0; // Disable all interrupts
LCD_RS = DATA_REG; // Switch to data register
// Set LCD_DATA to high nibble of value
LCD_DATA = (LCD_DATA&0x0F)|(value&0xF0);
LCD_E = 1; LCD_E = 0; // Write data to display
// Set LCD_DATA to lower nibble of value
LCD_DATA = (LCD_DATA&0x0F)|((value<<4)&0xF0);
LCD_E = 1; LCD_E = 0; // Write data to display
EA = 1; // Enable all interrupts
delay(4); // Wait 400祍
return;
}
/********************************************************************/
/* Function : LCD_printxy(x,y,*text) SUB */
/*------------------------------------------------------------------*/
/* Description : Prints text to position x/y of the display */
/*------------------------------------------------------------------*/
/* Author : Thorsten Godau NT8 */
/*------------------------------------------------------------------*/
/* Input : unsigned char x -> x position of the display */
/* unsigned char y -> y position of the display */
/* unsigned char *text -> pointer to text */
/*------------------------------------------------------------------*/
/* Returnvalue : none */
/*------------------------------------------------------------------*/
/* History : 06/99 V1.0 Basic routine */
/* */
/********************************************************************/
void LCD_printxy(unsigned char x,unsigned char y, unsigned char *text)
{
gotoxy(x,y); // Set cursor position
while( *text ) // while not end of text
{
LCD_putchar(*text++); // Write character and increment position
}
return;
}
/********************************************************************/
/* Function : delay(delaytime) SUB */
/*------------------------------------------------------------------*/
/* Description : Routine waits (dependening on Timer 0) for about */
/* delaytime * 100祍. Best for delaytime > 10 */
/*------------------------------------------------------------------*/
/* Author : Thorsten Godau NT8 */
/*------------------------------------------------------------------*/
/* Input : unsigned int time_100祍 */
/*------------------------------------------------------------------*/
/* Returnvalue : none */
/*------------------------------------------------------------------*/
/* History : 06/99 V1.0 Basic routine */
/* 09/99 V1.1 nop inserted in delay-loop */
/* */
/********************************************************************/
void delay(unsigned int time_100us)
{
if ( time_100us == 0 ) return; // Return if no delaytime is assigned
DELAY = time_100us; // Set global delaytime
while ( DELAY > 0 )
{
_asm
nop
_endasm;
} // Wait delaytime
return;
}
// MAIN =============================================================
void main( void )
{
init_MCU(); // Initialize MCU
init_LCD(); // Initialize the LCD display
LCD_printxy(1,1,"Test of the LCD!"); // Message to LCD
LCD_printxy(1,2,"---4bit mode----"); // Message to LCD
control_LCD(ON,NOBLINK,HIDE); // Display on, cursor hidden and non-blinking
while(1) // Main loop
{
// Do nothing
_asm
nop
_endasm;
}
}
// END MAIN =========================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -