?? 1602.h
字號:
/********************************************************
* 函數(shù)庫說明:LCD1602B基本驅(qū)動函數(shù)庫
* 版本: v0.0
* 作者: cht
* 日期: 2007年11月14日
* 修改: cht
* 修改日期: 2007年11月14日
*
* 說明:
* LCD驅(qū)動采用4線驅(qū)動方法,數(shù)據(jù)的雙向傳輸由PA口的PA7-PA6-PA5-PA4
* LCD_write函數(shù)功能:當command=0時,向LCD寫入數(shù)據(jù),否則向LCD寫入命令
* LCD第一行顯示寄存器地址:0X80-0X8F
* LCD第二行顯示寄存器地址:0XC0-0XCF
*********************************************************/
#include <util/delay.h>
#include <avr/io.h>
#include "avr_m16.h"
#ifndef LCD_1602
#define LCD_1602
/*--------------------------------------
常 數(shù) 宏 定 義
----------------------------------------*/
#define LCD_RS_PORT PORTB
#define LCD_WR_PORT PORTB
#define LCD_EN_PORT PORTB
#define LCD_DATA_PORT PORTD
#define LCD_DATA_PIN PIND
#define LCD_DATA_DDR DDRD
#define LCD_RS_H _BV(PB5)
#define LCD_WR_H _BV(PB6)
#define LCD_EN_H _BV(PB7)
#define LCD_RS_L ~_BV(PB5)
#define LCD_WR_L ~_BV(PB6)
#define LCD_EN_L ~_BV(PB7)
/*--------------------------------------
函 數(shù) 聲 明
----------------------------------------*/
void LCD_init (void);
void LCD_write_en (void);
void LCD_write(uint8_t cmd ,uint8_t data);
void LCD_waite (void);
void LCD_set_xy(uint8_t x,uint8_t y);
void LCD_write_string(uint8_t x,uint8_t y,char *s);
void LCD_self_def(uint8_t add,uint8_t table[]);
void LCD_write_char(uint8_t x,uint8_t y,uint8_t c);
/*---------------------------------------------------
void LCD_write_en (void)
說明:允許向LCD寫
1、是寫“命令”還是“數(shù)據(jù)”要看 LCD_ write函數(shù)中的 cmd 參數(shù)
-----------------------------------------------------*/
void LCD_write_en (void)
{
LCD_EN_PORT |=LCD_EN_H;
wait_us(15);
LCD_EN_PORT &=LCD_EN_L;
}
/*---------------------------------------------------
void LCD_write (uint8_t cmd ,uint8_t data);
說明:向LCD寫
1、 cmd<>0 寫命令 cmd為指令,data=0
2、 cmd=0 寫數(shù)據(jù) data 為數(shù)據(jù)
-----------------------------------------------------*/
void LCD_write(uint8_t cmd ,uint8_t data)
{
uint8_t cmd_temp,data_temp;
cmd_temp = cmd;
data_temp = data;
LCD_waite();
LCD_WR_PORT &=LCD_WR_L;
LCD_DATA_PORT &= 0X0f;
if(cmd==0)
{
LCD_RS_PORT |= LCD_RS_H; //RS=1
LCD_DATA_PORT |= data_temp&0xf0; //送數(shù)據(jù)的高 4bit
}
else
{
LCD_RS_PORT &= LCD_RS_L; //RS=0
LCD_DATA_PORT |= cmd_temp&0xf0; //送命令的高4bit
};
LCD_write_en();
LCD_DATA_PORT &= 0X0f;
cmd_temp=cmd_temp << 4; //send low 4bit
data_temp=data_temp << 4;
if (cmd==0)
LCD_DATA_PORT |= data_temp&0xf0;
else
LCD_DATA_PORT |= cmd_temp&0xf0;
LCD_write_en();
LCD_WR_PORT |= LCD_WR_H;
LCD_RS_PORT &= LCD_RS_L;
}
/*----------------------------------------------------------
void LCD_waite (void)
說明:等待LCD不忙
------------------------------------------------------------*/
void LCD_waite (void)
{
LCD_DATA_DDR &= ~0x80; //PA7 I/O口方向設置為輸入
LCD_WR_PORT |= LCD_WR_H; //RW=1
LCD_RS_PORT &= LCD_RS_L; //RS=0
LCD_EN_PORT |= LCD_EN_H; //EN=1
while (!( LCD_DATA_PIN&0x80 ) == 0); //RW=1,讀PD7,為0表示空閑;
LCD_EN_PORT &= LCD_EN_L; //EN=0
LCD_DATA_DDR |= 0xf0;
}
/*-------------------------------------------
void LCD_init(void)
說明:LCD初始化設置
---------------------------------------------*/
void LCD_init (void)
{
_delay_ms(50);
LCD_write(0x30,0);
_delay_ms(6);
LCD_write(0x30,0);
_delay_ms(1);
LCD_write(0x30,0);
_delay_ms(1);
LCD_write(0x02,0);
_delay_ms(1);
LCD_write(0x28,0); //4bit Display
_delay_ms(1);
LCD_write(0x08,0); // 顯示關(guān)閉
_delay_ms(1);
LCD_write(0x01,0); // 顯示清屏
_delay_ms(1);
LCD_write(0x06,0); // 顯示光標移動設置
_delay_ms(1);
LCD_write(0x0c,0); // 顯示開及光標設置
_delay_ms(30);
}
/*-----------------------------------------------------
void LCD_set_xy(uint8_t x,uint8_t y)
說明:設置LCD起始顯示位置
顯示字符串的位置,X:0-15,Y:0-1
LCD第一行顯示寄存器地址:0X80-0X8F
LCD第一行顯示寄存器地址:0XC0-0XCF
-------------------------------------------------------*/
void LCD_set_xy(uint8_t x,uint8_t y)
{
uint8_t address;
if (y == 0)
address = 0x80 + x; //第一行顯示
else
address = 0xc0 + x; //第二行顯示
LCD_write( address, 0 );
}
/*-------------------------------------------------------
void LCD_write_string(uint8_t x,uint8_t y,char *s)
說明: 英文字符串顯示函數(shù)
輸入?yún)?shù):*s(英文字符串指)
X、Y : 顯示字符串的位置
---------------------------------------------------------*/
void LCD_write_string(uint8_t x,uint8_t y,char *s)
{
LCD_set_xy( x, y );
while (*s)
{
LCD_write( 0, *s );
s ++;
}
}
/*-----------------------------------------------------------
void LCD_self_def(uint8_t add,uint8_t *table)
說明:把自定義的點陣符號數(shù)據(jù)寫入CGRAM中
1 ADD為數(shù)據(jù)的地址;
2 table為相應的數(shù)據(jù)
-------------------------------------------------------------*/
void LCD_self_def(uint8_t add,uint8_t table[])
{
uint8_t i;
for (i=0;i<8;i++)
{
LCD_write(add+i,0); // 設置自定義字符的 CGRAM 地址
_delay_ms(2);
LCD_write(0,table[i]); // 向CGRAM寫入自定義字符表的數(shù)據(jù)
_delay_ms(2);
};
}
/*------------------------------------------------------------
void LCD_write_char(uint8_t x,uint8_t y,uint8_t c)
說明:向LCD寫一個字符;
x,y為坐標 ;c 為所寫字符
--------------------------------------------------------------*/
void LCD_write_char(uint8_t x,uint8_t y,uint8_t c)
{
LCD_set_xy(x,y );
_delay_ms(2);
LCD_write(0,c);
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -