LCD1602.H
- #ifndef __LCD1604__H
- #define __LCD1604__H
- #include "def.h"
- //行控制碼1602&1604通用
- #define LINE0 0x80
- #define LINE1 0xC0
- #define LINE2 0x90
- #define LINE3 0xD0
-
- #ifdef _USE_SHOP_A_
- //P2口接LCD的D0~D7
- #define LCD1602_DATA P2
- //LCD的RS、RW、E腳
- sbit LCD1602_RS = P0^4;
- sbit LCD1602_E = P0^5;
- sbit LCD1602_RW = P0^6;
- //與LCD12864用同一P2口
- //置PSB=1插上也能亮
- sbit PSB = P0^7;
- #endif
- #ifdef _LY5A_L2A_
- sbit BEEP = P1^5;
- #define LED_PORT P2
- //P2口接LCD的D0~D7
- #define LCD1602_DATA P0
- //LCD的RS、RW、E腳
- sbit LCD1602_RS = P1^0;
- sbit LCD1602_RW = P1^1;
- sbit LCD1602_E = P1^2;
- //與LCD12864用同一P2口
- //置PSB=1插上也能亮
- sbit PSB = P1^6;
- #endif
- //LCD初如化
- void LcdInit();
- //LCD清屏
- void LcdClr();
- //清某一行
- void LcdClr0();
- void LcdClr1();
- //void LcdClr2();
- //void LcdClr3();
- /*****************************************************************************************
- * 在指定坐標顯示整型數字
- *------------------------
- * x,y : 橫縱屏幕坐標(列,行)
- * num : 要顯示的整型數字
- * nDispW: 要顯示的占位寬度(0~16個字符),0-基本的顯示,不設定(后兩位參就無意義了)
- * bRightAlign : 0-左對齊,1-右對齊
- * bLeftAdd0 : 右對齊時的數字前導空,左邊是否補0,0-不補0,1-補0
- *******************************************************************************************/
- void LcdDisplayNum(u8 x, u8 y, const long num, u8 nDispW , bool bRightAlign, bool bLeftAdd0);
- /**********************************************
- * 在指定(列,行)位置顯示字符串
- **********************************************/
- void LcdDisplayStr(u8 x, u8 y, const char *msg);
- #endif
復制代碼
LCD1604.c
- ///////////////////////////////////////////////////////////////////////////////////////
- // LCD1602&LCD1604字符顯示功能
- //
- // LCD1602_RW 不用功能電路不接入這個腳
- //-----------------------------------------------------------------------------------
- #include <string.h>
- #include <intrins.h>
- #include "LCD1602.h"
- void delay1Cycle(unsigned int cycle)
- {
- unsigned int i;
- for(i=0; i<cycle; i++)
- {
- _nop_();
- }
- }
- /*OK
- bool LcdBusy()
- {
- bit b;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- delay1Cycle(5);
- LCD1602_E = 1;
- delay1Cycle(5);
- b = LCD1602_DATA & 0x80;
- LCD1602_E = 0;
-
- return b;
- }
- */
- //------------------------------------------------
- //LCD E口跳變時,LCD顯示一個字符/接收一個指令
- //RS=0:命令指令,RS=1:顯示數據指令
- //------------------------------------------------
- void LcdWriteInfo( u8 RS, u8 RW, u8 dat )
- {
- //while(LcdBusy());
-
- LCD1602_RS = RS;
- LCD1602_E = 0;
- LCD1602_RW = RW;
-
- LCD1602_DATA = dat;
- delay1Cycle(5);
-
- LCD1602_E = 1;
- delay1Cycle(5);
- LCD1602_E = 0;
- delay1Cycle(25);
- }
- //---------------------------
- //RS=0,指令寄存器
- //RW=0,寫數據
- //---------------------------
- void LcdWriteCom( u8 com )
- {
- LcdWriteInfo( 0, 0, com );
- }
- //--------------------------
- //RS=1,數據寄存器
- //--------------------------
- void LcdWriteData(u8 dat)
- {
- LcdWriteInfo( 1, 0, dat );
- }
- //---------------
- //LCD初如化
- //---------------
- void LcdInit()
- {
- //要等待開機啟穩
- //否則顯示錯位不全
- delay1Cycle(1000);
- PSB = 1; //與開發板LCD12864同一數據口
- //讓12864插上也能有字,但...
- LcdWriteCom(0x38); //開顯示:0011 1000
- LcdWriteCom(0x38); //剛開機,各方電位還不穩定
- LcdWriteCom(0x38); //重要的事情說三便
- LcdWriteCom(0x0c); //不顯示光標:0000 1100
- LcdWriteCom(0x06); //光標右移:0000 0110
- LcdWriteCom(0x01); //清屏
- //別著急,抽根煙
- delay1Cycle(1000);
- }
- //---------------
- //LCD清屏
- //---------------
- /*OK
- void LcdClr()
- {
- LcdWriteCom(0x01); //清屏
- }
- */
- /**********************************************
- * 在指定位置顯示字符串
- **********************************************/
- void LcdDisplayStr(u8 x, u8 y, const char *msg)
- {
- char *p1 = msg;
- char *p2 = 0;
- u8 xx, nlen=0;;
-
- if( x>15 || y>3 ) return;
- //取顯示字串長度
- nlen = strlen(msg);
- if(nlen==0) return;
- if(nlen>32) nlen=32;
- //p2-折行字串指針
- //只顯示一個折行
- if(nlen<=16-x)
- p2 = 0; //字串只用顯示一行
- else
- p2 = p1 +16 - x; //字串長度過長有折行
-
- //顯示列控制碼
- if(y==0) LcdWriteCom(LINE0+x); //第一行顯示
- if(y==1) LcdWriteCom(LINE1+x); //第二行
- if(y==2) LcdWriteCom(LINE2+x); //第三行
- if(y==3)
- { p2 = 0; LcdWriteCom(LINE3+x);} //第四行
-
- for(xx = x; xx<16; xx++)
- {
- LcdWriteData( *p1 );
- p1++;
- if(*p1=='\0') break;
- }
-
- //有折行時
- if(p2!=0)
- {
- if(y==0) LcdWriteCom(LINE1);
- if(y==1) LcdWriteCom(LINE2);
- if(y==2) LcdWriteCom(LINE3);
- for(xx = 0; x<16; xx++)
- {
- LcdWriteData( *p2 );
- p2++;
- if(*p2=='\0') break;
- }
- }
- }
- /*****************************************************************************************
- * 在指定坐標顯示整型數字
- *------------------------
- * x,y : 橫縱屏幕坐標
- * num : 顯示整型數字
- * nDispW: 顯示占位寬度,當寬度小于顯示數字寬度時,此值無效
- * bRightAlign : 0-左對齊,1-右對齊
- * bLeftAdd0 : 數字前導空左邊是否補0,0-不補0,1-補0
- *******************************************************************************************/
- void LcdDisplayNum(u8 x, u8 y, const long num, u8 nDispW, bool bRightAlign, bool bLeftAdd0)
- {
- u8 w=0, i=0;
- char num_str[17]={'*'};
- char strBuff[17]={0x20};
- long tmp = 0;
-
- if(x>15 || y>3) return;
- if(nDispW>16) nDispW=16;
- //負數不支持左補0
- if(num<0) bLeftAdd0 = 0;
- tmp = num>0 ? num : num * (-1);
- //取字長度,數字到臨時字符串中
- while(1)
- {
- w++;
- strBuff[i] = tmp%10 + '0';
- if(tmp<10) break;
- tmp = tmp/10;
- i++;
- }
- //負號(-)
- if(num<0) {strBuff[i+1]='-';w++;}
- if(nDispW<w) nDispW=0;
-
- //寬度內填空格
- if(nDispW>0)
- {
- for(i=0; i<nDispW; i++)
- num_str[i] = 0x20;
- }
- //左補0的條件:給定寬度是否右對齊
- if(bLeftAdd0==1 && nDispW>0 && bRightAlign==1 )
- {
- for(i=0; i<nDispW; i++)
- num_str[i] = '0';
- }
-
- if(nDispW>0)
- {
- if(bRightAlign)
- memcpy(num_str, strBuff, w);
- else
- memcpy(num_str+nDispW-w, strBuff, w);
- }
- else
- {
- memcpy(num_str, strBuff, w);
- }
-
- //應以最寬的值
- w = (nDispW > w ? nDispW : w);
- if(y==0) LcdWriteCom(LINE0+x);
- if(y==1) LcdWriteCom(LINE1+x);
- if(y==2) LcdWriteCom(LINE2+x);
- if(y==3) LcdWriteCom(LINE3+x);
-
- for( i=w; i>0; i-- )
- {//LCD顯示
- LcdWriteData( num_str[i-1] );
- }
- }
- /********************
- *清行
- ********************
- OK
- void LcdClrLine(u8 y)
- {
- u8 x;
-
- if(y>3) return;
- //
- if(y==0) LcdWriteCom(LINE0); //第一行
- if(y==1) LcdWriteCom(LINE1); //第二行
- if(y==2) LcdWriteCom(LINE2); //第三行
- if(y==3) LcdWriteCom(LINE3); //第四行
- for(x = 0; x<16; x++)
- {
- LcdWriteData( 0x20 );
- }
- }
- void LcdClr0()
- {
- LcdClrLine(0);
- }
- void LcdClr1()
- {
- LcdClrLine(1);
- }
- /*OK
- void LcdClr2()
- {
- LcdClrLine(2);
- }
- void LcdClr3()
- {
- LcdClrLine(3);
- }
- */
復制代碼
def.h
- #ifndef __DEF__H__
- #define __DEF__H__
- #include <reg52.h>
- #ifndef u8
- #define u8 unsigned char
- #endif
- #ifndef bool
- #define bool bit
- #define false 0
- #define true 1
- #endif
- #ifndef u16
- #define u16 unsigned int
- #endif
- #ifndef u32
- #define u32 unsigned long
- #endif
- #ifndef NULL
- #define NULL 0
- #endif
- #define ON 1
- #define OFF 0
- #define _ON 0
- #define _OFF 1
- //ShopAxxx開發板
- #ifdef _USE_SHOP_A_
- sbit BEEP = P1^2; //1 - off 蜂鳴器
- sbit RELAY = P1^0; //1 - off 繼電器
- sbit _LE = P1^5; //0 - off 74HC573(/LE),控制P2口的LED
- #endif
- #endif
復制代碼 |