專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

基于51單片機(jī)1602庫+顯示函數(shù)(已驗(yàn)證)

作者:hkxiaoma   來源:互聯(lián)網(wǎng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年07月29日   【字體:

 

LCD1602.c

#include<reg52.h>
#include "LCD1602.h"

unsigned char TempBuffer[10];
/////////////////////////////////////////////////////
//////////////////五位數(shù)字顯示轉(zhuǎn)換///////////////////
/////////////////////////////////////////////////////
void IntToStr(unsigned int t, unsigned char *str, unsigned char n)
{
 unsigned char a[5]; char i, j;                                  
 a[0]=(t/10000);         //取得整數(shù)值到數(shù)組          
 a[1]=(t/1000);                                      
 a[2]=(t/100);                                       
 a[3]=(t/10);                                        
 a[4]=(t/1);                                         
                                                     
 for(i=0; i<5; i++)         //轉(zhuǎn)成ASCII碼               
  a[i]=a[i]+'0';                                     
 for(i=0; a[i]=='0' && i<=3; i++);                      
 for(j=5-n; j
  { *str=' ';  str++; }                              
 for(; i<5; i++)                                        
  { *str=a[i]; str++; }  //加入有效的數(shù)字            
 *str='\0';
}
 
void Delay1ms(unsigned int count)
{
 unsigned int i,j;
 for(i=0;i
 for(j=0;j<120;j++);
}

main()
{
 unsigned int Count = 0,ab;
 LCD_Initial();
 GotoXY(0,0);
 Print("xiaom:");
 GotoXY(0,1);//0表示次數(shù)字節(jié)長度不限制  1表示第二行
 Print("luotao:         ");
 Count=32277767;ab=3254;
 while(1)
 {
  IntToStr(Count,&TempBuffer[0],8);//8表示此處數(shù)字長度為8位數(shù)
  GotoXY(6,0);//6表示此處數(shù)字長度為6位
  Print(&TempBuffer[0]);
  IntToStr(ab,&TempBuffer[0],3);
  GotoXY(7,1);
  Print(&TempBuffer[0]);
  Delay1ms(100);
 }
}

 

 

LCD1602.h

#ifndef LCD_CHAR_1602_2005_4_9
#define LCD_CHAR_1602_2005_4_9

#include

//Port Definitions**********************************************************
sbit LcdRs  = P2^0;
sbit LcdRw  = P2^1;
sbit LcdEn   = P2^2;
sfr  DBPort  = 0x80;  //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.數(shù)據(jù)端口

//內(nèi)部等待函數(shù)**************************************************************************
unsigned char LCD_Wait(void)
{
 LcdRs=0;
 LcdRw=1; _nop_();
 LcdEn=1; _nop_();
 //while(DBPort&0x80);//在用Proteus仿真時(shí),注意用屏蔽此語句,在調(diào)用GotoXY()時(shí),會(huì)進(jìn)入死循環(huán),
       //可能在寫該控制字時(shí),該模塊沒有返回寫入完備命令,即DBPort&0x80==0x80
       //實(shí)際硬件時(shí)打開此語句
 LcdEn=0;
 return DBPort;  
}
//向LCD寫入命令或數(shù)據(jù)************************************************************
#define LCD_COMMAND        // Command
#define LCD_DATA        // Data
#define LCD_CLEAR_SCREEN 0x01      // 清屏
#define LCD_HOMING    0x02      // 光標(biāo)返回原點(diǎn)
void LCD_Write(bit style, unsigned char input)
{
 LcdEn=0;
 LcdRs=style;
 LcdRw=0;  _nop_();
 DBPort=input; _nop_();//注意順序
 LcdEn=1;  _nop_();//注意順序
 LcdEn=0;  _nop_();
 LCD_Wait(); 
}

//設(shè)置顯示模式************************************************************
#define LCD_SHOW   0x04    //顯示開
#define LCD_HIDE   0x00    //顯示關(guān)  

#define LCD_CURSOR   0x02  //顯示光標(biāo)
#define LCD_NO_CURSOR  0x00    //無光標(biāo)      

#define LCD_FLASH   0x01    //光標(biāo)閃動(dòng)
#define LCD_NO_FLASH  0x00    //光標(biāo)不閃動(dòng)

void LCD_SetDisplay(unsigned char DisplayMode)
{
 LCD_Write(LCD_COMMAND, 0x08|DisplayMode); 
}

//設(shè)置輸入模式************************************************************
#define LCD_AC_UP   0x02
#define LCD_AC_DOWN   0x00      // default

#define LCD_MOVE   0x01      // 畫面可平移
#define LCD_NO_MOVE   0x00      //default

void LCD_SetInput(unsigned char InputMode)
{
 LCD_Write(LCD_COMMAND, 0x04|InputMode);
}

//移動(dòng)光標(biāo)或屏幕************************************************************

//初始化LCD************************************************************
void LCD_Initial()
{
 LcdEn=0;
 LCD_Write(LCD_COMMAND,0x38);           //8位數(shù)據(jù)端口,2行顯示,5*7點(diǎn)陣
 LCD_Write(LCD_COMMAND,0x38);
 LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR);    //開啟顯示, 無光標(biāo)
 LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN);   //清屏
 LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE);       //AC遞增, 畫面不動(dòng)
}

/
//************************************************************************
#endif

關(guān)閉窗口

相關(guān)文章