1602相關(guān)知識及四線驅(qū)動程序
今天在調(diào)1602顯示的時候又費(fèi)了點(diǎn)小周折,由于平常我們一般是對液晶進(jìn)行寫操作,所以在有的開發(fā)板上這個引腳直接是接地的,今天自己用杜邦線連了個1602忽略了這個問題,后來接地就好了。
數(shù)據(jù)傳輸?shù)乃木€接法和八線接法差別在于寫數(shù)據(jù)的順序,下面附上四線接法的程序。
[cpp] view plaincopy/*--------------------------------------------------------------*/
//Name: LCD1602四線驅(qū)動程序
//File: LCD1602_4.H
//Date: 11-2-9
//TimE: 12:42
//Ver: 0.1
/*--------------------------------------------------------------*/
#include "reg51.h"
#include
//LCD1602接口定義
#define LCD_DATA P1 //P1口(P14~P17)與LCD高四位(D4~D7)對應(yīng)相接
sbit LCD1602_RS=P1^2; //數(shù)據(jù)指令
sbit LCD1602_EN=P1^3; //使能
//延時函數(shù),12mhz 6t延遲時間10*255us
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);
//以下函數(shù)用于輸出字符串和數(shù)字
int LCD_PutNum(unsigned long num,int XS,int pos);
int LCD_PutStr(unsigned char *DData,int pos);
//-------------------------1us延時函數(shù)---------------------------------
void delay_1us(void)
{
_nop_();
}
//------------------------N us延時函數(shù)---------------------------------
void delay_nus(unsigned int n)
{
unsigned int i=0;
for (i=0;i
delay_1us();
}
//-----------------------1ms延時函數(shù)----------------------------------
void delay_1ms(void)
{
unsigned int i;
for (i=0;i<1140;i++);
}
//-----------------------N ms延時函數(shù)---------------------------------
void delay_nms(unsigned int n)
{
unsigned int i=0;
for (i=0;i
delay_1ms();
}
//------------------------液晶初始化-----------------------------------
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); //顯示開
LCD_write_command(0x01); //清屏
delay_nms(2);
}
//----------------------液晶使能函數(shù)-------------------------------------
void LCD_en_write(void)
{ //EN由高電平跳變到低電平時液晶使能
LCD1602_EN=1;
delay_nus(1);
LCD1602_EN=0;
}
//------------------------寫指令函數(shù)--------------------------------------
void LCD_write_command(unsigned char command)
{
delay_nus(16);
LCD1602_RS=0; //RS=0
LCD_DATA &=0X0f; //清高四位
LCD_DATA|=command&0xf0; //寫高四位
LCD_en_write();
command=command<<4; //低四位移到高四位
LCD_DATA&=0x0f; //清高四位
LCD_DATA|=command&0xf0; //寫低四位
LCD_en_write();
}
//------------------------寫數(shù)據(jù)函數(shù)--------------------------------------
void LCD_write_data(unsigned char Recdata)
{
delay_nus(16);
LCD1602_RS=1; //RS=1
LCD_DATA&=0X0f; //清高四位
LCD_DATA|=Recdata&0xf0; //寫高四位
LCD_en_write();
Recdata=Recdata<<4; //低四位移到高四位
LCD_DATA&=0X0f; //清高四位
LCD_DATA|=Recdata&0xf0; //寫低四位
LCD_en_write();
}
//-----------------------地址定位函數(shù)-------------------------------------
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command(address);
}
//----------------------在某個地址處,寫一個字符----------------------------[!--empirenews.page--]
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata) //列x=0~15,行y=0,1
{
LCD_set_xy(X, Y); //寫地址
LCD_write_data(Recdata);
}
//----------------------------輸出字符串-------------------------------------
int LCD_PutStr(unsigned char *DData,int pos) //pos表示字符顯示位置,0~31
{
unsigned char i;
if(pos==-1)
{
LCD_write_command(0x01); //清屏
delay_nms(2);
pos=0;
}
while((*DData)!='