📄 lcd4bit._c
字号:
/*
** File: LCD4BITS.C
**
** Purpose: LCD Routines for 4 bit I/O (D4->D7+E+RS)
** D4-D7 is connected on PORTB4-7
** E is connected on PORTB3
** RS is connected on PORTB2
**
** Chip: ATMega48
**
** Version: 1.0.0, 23:rd of May 2003
**
** Author: Lars Wictorsson
** LAWICEL / SWEDEN
** http://www.lawicel.com lars@lawicel.com
**
** Copyright: The copyright to the computer program(s) herein is the
** property of LAWICEL HB, Sweden. The program(s) may be used
** and/or copied only with the written permission of LAWICEL HB
** in accordance with the terms and conditions stipulated in
** the agreement/contract under which the program(s) have been
** supplied.
**
** Remarks: This program is tested with ICCAVR version 6.27A.
**
** History: 2003-05-23 1.0.0 Created (LWI)
*/
#include <iom48v.h>
#include <macros.h>
//void waitMS(unsigned long);
#define WASTE waste_lcd()
#define LCD_E_SET PORTB |= 0x40 //PORTB.3-LCD ENABLE
#define LCD_E_CLR PORTB &= ~0x40 //PORTB.3-LCD DISABLE
#define LCD_RS_SET PORTB |= 0x80 //PORTB.2-DISPLAY DATA
#define LCD_RS_CLR PORTB &= ~0x80 //PORTB.2-INSTRCTION
//** E is connected on PORTB3
//** RS is connected on PORTB2
#define xtal 8
#define DELAY for (del = 0; del < 16; del++)
long del;
void waste_lcd(void)
{
}
void delay_1ms(void)
{
unsigned int i;
for(i=1;i<(unsigned int)(xtal*143-2);i++);
}
void waitms(unsigned int n)
{
unsigned int i=0;
while(i<n)
{
delay_1ms();
i++;
}
}
void LCD4_WR4bits(unsigned char c)
{
c &= 0x0F;//屏蔽高四位
c = c << 4;//移入高四位
LCD_E_SET;
WASTE;
PORTB &= 0x0F;
PORTB |= c;
WASTE;
LCD_E_CLR;
WASTE;
}
void LCD4_Init(void)
{
waitms(15); // Wait on power up 15mS
LCD_RS_CLR;
DELAY;
LCD4_WR4bits( 0x03); // Function Set 8bit
waitms(5);
LCD4_WR4bits( 0x03); // Function Set 8bit
waitms(5);
LCD4_WR4bits( 0x03); // Function Set 8bit
waitms(5);
LCD4_WR4bits( 0x02); // Function Set 4bit
waitms(5);
LCD4_WR4bits( 0x02); // Function Set
LCD4_WR4bits( 0x08);
DELAY;
LCD4_WR4bits( 0x00); // Display ON/OFF
LCD4_WR4bits( 0x0C); // Display ON, Curson OFF, Blink OFF
DELAY;
LCD4_WR4bits( 0x00); // Entry mode
LCD4_WR4bits( 0x06);
DELAY;
LCD_RS_SET;
waitms(5);
}
void LCD4_Clear(void)
{
LCD_RS_CLR;
DELAY;
LCD4_WR4bits( 0x00); // Clear Display
LCD4_WR4bits( 0x01);
DELAY;
LCD_RS_SET;
waitms(5);
}
void LCD4_Home(void)
{
LCD_RS_CLR;
DELAY;
LCD4_WR4bits( 0x00);
LCD4_WR4bits( 0x02);
DELAY;
LCD_RS_SET;
waitms(5);
}
void LCD4_Home2(void)
{
LCD_RS_CLR;
DELAY;
LCD4_WR4bits( 0x0C); // Clear Display
LCD4_WR4bits( 0x00);
DELAY;
LCD_RS_SET;
waitms(5);
}
void LCD4_PutC(unsigned char data)
{
LCD4_WR4bits( data >> 4);
DELAY;
LCD4_WR4bits( data);
DELAY;
}
void LCD4_PutC_Const(const unsigned char data)
{
LCD4_WR4bits( data >> 4);
DELAY;
LCD4_WR4bits( data);
DELAY;
}
void LCD4_PutS(unsigned char *data)
{
unsigned char c = 0;
while (data[c] != 0) {
LCD4_WR4bits( data[c] >> 4);
LCD4_WR4bits( data[c++]);
DELAY;
}
}
void main(void)
{
LCD4_Init();
LCD4_Clear();
LCD4_Home();
LCD4_Home2();
// LCD4_PutC(unsigned char);
// LCD4_PutC_Const(const unsigned char);
// LCD4_PutS(unsigned char *);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -