本文件可以完美驅(qū)動(dòng)1602液晶屏,調(diào)用方法詳見: http://www.zg4o1577.cn/bbs/dpj-24670-1.html ,有2個(gè)文件1602.c和1602.h頭文件(在后面).
1602.c文件
//*****************************
#include<stc12c5a.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define IODATA P0
//這三個(gè)引腳參考資料
sbit E=P2^7; //1602使能引腳
sbit RW=P2^6; //1602讀寫引腳
sbit RS=P2^5; //1602數(shù)據(jù)/命令選擇引腳
/********************************************************************
* 名稱 : delay()
* 功能 : 延時(shí)
* 輸入 : 無
* 輸出 : 無
***********************************************************************/
void delay1602()
{
_nop_();
_nop_();
}
void Delay1602(uint del)
{
uint i,j;
for(i=0;i<del;i++)
for(j=0;j<=148;j++)
{
}
}
/********************************************************************
* 名稱 : bit Busy(void)
* 功能 : 這個(gè)是一個(gè)讀狀態(tài)函數(shù),讀出函數(shù)是否處在忙狀態(tài)
* 輸入 : 輸入的命令值
* 輸出 : 無
***********************************************************************/
void Busy(void)
{
bit busy_flag = 1;
IODATA = 0xff;
RS = 0;
delay1602();
RW = 1;
delay1602();
E = 1;
//Delay(1);
while(1)
{
busy_flag = (bit)(IODATA & 0x80);
if(busy_flag == 0)
{
break;
}
}
E = 0;
}
/********************************************************************
* 名稱 : wcmd(uchar del)
* 功能 : 1602命令函數(shù)
* 輸入 : 輸入的命令值
* 輸出 : 無
***********************************************************************/
void wcmd(uchar del)
{
RS = 0;
delay1602();
RW = 0;
delay1602();
E = 0;
delay1602();
IODATA = del;
delay1602();
E = 1;
delay1602();
E = 0;
}
/********************************************************************
* 名稱 : wdata(uchar del)
* 功能 : 1602寫數(shù)據(jù)函數(shù)
* 輸入 : 需要寫入1602的數(shù)據(jù)
* 輸出 : 無
***********************************************************************/
void lcd1602_write_data(uchar del)
{
Busy();
delay1602();
RS = 1;
delay1602();
RW = 0;
delay1602();
E = 0;
delay1602();
IODATA = del;
delay1602();
E = 1;
delay1602();
E = 0;
}
/********************************************************************
* 名稱 : L1602_init()
* 功能 : 1602初始化,請(qǐng)參考1602的資料
* 輸入 : 無
* 輸出 : 無
***********************************************************************/
void lcd1602_init(void)
{
Delay1602(15);
wcmd(0x38);
Delay1602(5);
wcmd(0x38);
Delay1602(5);
wcmd(0x38);
wcmd(0x38);
Busy();
wcmd(0x08);
Busy();
wcmd(0x01);
Busy();
wcmd(0x06);
Busy();
wcmd(0x0c);
}
/********************************************************************
* 名稱 : L1602_char(uchar hang,uchar lie,char sign)
* 功能 : 改變液晶中某位的值,如果要讓第一行,第五個(gè)字符顯示"b" ,調(diào)用該函數(shù)如下
L1602_char(1,5,'b')
* 輸入 : 行,列,需要輸入1602的數(shù)據(jù)
* 輸出 : 無
***********************************************************************/
void lcd1602_char(uchar hang,uchar lie,char sign)
{
uchar a;
if(hang == 1)
{
a = 0x80;
}
if(hang == 2)
{
a = 0xc0;
}
a = a + lie - 1;
Busy();
wcmd(a);
Busy();
lcd1602_write_data(sign);
}
/********************************************************************
* 名稱 : L1602_string(uchar hang,uchar lie,uchar *p)
* 功能 : 改變液晶中某位的值,如果要讓第一行,第五個(gè)字符開始顯示"ab cd ef" ,調(diào)用該函數(shù)如下
L1602_string(1,5,"ab cd ef;")
* 輸入 : 行,列,需要輸入1602的數(shù)據(jù)
* 輸出 : 無
***********************************************************************/
void lcd1602_string(uchar hang,uchar lie,uchar *p)
{
uchar a;
if(hang == 1)
{
a = 0x80;
}
if(hang == 2)
{
a = 0xc0;
}
a = a + lie - 1;
while(1)
{
Busy();
wcmd(a);
Busy();
lcd1602_write_data(*p);
a++;
p++;
if((*p == '\0')||(a==0x90)||(a==0xd0))
{
break;
}
}
}
//定位光標(biāo)
void lcd1602_locate(unsigned char x,unsigned char y)
{ unsigned char aa;
if (x==1) aa=0x80;
if (x==2) aa=0x80+0x40;
Busy();
wcmd(aa+y-1);
Busy();
}
//*****************1602.h文件
#ifndef _1602_
#define _1602_
#define uchar unsigned char
#define uint unsigned int
void lcd1602_init(void);
void lcd1602_char(uchar hang,uchar lie,char sign);
void lcd1602_string(uchar hang,uchar lie,uchar *p);
void lcd1602_write_data(uchar del);
void lcd1602_locate(unsigned char x,unsigned char y);
#endif
|