?? 12864lcd.h
字號:
/*
管腳號 管腳名稱 電平 管腳功能描述
1 VSS 0V 電源地
2 VCC +5V 電源正
3 V0 - 對比度(亮度)調整
4 RS(CS) H/L RS="H",表示DB7--DB0為顯示數據
4 RS(CS) H/L RS="L",表示DB7--DB0為顯示指令數據
5 R/W(SID) H/L R/W="H",E="H",數據被讀到DB7--DB0
5 R/W(SID) H/L R/W="L",E="H→L", DB7--DB0的數據被寫到IR或DR
6 E(SCLK) H/L 使能信號
7 DB0 H/L 三態數據線
8 DB1 H/L 三態數據線
9 DB2 H/L 三態數據線
10 DB3 H/L 三態數據線
11 DB4 H/L 三態數據線
12 DB5 H/L 三態數據線
13 DB6 H/L 三態數據線
14 DB7 H/L 三態數據線
15 PSB H/L H:8位或4位并口方式,L:串口方式(見注釋1)
16 NC - 空腳
17 /RESET H/L 復位端,低電平有效(見注釋2)
18 VOUT - LCD驅動電壓輸出端
19 A VDD 背光源正端(+5V)(見注釋3)
20 K VSS 背光源負端(見注釋3)
*/
//使用8M晶振
//定義MCU與LCD的接口
#define LCD_EN_PORT PORTD
#define LCD_RW_PORT PORTD
#define LCD_RS_PORT PORTD
#define CONTROL_PORT DDRD
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_RS 0x10 //portD4 out/in
#define LCD_RW 0x20 //portD5 out
#define LCD_EN 0x40 //portD6 port out
#define LCD_DATA 0xff //portC 0~4/5/6/7 out
/**********************************************************
RT12864M LCD DISPLAY
建立時間:2005年2月1號
修改日期:2005年2月1號
**********************************************************/
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init (void);
void LCD_en_write (void);
void LCD_write_char (unsigned char command,unsigned char data);
void LCD_wait_Ready (void);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
//void delay_nus (unsigned int n);
//void delay_nms (unsigned int n);
//void delay_1us (void);
//void delay_1ms (void);
void delay_1us(void) //1us延時函數
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延時函數
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void LCD_init(void)
{
LCD_DATA_DDR|=LCD_DATA; //set output 0xff
CONTROL_PORT |= LCD_RS | LCD_EN | LCD_RW; //設置控制輸出
delay_nms(1);
LCD_write_char(0x01,0); //顯示清屏
LCD_write_char(0x0C,0); //顯示開
LCD_write_char(0x80,0); //顯示光標移動設置
}
void LCD_en_write(void) //EN端產生一個高電平脈沖,寫LCD
{
LCD_EN_PORT |= LCD_EN; //EN=1
delay_nus(1);
LCD_EN_PORT &= ~LCD_EN; //EN=0
}
/*-----------------------------------------------------------------------
LCD_write_char : 中英文字符串顯示函數
LCD_write函數功能:當command=0時,向LCD寫入數據,否則向LCD寫
入命令
輸入參數:*s :中英文字符串指針;
X、Y : 顯示字符串的位置,X:0-15,Y:0-1
LCD第一行顯示寄存器地址:0X80-0X87
LCD第二行顯示寄存器地址:0X90-0X9F
LCD第三行顯示寄存器地址:0X88-0X8F
LCD第四行顯示寄存器地址:0X98-0X9F
-----------------------------------------------------------------------*/
void LCD_write_char(unsigned command,unsigned data)
{
unsigned command_temp;
unsigned data_temp;
command_temp = command;
data_temp = data;
LCD_wait_Ready();
if (command == 0)
{
LCD_EN_PORT &= ~LCD_EN; //EN=0
LCD_RS_PORT |= LCD_RS; //RS=1
LCD_RW_PORT &= ~LCD_RW; //RW=0
LCD_DATA_PORT = data_temp; //send 8bit
LCD_en_write();
}
else
{
LCD_EN_PORT &= ~LCD_EN; //EN=0
LCD_RS_PORT &= ~LCD_RS; //RS=0
LCD_RW_PORT &= ~LCD_RW; //RW=0
LCD_DATA_PORT = command_temp;//send command
LCD_en_write();
}
LCD_RW_PORT |= LCD_RW;
LCD_RS_PORT ^= LCD_RS;
}
void LCD_wait_Ready(void) //等待LCD空閑
{
LCD_DATA_DDR &= ~0x80; //PD7 I/O口方向設置為輸入
LCD_RS_PORT &= ~LCD_RS; //RS=0
// delay_1us(1);
LCD_RW_PORT |= LCD_RW; //RW=1
LCD_EN_PORT |= LCD_EN; //EN=1
while (!( LCD_DATA_PIN&0x80 ) == 0);
LCD_EN_PORT &= ~LCD_EN; //EN=0
LCD_DATA_DDR |= 0xFF;
}
/*-----------------------------------------------------------------------
LCD_set_xy : 設置LCD顯示的起始位置
輸入參數:x、y : 顯示字符串的位置,X:0-15,Y:0-1
LCD第一行顯示寄存器地址:0X80-0X8F
LCD第一行顯示寄存器地址:0XC0-0XCF
編寫日期 :2005
最后修改日期 :2005
-----------------------------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
switch(y){
case 0:
address = 0x80 + x;
break;
case 1:
address = 0x90 + x;
break;
case 2:
address = 0x88 + x;
break;
case 3:
address = 0x98 + x;
break;
default:address = 0x80 + x;}
LCD_write_char( address, 0 );
}
/*-----------------------------------------------------------------------
LCD_write_string : 中英文字符串顯示函數
輸入參數:*s :英文字符串指針;
X、Y : 顯示字符串的位置
編寫日期 :2005
最后修改日期 :2005
-----------------------------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y );
while (*s)
{
LCD_write_char( 0, *s );
s ++;
delay_nms(1);
}
}
void LCD_char(unsigned char X,unsigned char Y,unsigned char s)
{
LCD_set_xy( X, Y );
LCD_write_char( 0, s );
}
void LCD_SHU(unsigned char X,unsigned char Y,unsigned int p)
{unsigned char shu[]={'0','1','2','3','4','5','6','7','8','9'};
unsigned int wan;
unsigned int qian;
unsigned int bai;
unsigned int shi;
unsigned int d;
unsigned int wei;
wan=p/10000;
d=p-wan*10000;
qian=d/1000;
d=d-qian*1000;
bai=d/100;
d=d-bai*100;
shi=d/10;
d=d-shi*10;
wei=d;
LCD_set_xy( X, Y );
LCD_write_char( 0, shu[wan] );
LCD_write_char( 0, shu[qian] );
LCD_write_char( 0, shu[bai] );
LCD_write_char( 0, '.' );
LCD_write_char( 0, shu[shi] );
LCD_write_char( 0, shu[wei] );
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -