⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd40x4drv.c

📁 64输入32输出单片机程序
💻 C
字号:
//-------------------------------------------------------------------------
//	Filename :  LCD40X4DRV.c
//	Language :  C for AT89S53
//	Revision :  1.0
//  Date :  	11/01/2004
//  System clock :  24.00MHZ
//-------------------------------------------------------------------------
#include <..\atmel\at89s53.h>
#include <absacc.h>
#include <intrins.h>
#include <dom12a.h>

//-------------------------------------------------------------------------
#define max_cols	40
#define max_rows	4
#define Out_Port	XBYTE[0x2000]
#define DispPort    XBYTE[0x2001]
//
//  Routinues for 40x4 LCD Display.(4 bit data transfer)
//  D7    D6   D5   D4   D3   D2   D1   D0
//  D7    D6   D5   D4   NC   RS   E1   E0		//40x4
//						 E2	  E1   NC	RS		//24X2
//global variables for LCD Display
uchar idata outp_buf =  0xff;
uchar idata col, row;
char  xdata strbuf[100];
char  xdata *ptrbuf = strbuf;

//external variables
extern bit		keyDetectEnable;

// function prototype --- Prefix with '_' as driver type
void lcd_putchar(char c);
void _set_display (char s, char d);
void _initial_disp (void);
void _clear_disp (void);
void _lcdCursor_FlashToggling (char stFlashing);
void _lcdCursorGoto(char r, char c);

void _delay5ms (void);
void _delay200us (void);
void _delay100us (void);
void _delay5us (void);

//char _outp_stus (char num);
//void _outp_bit (char num, char status);

//-------------------------------------------------------------------------
//
//	Routinues for 40x4 LCD Display.
//
//*******************************************************************
// putchar (basic version): expands '\n' into CR LF
// switching % serial port & LCD display
char putchar(char c) 	// override of the built-in putchar
{
  //-------------LCD Display only
  //_delay100us();
  if ( c == '\n' ) {
		c = ' ';
		while ( col++ < max_cols) 
			lcd_putchar(c);
		col = 0;
		if ( ++row >= max_rows )
			row = 0;	//?????????? should scroll down???
		_lcdCursorGoto(row, col);
  } else
		lcd_putchar(c);	
  
  if ( ++col >= max_cols) {
		col = 0;
		if ( ++row >= max_rows )
			row = 0;
		_lcdCursorGoto(row, col);
  } // end if
  return (c);
}  // end func.


//******************************************************************
void lcd_putchar(char c)
{
  char buf = c;
  
  if ( row < 2) {
		buf &= 0xf0;
        buf |= 0x04;
        DispPort = buf;
        buf |= 0x01;
        DispPort = buf;
        buf &= 0xfe;
        DispPort = buf;
        buf = (c<<4)&0xf0;
        buf |= 0x04;
        DispPort = buf;
        buf |= 0x01;
        DispPort = buf;
        buf &= 0xfe;
        DispPort = buf;
  } else {
		buf &= 0xf0;
        buf |= 0x04;
        DispPort = buf;
        buf |= 0x02;
        DispPort = buf;
        buf &= 0xfd;
        DispPort = buf;
        buf = (c<<4)&0xf0;
        buf |= 0x04;
        DispPort = buf;
        buf |= 0x02;
        DispPort = buf;
        buf &= 0xfd;
        DispPort = buf;
  } // end else
  _delay100us();
} // end func.

//-------------------------------------------------------------------------
void _set_display (char s, char d)
{ // senting command/data to the LCD module
  // s: 0 -- upper, 1 -- lower
  char buf = d;

  switch (s) {
    case 0 :
        buf &= 0xf0;
        DispPort = buf;
        buf |= 0x01;
        DispPort = buf;
        buf &= 0xf0;
        DispPort = buf;
        buf = (d<<4);
        buf &= 0xf0;
        DispPort = buf;
        buf |= 0x01;
        DispPort = buf;
        buf &= 0xf0;
        DispPort = buf;
        _delay100us();
        break;
    case 1 :
        buf &= 0xf0;
        DispPort = buf;
        buf |= 0x02;
        DispPort = buf;
        buf &= 0xf0;
        DispPort = buf;
        buf = (d<<4);
        buf &= 0xf0;
        DispPort = buf;
        buf |= 0x02;
        DispPort = buf;
        buf &= 0xf0;
        DispPort = buf;
        _delay100us();
  } // end switch
} // end func.

//-------------------------------------------------------------------------
void _initial_disp (void)
{ //initialize the LCD display
	unsigned char loop;

	_set_display(0, 0x30);	// 1st initial
	_set_display(1, 0x30);
    _delay5ms();
    _set_display(0, 0x30);	// 2nd initial
    _set_display(1, 0x30);
    _delay200us();
    _set_display(0, 0x30);  // 3rd initial
    _set_display(1, 0x30);
    _delay200us();
    _set_display(0, 0x20);  //
    _set_display(1, 0x20);
    _delay200us();
    _set_display(0, 0x2c);	// set display is 4bit data transfer.
    _set_display(1, 0x2c);
    _delay200us();
    _set_display(0, 0x0f);	// display on
    _set_display(1, 0x0f);
    _delay200us();
    _set_display(0, 0x01);	// clear display
    _set_display(1, 0x01);
    _delay200us();
    _set_display(0, 0x06);	// entry mode.
    _set_display(1, 0x06);
    _delay5ms();
    _set_display(0, 0x0c);	// cursor off
    _set_display(1, 0x0c);	// cursor off
    _delay5ms();

    col = row = 0;
	DispPort |= 0xf0;
	for (loop=0; loop<10; loop++)
    		_delay5ms(); 
} // end func.

//-------------------------------------------------------------------------
void _clear_disp (void)
{ // clear the LCD screen

    _set_display(0, 0x01);
    _set_display(1, 0x01);
	row = col = 0;
    _delay5ms();
} // end func.

//-------------------------------------------------------------------------
void _lcdCursor_FlashToggling (char stFlashing)
{ // cursor flashing ON/OFF toggling
  uchar r;
  
  if ( row < 2) 
	r = 0;
  else
	r = 1;

  if (stFlashing)
		_set_display(r, 0x0f);	  // r row ON ,r=0 ,1&2 row r=1 3&4 row
  else
		_set_display(r, 0x0c);	  // r row OFF
} // end func.

//-------------------------------------------------------------------------
void _lcdCursorGoto(char r, char c)
{  
  row = r; col = c;
  switch(r){
     case 1:c |= 0xc0;
			_set_display(0,c);
		    break;
     case 0:c |= 0x80;
			_set_display(0,c);
		    break;
     case 3:c |= 0xc0;
			_set_display(1,c);
		    break;
	 case 2:c |= 0x80;
			_set_display(1,c);
		    break;
  } // end switch

} // end func.

//assume the uP works under 24MHz
//-------------------------------------------------------------------------
void _delay5ms(void)
{ 
  unsigned int tref = 900;
  while (tref != 0)
  		tref--;
}
//-------------------------------------------------------------------------
void _delay200us (void)
{
    char tref = 64;
    while (tref--);
}
//-------------------------------------------------------------------------
void _delay100us (void)
{
    char tref = 32;
    while (tref--);
} // end func.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -