專注電子技術學習與研究
當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

基于C8051F的金鵬LCD19264(帶字庫)程序

作者:文于鷹   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年12月06日   【字體:

//-------------------------------------------------------------------------*
//文件名:  LCD.h (LCD頭文件)                                          *
//-------------------------------------------------------------------------*
#include "C8051F340.h"
#include <intrins.h>
#define uint  unsigned int
#define uchar unsigned char

sbit rw = P2^1;           //H=read; L=write;
//sbit e1 = P2^2;           //up input enable;
sbit e2 = P2^2;           //down input enable;
sbit rs = P2^0;           //H=data; L=command;
//sbit rst= P3^0;           //Reset Signal 低電平有效
void init_lcd ();
void clrram ();
void string_disp (uchar up,uchar down,uchar addr,uchar length,uchar code *pp); //顯示字符串
void number_disp (uchar up,uchar down,uchar addr,uchar length,uchar number1,uchar number2); //顯示數(shù)字

//up:上半屏  down:下半屏
//up為1,控制上半屏,down為1,控制下半屏
uchar Math_Subwith(uchar dat)      //線序反轉
{
      uchar i,temp;
      temp = 0x00;
      for(i=0;i<8;i++)
        {
                temp >>= 1;
                if((dat<<i)&0x80)
                {
                        temp |= 0x80;      
                }
                else
                {
                        temp &= 0x7F;
                }
        }
        return temp;
}
//*******************************
//判忙
//*******************************
void chk_busy (uchar up,uchar down)
{
  if(up==1)
  {
 P1 = Math_Subwith(0xff);
    rs=0;
    rw=1;
    //e1=1;
 P4 = P4|0x01;
 while(P1 & 0x01);
    //e1=0;
 _nop_();_nop_();_nop_();_nop_();
 P4 = P4&0xfe;
  }
  if(down==1)
  {
 P1 =Math_Subwith(0xff);   
    rs=0;
    rw=1;
    e2=1;
 while(P1& 0x01);
 _nop_();_nop_();_nop_();_nop_();
    e2=0;
  }
}
//********************************************
//寫命令
//********************************************
void send_c (uchar up,uchar down,uchar comm)
{
   chk_busy (up,down);
   rs=0;                //command
   rw=0;                //write
   if(up==1)
   {
  P1 = Math_Subwith(comm);        //output comm
     //e1=1;
  P4 = P4|0x01;
     _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     //e1=0;
  P4 = P4&0xfe;
   }
   if(down==1)
   {
  P1 = Math_Subwith(comm);       //output comm
     e2=1;
     _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     e2=0;
   }
}
//********************************************
//寫數(shù)據
//********************************************
void send_d (uchar up,uchar down,uchar dat)
{
  chk_busy (up,down);
  rs=1;               //data
  rw=0;               //write
  if(up==1)
  {
 P1 = Math_Subwith(dat);       //output data
    //e1=1;
 P4 = P4|0x01;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
    //e1=0;
 P4 = P4&0xfe;
  }
  if(down==1)
  {
 P1 = Math_Subwith(dat);       //output data
    e2=1;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
    e2=0;
  }
}
/*------------------初始化-----------------*/
void init_lcd (void)
{

   send_c (1,1,0x30);        /*30---基本指令動作*/

 send_c (1,1,0x01);        /*清屏,地址指針指向00H*/

 send_c (1,1,0x06);        /*光標的移動方向*/

 send_c (1,1,0x0c);        /*開顯示,關游標*/

}
/*---------------顯示漢字或字符----------------*/
/*void chn_disp (uchar code *chn)
{
  uchar i;
   send_c (1,1,0x30);       //第一行
 send_c (1,0,0x80);
    for (i=0;i<24;i++)
    send_d (1,0,chn[i]);

   send_c (1,0,0x90);       //第二行
    for (i=0;i<24;i++)
    send_d (1,0,chn[24+i]);

    send_c (0,1,0x80);       //第三行
    for (i=0;i<24;i++)
    send_d (0,1,chn[48+i]);

   send_c (0,1,0x90);       //第四行
    for (i=0;i<24;i++)
    send_d (0,1,chn[72+i]);
}*/
/*---------------任意位置顯示漢字或字符----------------*/
void string_disp (uchar up,uchar down,uchar addr,uchar length,uchar code *pp)//地址,長度,數(shù)據
{
  uchar i;
   send_c (1,1,0x30);      
 send_c (up,down,addr);       //第一行
    for (i=0;i<length;i++)
    send_d (up,down,pp[i]);
}
void number_disp (uchar up,uchar down,uchar addr,uchar length,uchar number1,uchar number2)//地址,長度,數(shù)據
{
  uchar i;
   send_c (1,1,0x30);      
 send_c (up,down,addr);       //第一行
    for (i=0;i<length;i++)
    send_d (up,down,number1);
 for(i=1;i<(length+1);i++)
 send_d (up,down,number2);
}
/*--------------清DDRAM------------------*/
void clrram (void)
{
   send_c (1,1,0x30);
   send_c (1,1,0x01);
}

關閉窗口

相關文章