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

📄 lcd.c

📁 avr charactor lcd display source
💻 C
字号:
/*
  Copyright (C) 2001 Jesper Hansen <jesperh@telia.com>.

  Rewritten by:	Nikolai Vorontsov <nickviz@mail.be>

  This file is part of the yampp system.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation, 
  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <avr/io.h>
#include <avr/pgmspace.h>

#include "lcd.h"
//#include "mem.h"
#include "delay.h"
//#include "types.h"

#ifdef ENABLE_LCD

static u08 lcd_x, lcd_y;

/*************************************************************/
/********************** LOCAL FUNCTIONS **********************/
/*************************************************************/
void  lcdDelay(int del)
{
 while ( del--);
}


void  LCDSetAddress(u08 rs, u08 rw) 
{ 
	if(rw == 0)
	{
		lcd_io_write();
	}
	else
	{
		 lcd_io_read();
  	}
  
  	if(rs == 0)
  	{
  	  	lcd_io_instruction();
	}
  	else
  	{
  		lcd_io_data();
	}

}


static void lcd_waitbusy(void)				// loops while lcd is busy 
{															// no watchdog in this function
	register u08 i = 0;				// should work fast enough 
	register u08 data;
	
	// setup RS and RW pins
  LCDSetAddress(LCD_IO_INSTRUCTION , LCD_IO_READ);
  
	outp(0x00, LCD_DATA_DDR );			// set port as input
	outp(0xff, LCD_DATA_PORT);			// enable pullup
	
	//LCD_DATA_DDR = 0x00;
	//LCD_DATA_PORT = 0xff;
	
	do
	{
		lcd_en_high();				// set LCD enable high
		
		data = inp(PIN(LCD_DATA_PORT));		// read byte
		
		lcd_en_low();				// set LCD enable low
		i++;
	} while ((data & (1 << LCD_BUSY)) && (i != 255));
	//enable_ram();					// enable ExtRAM
}


static void lcd_write(u08 data, u08 rs) 
{

	lcd_waitbusy();
	//_delay_ms(1);
	// setup RS and RW pins
  LCDSetAddress(rs , LCD_IO_WRITE);
  	
	outp(0xff, LCD_DATA_DDR );		// set port as output
	outp(data, LCD_DATA_PORT);		// write byte
	//LCD_DATA_DDR = 0xff;
	//LCD_DATA_PORT = data;
	
	lcd_en_high();				// set LCD enable high
	_Delay(10);
	//asm volatile ("nop");
	//asm volatile ("nop");	 
	lcd_en_low();				// set LCD enable low
	
	_Delay(10);
	//enable_ram();				// enable ExtRAM
}


static void lcd_newline(void)		// goto start of next line
{
    lcd_x = 0;
    if (lcd_y < LCD_LINES)
        lcd_y++;
}


void lcd_goto(void) 		// goto position (lcd_x,lcd_y)
{

#if (LCD_LINE_LENGTH == 16)
 static	u08 lcd_line[] = {0x00, 0x40, 0x10, 0x50};
#else
 static	u08 lcd_line[] = {0x00, 0x40, 0x14, 0x54};
#endif
	lcd_command((1 << LCD_DDRAM) + lcd_line[lcd_y] + lcd_x);
}



/*************************************************************/
/********************* PUBLIC FUNCTIONS **********************/
/*************************************************************/

void lcd_command(u08 cmd)			// send commando <cmd> to LCD 
{
	lcd_write(cmd, LCD_IO_INSTRUCTION);
}


void lcd_data(u08 data)				// send data <data> to LCD
{
	lcd_write(data, LCD_IO_DATA);
}


void lcd_gotoxy(u08 x, u08 y)		// goto position (x,y) 
{
	lcd_x = x;  lcd_y = y;
	lcd_goto();
}


void lcd_clrscr(void)				// clear lcd
{
	lcd_x = lcd_y = 0;
	lcd_command( 1<< LCD_CLR);
	_delay_10us(300);					// 3 ms delay
}


#ifdef ENABLE_LCD_HOME_COMMAND
void lcd_home(void)					// set cursor
{									//  to home position
	lcd_x = lcd_y = 0;
	lcd_command(1 << LCD_HOME);
	_delay_10us(200);					// 2 ms delay
}
#endif


void lcd_putchar(u08 data)			// print character to
{						//  current cursor position
	if (data == '\n')
	{
		lcd_newline();
		lcd_goto();
	}
	else
	{
		if (lcd_x < LCD_LINE_LENGTH)
		{
			lcd_x++;
			lcd_write(data, LCD_IO_DATA);
		}
	}
}


void lcd_putstring(u08* str, u08 str_size)				// print string on lcd 
{						//  (no auto linefeed)
	u08 j;
	
	for(j=0; j<str_size; j++)
	{
		lcd_putchar(*str++);
	}
}

void lcd_puts(u08* str)				// print string on lcd 
{						//  (no auto linefeed)
	while (*str)
		lcd_putchar(*str++);
}

void lcd_reputs(u08* str)				// print string on lcd 
{						//  (no auto linefeed)
	while (*str)
		lcd_putchar(*str--);
}

void lcd_puts_p(u08* p)				// print string from flash on lcd 
{	
	register u08 b;
		while ((b = pgm_read_byte(p++)))
			lcd_putchar(b);
}


static u16 wait[] = {7,7,500,1600};

         
#endif

//------------------------------------------------------------------------------
// cursor:   0 = off, 2 = on, 3 = blinking
// fnc: see LCD_FUNCTION_xxx
void lcd_init(u08 cursor, u08 fnc)
{
#ifdef ENABLE_LCD
	register u08 i = 3;

	fnc |= (1 << LCD_FUNCTION);
	
	do						// reset lcd
	{
		
		_delay_10us(wait[i]);			// 16ms, 5ms, 70us, 70us
		lcd_write(fnc, LCD_IO_INSTRUCTION);	// reset function
	} while (i--);

	lcd_command(1 << LCD_ON);
	lcd_clrscr();
	lcd_command(LCD_MODE_DEFAULT);
	lcd_command((1 << LCD_ON)|(1 << LCD_ON_DISPLAY) | cursor);
#endif

} 

⌨️ 快捷键说明

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