📄 lcd.c
字号:
//*****************************************************************
//
// File Name : 'lcd.c'
// Title : Character LCD driver for HD44780 displays (mem-mapped)
// Author : Pascal Stang
// Date : 11/22/2000
// Version : 0.1
// Target MCU : ATmega103
// Editor Tabs : 3
//
//*****************************************************************
#include <io.h>
#include "global.h"
//#include "delay.h"
//#include "uart.h"
#include "lcd.h"
/*************************************************************/
/********************** LOCAL FUNCTIONS **********************/
/*************************************************************/
void lcdBusyWait(void)
{
// sbi(MCUCR, SRW); // enable RAM waitstate
// wait until LCD busy bit goes to zero
while(*(volatile unsigned char *) (LCD_CTRL_ADDR) & 1<<LCD_BUSY);
// cbi(MCUCR, SRW); // disable RAM waitstate
}
void lcdControlWrite(u08 data)
{
sbi(MCUCR, SRW); // enable RAM waitstate
lcdBusyWait(); // wait until LCD not busy
*(volatile unsigned char *) (LCD_CTRL_ADDR) = data;
cbi(MCUCR, SRW); // disable RAM waitstate
}
u08 lcdControlRead(void)
{
register u08 data;
sbi(MCUCR, SRW); // enable RAM waitstate
lcdBusyWait(); // wait until LCD not busy
data = *(volatile unsigned char *) (LCD_CTRL_ADDR);
cbi(MCUCR, SRW); // disable RAM waitstate
return data;
}
void lcdDataWrite(u08 data)
{
sbi(MCUCR, SRW); // enable RAM waitstate
lcdBusyWait(); // wait until LCD not busy
*(volatile unsigned char *) (LCD_DATA_ADDR) = data;
cbi(MCUCR, SRW); // disable RAM waitstate
}
u08 lcdDataRead(void)
{
register u08 data;
sbi(MCUCR, SRW); // enable RAM waitstate
lcdBusyWait(); // wait until LCD not busy
data = *(volatile unsigned char *) (LCD_DATA_ADDR);
cbi(MCUCR, SRW); // disable RAM waitstate
return data;
}
/*************************************************************/
/********************* PUBLIC FUNCTIONS **********************/
/*************************************************************/
void lcdInit()
{
// LCD function set
lcdControlWrite(LCD_FUNCTION_DEFAULT);
// clear LCD
lcdControlWrite(1<<LCD_CLR);
// set entry mode
lcdControlWrite(1<<LCD_ENTRY_MODE | 1<<LCD_ENTRY_INC);
// set display to on
lcdControlWrite(1<<LCD_ON_CTRL | 1<<LCD_ON_DISPLAY | 1<<LCD_ON_BLINK);
// move cursor to home
lcdControlWrite(1<<LCD_HOME);
// set data address to 0
lcdControlWrite(1<<LCD_DDRAM | 0x00);
}
void lcdHome(void)
{
// move cursor to home
lcdControlWrite(1<<LCD_HOME);
}
void lcdGotoXY(u08 x, u08 y)
{
register u08 DDRAMAddr;
// remap lines into proper order
switch(y)
{
case 0: DDRAMAddr = 00+x; break;
case 1: DDRAMAddr = 40+x; break;
case 2: DDRAMAddr = 20+x; break;
//case 3: DDRAMAddr = 60+x; break;
case 3: DDRAMAddr = 84+x; break; // **** why!?
default: DDRAMAddr = 0+x;
}
// set data address
lcdControlWrite(1<<LCD_DDRAM | DDRAMAddr);
}
void lcdPrintStr(char str[])
{
register u08 *c;
// check to make sure we have a good pointer
if (!(c=str)) return;
// print the string until a null-terminator
while (*c)
{
lcdDataWrite(*c);
c++;
}
}
void lcdPrintfU4(u08 Data)
{
// Send 4-bit hex value
u08 Character = Data&0x0f;
if (Character>9)
{
Character+='A'-10;
}
else
{
Character+='0';
}
lcdDataWrite(Character);
}
void lcdPrintfu08(u08 Data)
{
// Send 8-bit hex value
lcdPrintfU4(Data>>4);
lcdPrintfU4(Data);
}
void lcdPrintfu16(u16 Data)
{
// Send 16-bit hex value
lcdPrintfu08(Data>>8);
lcdPrintfu08(Data);
}
void lcdPrintfu32(u32 Data)
{
// Send 32-bit hex value
lcdPrintfu16(Data>>16);
lcdPrintfu16(Data);
}
/*
static void lcd_newline(void)
// goto start of next line
{
lcd_x = 0;
if (++lcd_y >= LCD_LINES)
lcd_y = 0;
}
static void lcd_goto(void)
// goto position (lcd_x,lcd_y)
{
lcd_command((1<<LCD_DDRAM)+LCD_LINE_LENGTH*lcd_y+lcd_x);
}
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(1640);
}
void lcd_home(void)
// set cursor to home position
{
lcd_x = lcd_y = 0;
lcd_command(1<<LCD_HOME);
//delay(1640);
}
void lcd_putchar(u08 data)
// print character to current cursor position
{
lcd_waitbusy();
if (data=='\n') {
lcd_newline();
lcd_goto();
}
else {
if (++lcd_x >= LCD_LINE_LENGTH) {
lcd_newline();
lcd_goto();
}
lcd_write(data, 1);
}
}
void lcd_puts(char s[])
// print string on lcd (no auto linefeed)
{
register u08 *c;
if (!(c=s)) return;
while (*c) {
lcd_putchar(*c);
c++;
}
}
void lcd_init(u08 cursor, u08 fnc)
// cursor: 0 = off, 2 = on, 3 = blinking
// fnc: see LCD_FUNCTION_xxx
{
u08 wait[] = { 250, 78, 1, 1 };
register u08 i;
// configure control line pins as output
sbi(DDR(LCD_E_PORT), LCD_E_PIN);
// set enable line high
sbi(LCD_E_PORT, LCD_E_PIN);
// enable external SRAM (memory mapped lcd) and wait states
outp((1<<SRE)|(1<<SRW), MCUCR);
fnc |= (1<<LCD_FUNCTION);
// reset lcd
for (i=0; i<4; i++) {
delay(wait[i]<<6); // 16ms, 5ms, 64us, 64us
lcd_write(fnc, 0); // reset function
}
lcd_command(1<<LCD_ON);
lcd_clrscr();
lcd_command(LCD_MODE_DEFAULT);
lcd_command((1<<LCD_ON)|(1<<LCD_ON_DISPLAY)|cursor);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -