基于51的LCD1602四數(shù)據(jù)線驅(qū)動(dòng)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
/這個(gè)程序已經(jīng)調(diào)試過(guò)了
//下面調(diào)用實(shí)例
//////////////////////////////////
//LCD1602讀寫(xiě)頭文件
//四線驅(qū)動(dòng)方式
//////////////////////////////////
#include
#include "LCD1602.h"
/*-----------
管腳定義在液晶頭文件中
-----------*/
void main(void){
LCD_init();
p=LCD_PutStr("Hello Lcd1602!n",-1); //顯示一段文字
p=LCD_PutNum(1234,2,p); //顯示12.34這個(gè)數(shù)
while(1);
}
/*---------------------------------------------------------
液晶LCD1602C 使用4條數(shù)據(jù)線(D4~D7)
-----------------------------------------------------------*/
/*-------------------------------------------------------------
LCD引腳定義
1---GND
2---VCC
3---VO
4---RS
5---RW
6---EN
7到14--D0-D7
15--背景燈+
16--背景燈-
-----------------------------------------------------------------*/
#include
#include
#define LCD_DATA P2
sbit LCD1602_RS=P2^2;
sbit LCD1602_EN=P2^3;
int p=0;
/*--------------------------------------------------------------------------------------------------
函數(shù)說(shuō)明
--------------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(unsigned char command) ;
void LCD_write_data(unsigned char Recdata);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata);
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
void delay_1us(void) //1us延時(shí)函數(shù)
{
_nop_();
}
void delay_nus(unsigned int n) //N us延時(shí)函數(shù)
{
unsigned int i=0;
for (i=0;i
}
void delay_1ms(void) //1ms延時(shí)函數(shù)
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時(shí)函數(shù)
{
unsigned int i=0;
for (i=0;i
}
void LCD_init(void) //液晶初始化
{
LCD_write_command(0x28);
delay_nus(40);
LCD_write_command(0x28);
delay_nus(40);
LCD_write_command(0x28);
delay_nus(40);
LCD_en_write();
delay_nus(40);
LCD_write_command(0x28); //4位顯示
LCD_write_command(0x0c); //顯示開(kāi)
LCD_write_command(0x01); //清屏
delay_nms(2);
}
void LCD_en_write(void) //液晶使能
{
LCD1602_EN=1;
delay_nus(1);
LCD1602_EN=0;
}
void LCD_write_command(unsigned char command) //寫(xiě)指令
{
delay_nus(16);
LCD1602_RS=0; //RS=0
LCD_DATA&=0X0f; //清高四位
LCD_DATA|=command&0xf0; //寫(xiě)高四位
LCD_en_write();
command=command<<4; //低四位移到高四位
LCD_DATA&=0x0f; //清高四位
LCD_DATA|=command&0xf0; //寫(xiě)低四位
LCD_en_write();
}
void LCD_write_data(unsigned char Recdata) //寫(xiě)數(shù)據(jù)
{
delay_nus(16);
LCD1602_RS=1; //RS=1
LCD_DATA&=0X0f; //清高四位
LCD_DATA|=Recdata&0xf0; //寫(xiě)高四位
LCD_en_write();
Recdata=Recdata<<4; //低四位移到高四位
LCD_DATA&=0X0f; //清高四位
LCD_DATA|=Recdata&0xf0; //寫(xiě)低四位
LCD_en_write();
}
void LCD_set_xy( unsigned char x, unsigned char y ) //寫(xiě)地址函數(shù)
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command(address);
}
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata) //列x=0~15,行y=0,1
{
LCD_set_xy(X, Y); //寫(xiě)地址
LCD_write_data(Recdata);
}
int LCD_PutStr(unsigned char *DData,int pos){
unsigned char i;
if(pos==-1){
LCD_write_command(0x01); //清屏
delay_nms(2);
pos=0;
}
while((*DData)!='