📄 5_自制单片机之五(2)……lcd1602的驱动_边学边秀单片机.htm
字号:
}
#usrbar {
LETTER-SPACING: normal
}
#usrbar A {
LETTER-SPACING: normal
}
#usrbar A:link {
LETTER-SPACING: normal
}
#usrbar A:visited {
LETTER-SPACING: normal
}
#ft {
LETTER-SPACING: normal
}
#ft A {
LETTER-SPACING: normal
}
#ft A:link {
LETTER-SPACING: normal
}
#ft A:visited {
LETTER-SPACING: normal
}
</STYLE>
<!-- 登陆状态条 -->
<DIV id=usrbar><NOBR><A href="http://www.baidu.com/" target=_blank>百度首页</A> | <A
id=hi_index href="http://hi.baidu.com/" target=_blank>百度空间</A>
<SCRIPT type=text/javascript>
document.write('| <a href="http://passport.baidu.com/?login&tpl=sp&tpl_reg=sp&u=http://hi.baidu.com' + encodeURI('/txz01/blog/item/aff17b385cd50f26b9998f86%2Ehtml') + '">登录</a>');
</SCRIPT>
</NOBR></DIV>
<DIV id=main align=left><!--[if IE]>
<SCRIPT>
var objmain = document.getElementById("main");
function updatesize(){ var bodyw = window.document.body.offsetWidth; if(bodyw <= 790) objmain.style.width="772px"; else if(bodyw >= 1016) objmain.style.width="996px"; else objmain.style.width="100%"; }
updatesize(); window.onresize = updatesize;
</SCRIPT>
<![endif]-->
<DIV id=header>
<DIV class=lc>
<DIV class=rc></DIV></DIV>
<DIV class=tit><A class=titlink title="txz001的空间 http://hi.baidu.com/txz01"
href="http://hi.baidu.com/txz01">边学边秀单片机</A></DIV>
<DIV class=desc>学习单片机,要的是有动手的欲望。这样你才能在单片机的世界里累并快乐着。</DIV>
<DIV id=tabline></DIV>
<DIV id=tab><A href="http://hi.baidu.com/txz01">主页</A><A class=on
href="http://hi.baidu.com/txz01/blog">博客</A><A
href="http://hi.baidu.com/txz01/album">相册</A><SPAN>|</SPAN><A
href="http://hi.baidu.com/txz01/profile">个人档案</A> <SPAN>|</SPAN><A
href="http://hi.baidu.com/txz01/friend">好友</A> </DIV></DIV>
<DIV class=stage>
<DIV class=stagepad>
<DIV style="WIDTH: 100%">
<TABLE class=modth cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=modtl width=7> </TD>
<TD class=modtc noWrap>
<DIV class=modhead><SPAN class=modtit>查看文章</SPAN></DIV></TD>
<TD class=modtc noWrap align=right></TD>
<TD class=modtr width=7> </TD></TR></TBODY></TABLE>
<DIV class=modbox id=m_blog>
<DIV class=tit>5.自制单片机之五(2)……LCD1602的驱动</DIV>
<DIV class=date>2007-08-25 22:14</DIV>
<TABLE style="TABLE-LAYOUT: fixed">
<TBODY>
<TR>
<TD>
<DIV class=cnt id=blog_text>
<P>具体电路的制作是很简单的,就接了两个电阻,一个是10欧姆的背光限流电阻,另一个是2K的LCD极板电压调节电阻。这两个电阻的阻值怎么定呢?背光比较简单,它就相当于在后面接了几个发光二极管,任何时候你只要在15、16脚串上个100欧的电位器接上电源,调节电位器,觉得亮度合适。此时的阻值便可。LCD液晶极板驱动电压调节电阻的确定就稍微麻烦一点。在各数据线,控制线接好关通上电源的前提下在第3脚(VEE)和地之间接一个10K的电位器。调节电位器。当3脚电压高时为全亮,电压为0时为全暗(液晶全显示为黑块)。你用电位器把屏幕从全暗刚好调到变亮。这时便可调试程序。待屏幕能正确显示后再细调电位器,使对比度合适。这时的阻值便可确定,然后换成等值的固定电阻焊上便可。</P>
<P>我们接着上次的系统板制做:</P>
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/2adf9cfc4641328bb801a03e.jpg"
border=0 small="0"></DIV>
<DIV forimg="1"></DIV>
<DIV forimg="1">新买的1602LCD,20元,贵不?</DIV>
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/80c14c30790b2290a8018e57.jpg"
border=0 small="0"></DIV>
<DIV forimg="1">反面:</DIV>
<DIV forimg="1">
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/1060fa01df39f40f1c958351.jpg"
border=0 small="0"></DIV>
<DIV forimg="1">组装后:
<DIV forimg="1">
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/f17be1b7641fc7fd30add1a5.jpg"
border=0 small="0"></DIV></DIV>
<DIV forimg="1">具体电路图:
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/0ee7fbd30aa08a3d960a16a7.bmp"
border=0 small="0"></DIV>
<DIV forimg="1">接口说明:
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/05b282134132490b5baf53a1.jpg"
border=0 small="0"></DIV></DIV></DIV>
<DIV forimg="1">运行:</DIV>
<DIV forimg="1">
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/b926faec0b45db2e63d09f3e.jpg"
border=0 small="0"></DIV>
<DIV forimg="1">用户自定义字符的应用:</DIV></DIV></DIV>
<DIV forimg="1">
<DIV forimg="1"></DIV>
<DIV forimg="1"></DIV>
<DIV
forimg="1">我们从CGROM表上可以看到,在表的最左边是一列可以允许用户自定义的CGRAM,从上往下看着是16个,实际只有8个字节可用。它的字符码是00000000-00000111这8个地址,表的下面还有8个字节,但因为这个CGRAM的字符码规定0-2位为地址,3位无效,4-7全为零。因此CGRAM的字符码只有最后三位能用也就是8个字节了。等效为0000X111,X为无效位,最后三位为000-111共8个。<BR>如果我们要想显示这8个用户自定义的字符,操作方法和显示CGROM的一样,先设置DDRAM位置,再向DDRAM写入字符码,例如“A”就是41H。现在我们要显示CGRAM的第一个自定义字符,就向DDRAM写入00000000B(00H),如果要显示第8个就写入00000111(08H),简单吧!<BR>好!现在我们来看怎么向这八个自定义字符写入字模。有个设置CGRAM地址的指令大家还记得吗?赶快再找出来看看。</DIV>
<DIV forimg="1">
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/959406fbd2e0a61f6c22eb7b.jpg"
border=0
small="0"></DIV><BR>从这个指令可以看出指令数据的高2位已固定是01,只有后面的6位是地址数据,而这6位中的高3位就表示这八个自定义字符,最后的3位就是字模数据的八个地址了。例如第一个自定义字符的字模地址为01000000-01000111八个地址。我们向这8个字节写入字模数据,让它能显示出“℃”<BR>地址:01000000 数据:00010000
图示:○○○■○○○○<BR> 01000001 00000110
○○○○○■■○<BR> 01000010 00001001
○○○○■○○■<BR> 01000011 00001000
○○○○■○○○<BR> 01000100 00001000
○○○○■○○○<BR> 01000101 00001001
○○○○■○○■<BR> 01000110 00000110
○○○○○■■○<BR> 01000111 00000000
○○○○○○○○<BR>下面我们写一段程序让这8个自定义字符显示出一个心的图案:<BR># include
<reg51.h><BR>unsigned char
table1[]={0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f,<BR>
0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,<BR>
0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,<BR>
0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E,<BR>
0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00,<BR>
0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01,<BR>
0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00,<BR>
0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00};//心图案<BR>unsigned char
table[]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};//字符℃
<DIV class=tmpDiv></DIV></DIV>
<DIV forimg="1">#define
CLEARSCREEN LCD_write_command(0x01)</DIV>
<DIV forimg="1">/**************定义接口************************/</DIV>
<DIV forimg="1">#define
LCDIO P2<BR>sbit
LCD1602_RS=P3^0; <BR>sbit
LCD1602_RW=P3^1; <BR>sbit
LCD1602_EN=P3^2; </DIV>
<DIV forimg="1">/**************定义函数************************/<BR>void
LCD_write_command(unsigned char command);//写入指令函数<BR>void
LCD_write_dat(unsigned char dat);//写入数据函数<BR>void LCD_set_xy( unsigned
char x, unsigned char y );//设置显示位置函数<BR>void LCD_dsp_char( unsigned
x,unsigned char y,unsigned char dat);//显示一个字符函数<BR>void
LCD_dsp_string(unsigned char X,unsigned char Y,unsigned char
*s);//显示字符串函数<BR>void LCD_init(void);//初始化函数<BR>void delay_nms(unsigned
int n);//延时函数<BR>/********************************************/</DIV>
<DIV forimg="1">/************初始化函数****************/<BR>void
LCD_init(void)<BR>{ <BR>CLEARSCREEN;//clear screen
<BR>LCD_write_command(0x38);//set 8 bit data transmission mode
<BR>LCD_write_command(0x0c);//open display (enable lcd
display)<BR>LCD_write_command(0x80);//set lcd first display address
<BR>CLEARSCREEN;//clear
screen<BR>}<BR>/****************************************************/</DIV>
<DIV
forimg="1">/**************写指令函数********************************/ <BR>void
LCD_write_command(unsigned char
command)<BR>{<BR>
LCDIO=command;<BR>
LCD1602_RS=0; <BR>
LCD1602_RW=0;<BR>
LCD1602_EN=0;<BR>
LCD1602_EN=1;<BR>
delay_nms(10);<BR>}<BR>/***************************************************/<BR>/****************写数据函数************************/<BR>void
LCD_write_dat(unsigned char
dat)<BR>{<BR>LCDIO=dat;<BR>LCD1602_RS=1;<BR>LCD1602_RW=0;<BR>LCD1602_EN=0;<BR>delay_nms(1);<BR>LCD1602_EN=1;<BR>}<BR>/****************************************************/</DIV>
<DIV forimg="1">/***************设置显示位置**************************/<BR>void
LCD_set_xy( unsigned char x, unsigned char y )<BR>{<BR>unsigned char
address;<BR>if (y == 1) <BR> address = 0x80 +
x;<BR>else <BR> address =0xc0+
x;<BR>LCD_write_command(address);
<BR>}<BR>/***************************************************/</DIV>
<DIV forimg="1">/****************显示一个字符**********************/<BR>void
LCD_dsp_char( unsigned x,unsigned char y,unsigned char
dat)<BR>{<BR>LCD_set_xy( x, y );
<BR>LCD_write_dat(dat);<BR>}<BR>/**********************************************/</DIV>
<DIV forimg="1">/***************显示字符串函数***************/<BR>void
LCD_dsp_string(unsigned char X,unsigned char Y,unsigned char
*s)<BR>{<BR> LCD_set_xy( X, Y );
<BR> while
(*s) <BR>
{<BR>
LCD_write_dat(*s); <BR>
s ++;<BR>
}<BR>}<BR>/***********************************************/</DIV>
<DIV forimg="1">/********** 延时**********************/<BR>void
delay_nms(unsigned int
n) <BR>{<BR>
unsigned int i=0,j=0;<BR> for
(i=n;i>0;i--)<BR> for
(j=0;j<10;j++); <BR>}<BR>/**************************************/</DIV>
<DIV forimg="1">/***********主函数**************/<BR>void
main(void)<BR>{<BR>unsigned char
i,j,k,tmp;<BR>LCD_init();<BR>delay_nms(100);<BR>tmp=0x40;//设置CGRAM地址的格式字<BR>k=0;<BR>for(j=0;j<8;j++)<BR>
{<BR>
for(i=0;i<8;i++)<BR>
{<BR>
LCD_write_command(tmp+i); // 设置自定义字符的 CGRAM
地址 <BR>
delay_nms(2);<BR>
LCD_write_dat(table1[k]); //
向CGRAM写入自定义字符表的数据<BR>
k++;<BR>
delay_nms(2);<BR>
}<BR>
tmp=tmp+8;<BR> }<BR>
LCD_dsp_string(1,1,"LCD TEST
");//在第一行第一列显示“LCD TEST”<BR>
LCD_dsp_string(1,2,"SUCCESSFUL
");//在第二行第一列显示“SUCCESSFUL”<BR> for
(i=0;i<4;i++)<BR>
{<BR> LCD_dsp_char(
12+i,1,i);//在第一行第12列位置显示心图案的上半部<BR>
delay_nms(1);<BR>
}<BR> for
(i=4;i<8;i++)<BR>
{<BR> LCD_dsp_char(
12+i-4,2,i);在第二行第12列位置显示心图案的下半部<BR>
delay_nms(1);<BR>
}<BR> while
(1);<BR>}<BR>/********************************************************************/</DIV>
<DIV forimg="1">实际效果如图:</DIV>
<DIV forimg="1">
<DIV forimg="1">
<DIV forimg="1">
<DIV forimg="1"><IMG class=blogimg
src="5_自制单片机之五(2)……LCD1602的驱动_边学边秀单片机_files/1060fa0104398b0f1c958351.jpg"
border=0 small="0"></DIV>
<DIV class=tmpDiv></DIV></DIV>
<DIV class=tmpDiv></DIV></DIV>
<DIV class=tmpDiv></DIV></DIV></DIV></DIV></DIV></TD></TR></TBODY></TABLE><BR>
<DIV class=opt><A title=查看该分类中所有文章
href="http://hi.baidu.com/txz01/blog/category/c51µ¥Æ¬»ú">类别:c51单片机</A> | <A
title=将此文章添加到百度搜藏 onclick="return addToFavor();"
href="http://cang.baidu.com/do/add" target=_blank>添加到搜藏</A> | 浏览(<SPAN
id=result></SPAN>) | <A
href="http://hi.baidu.com/txz01/blog/item/aff17b385cd50f26b9998f86.html#send">评论</A> (3)
<SCRIPT language=javascript>
/*<![CDATA[*/
var pre = [true,'5.自制单片机之五(1)……LCD1602的驱动', '5.自制单片机之五(1)……LCD16...','/txz01/blog/item/4807ca257515bc6235a80f45.html'];
var post = [true,'6.自制单片机之六……LCD12864的驱动','6.自制单片机之六……LCD12864的...', '/txz01/blog/item/c6ff4e954da0eb0a7af480bc.html'];
if(pre[0] || post[0]){
document.write('<div style="height:5px;line-height:5px;"> </div><div id="in_nav">');
if(pre[0]){
document.write('上一篇:<a href="' + pre[3] + '" title="' + pre[1] + '">' + pre[2] + '</a> ');
}
if(post[0]){
document.write('下一篇:<a href="' + post[3] + '" title="' + post[1] + '">' + post[2] + '</a>');
}
document.write('</div>');
}
/*]]>*/
</SCRIPT>
</DIV>
<DIV class=line></DIV>
<STYLE type=text/css>#in_related_doc A {
TEXT-DECORATION: none
}
</STYLE>
<DIV id=in_related_tmp></DIV>
<SCRIPT language=javascript type=text/javascript>
/*<![CDATA[*/
function HI_MOD_IN_RELATED_DOC_CALLBACK(arg){
if(arg.length <= 1) return false;
var hasMore = arg[0];
var D=function(A,B){A[A.length]=B;}
if(arg.length % 2 == 0) D(arg, ["","","",""]);
var html = ['<div id="in_related_doc"><div class="tit">相关文章:</div>'];
D(html, '<table cellpadding="0" cellspacing="3" border="0">');
for(var i = 1, j = arg.length; i < j; i += 2){
D(html, '<tr>');
D(html, '<td width="15px"><a style="font-size:25px" >•</a></td><td><a href="http://hi.baidu.com/' + arg[i][3] + '/blog/item/' + arg[i][2] + '.html" target="_blank" title="' + arg[i][0] + '">' + arg[i][1] + '</a>');
D(html, new Array(10).join('\u3000'));
D(html, '</td>');
if(arg[i + 1][0] != "")
D(html, '<td width="15px"><a style="font-size:25px" >•</a></td><td><a href="http://hi.baidu.com/' + arg[i + 1][3] + '/blog/item/' + arg[i + 1][2] + '.html" target="_blank" title="' + arg[i + 1][0] + '">' + arg[i + 1][1] + '</a></td>');
else
D(html, '<td> </td><td> </td>');
D(html, '</tr>');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -