51單片機(jī)lcd1602的驅(qū)動程序
/*
********************************************
**頭文件名:LCD液晶驅(qū)動
** Target : AT89s52
** Crystal: 11.0952
*********************************************
*/
#ifndef _LCD_51_H_
#define _LCD_51_H_
#include "DELAY_51.H"
#include
/*
------80s52與LCD連接信息-----------------------------------------
**P1_2 ->RS
**P1_3 ->EN
**P1_4 ->D4
**P1_5 ->D5
**P1_6 ->D6
**P1_7 ->D7 6根線
** 注意本驅(qū)動配合delay_51.h使用
------------------------------------------------------------------
*/
//=============使用本驅(qū)動,改變下面配置信息即可==================
#define uint unsigned int
#define uchar unsigned char
#define RS_SET P1_2 = 1
#define RS_CLR P1_2 = 0
#define EN_SET P1_3 = 1
#define EN_CLR P1_3 = 0
#define DB P1
//=================================================================
//===================函數(shù)說明======================================
void LCD_en_write(void); //使能
void LCD_write_command(uchar command); //寫指令
void LCD_write_data(uchar DATA); //寫數(shù)據(jù)
void LCD_init( void ); //初始化
void LCD_set_rc( uchar r, uchar c ); //寫位置
void LCD_write_string(uchar r, uchar c, uchar *s); //寫字符串
void LCD_write_char(uchar r, uchar c, uchar DATA); //寫字符
void LCD_write_chardata( uchar r, uchar c, uchar chardata[]); //寫整型字符n
void LCD_write_datafloat( uchar r, uchar c, float datafloat, uint p, uint mm); //寫一個浮點數(shù)年
void LCD_write_datalongint( uchar r, uchar c, long int dataint); //寫一個整數(shù)
void LCD_write_array( uchar r, uchar c, uchar array[] ); //寫字符串
//==================================================================
#endif //
#define _LCD_51_C_
#include "LCD_51.h"
//==========================================
//函數(shù)名稱:液晶模塊使能
//函數(shù)作用: 使能液晶
//入口參數(shù): 無
//返回類型:無
//==========================================
void LCD_en_write(void)
{
EN_SET;
delay_nus(4);
EN_CLR;
}
//==========================================
//函數(shù)名稱:寫指令
//函數(shù)作用:
//入口參數(shù): 液晶指令command
//返回類型:無
//==========================================
void LCD_write_command(uchar command)
{
delay_nus( 16 );
RS_CLR;
DB &= 0X0F; //清高四位
DB |= command & 0xf0; //寫高四位
LCD_en_write();
command=command << 4; //低四位移到高四位
DB &= 0x0f; //清高四位
DB |= command & 0xf0; //寫低四位
LCD_en_write();
//delay_nms(2); //清屏指令時間很長, 保險起見,可以增加
}
//==========================================
//函數(shù)名稱:寫數(shù)據(jù)
//函數(shù)作用:
//入口參數(shù): 數(shù)據(jù)DATA
//返回類型:無
//==========================================
void LCD_write_data(uchar DATA)
{
delay_nus(16);
RS_SET; //RS=1
DB &= 0X0f; //清高四位
DB |= DATA&0xf0; //寫高四位
LCD_en_write();
DATA = DATA << 4; //低四位移到高四位
DB &= 0X0f; //清高四位
DB |= DATA&0xf0; //寫低四位
LCD_en_write();
}
//==========================================
//函數(shù)名稱:液晶初始化函數(shù)
//函數(shù)作用:
//入口參數(shù): 無
//返回類型:無
//==========================================
void LCD_init( void )
{
LCD_write_command(0x28);
LCD_en_write(); //********總結(jié)這里一定要加這液晶使能*********
delay_nus(20);
LCD_write_command(0x28); //4位顯示
LCD_write_command(0x0c); //顯示開
LCD_write_command(0x01); //清屏
delay_nms(5); //延時一定要長
}
//==========================================
//函數(shù)名稱:寫地址
//函數(shù)作用:
//入口參數(shù): 行位置r, 列位置c
//返回類型:無
//==========================================
void LCD_set_rc( uchar r, uchar c )
{
uchar address;
if (r == 1)
address = 0x80 + c;
else if(r == 2)
address = 0xc0 + c;
LCD_write_command( address );
}
//==========================================
//函數(shù)名稱:寫字符串
//函數(shù)作用: 在指定位置寫一串字符串
//入口參數(shù): 行位置r, 列位置c, 字符串*s
//返回類型:無
//==========================================
void LCD_write_string(uchar r, uchar c, uchar *s)
{
LCD_set_rc( r, c ); //寫地址
while( *s ) // 寫顯示字符
{
LCD_write_data( *s );
s++;
}
}
void LCD_write_char(uchar r, uchar c, uchar DATA) //寫字符
{
LCD_set_rc( r, c );
LCD_write_data( DATA );
}
//==========================================
//函數(shù)名稱:寫字符數(shù)組
//函數(shù)作用: 在指定位置寫一串字符串
//入口參數(shù): 行位置r, 列位置c, 字符數(shù)組chardata[], 字符個數(shù)
//返回類型:無
//==========================================
void LCD_write_array( uchar r, uchar c, uchar array[] )
{
uchar i = 0;
while( array[i] != '