ameg的笔记 https://bbs.21ic.com/?471810 [收藏] [复制] [RSS]

日志

1602 显示时序

已有 951 次阅读2006-10-27 20:36

经常看数据用寄存器也不直观,就把寄存器上的数用1602显示吧,下面是今天修改的标准时序,没出现问题。适用于我的808实验板。


 


/***********************************************
方便和其他程序连接起来使用,显示用,注释部分可以单独显示
 ***************************************************/


#include <at89x52.h>
#include<intrins.h>


#define LCM_RS P1_2
#define LCM_RW P1_1  
#define LCM_E P1_0
#define LCM_Data P2
#define Busy 0x80   



void WriteDataLCM(unsigned char WDLCM);      
void WriteCommandLCM(unsigned char WCLCM,BuysC);   
unsigned char ReadStatusLCM(void);       
void LCMInit(void);           
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);  
void Delay5Ms(void);
void Delay400Ms(void);



unsigned char code Y1[]={"rong yi dian zi "};         //取消
unsigned char code Y2[]={"meg is a pig    "};         //取消


void main(void)
{
Delay400Ms();         
LCMInit();          
Delay5Ms();         
DisplayListChar(0, 0, Y1);              //取消
DisplayListChar(0, 1, Y2);              //取消
//DisplayOneChar(5,0,'a');
        
while(1);
}



void WriteDataLCM(unsigned char WDLCM) {
 ReadStatusLCM();        //检测忙
 LCM_RS = 1;         //产生写操作时序
 LCM_RW = 0;
   _nop_();
LCM_Data = WDLCM;
   _nop_();
 LCM_E = 1;          //若晶振速度太高可以在这后加小的延时
    _nop_();_nop_();
 LCM_E = 0;          //延时


}



void WriteCommandLCM(unsigned char WCLCM,BuysC){
 if (BuysC) ReadStatusLCM();      //根据需要检测忙
 LCM_RS = 0;          //产生写指令时序
    LCM_RW = 0;                                      //RW=0,时序写
    _nop_();
 LCM_Data = WCLCM;
    _nop_();
 LCM_E = 1;                                       //下降沿
 _nop_();_nop_();
 LCM_E = 0;                                       //E置位
}


unsigned char ReadStatusLCM(void) {
 LCM_Data = 0xFF;
 LCM_RS = 0;                                    //rs=0
 LCM_RW = 1;                                    //rw=1
    _nop_();
     LCM_E = 1;                                              //LCM_E=1
  _nop_();                                      // DB7=0  LCD控制器空闲 ,  DB7=1  LCD控制器忙
    _nop_();
 while (LCM_Data & Busy);       //检测忙信号
 return(LCM_Data);
}


 



void LCMInit(void) {        
 LCM_Data = 0;
 WriteCommandLCM(0x38,0);        
 Delay5Ms();
 WriteCommandLCM(0x38,0);
 Delay5Ms();
 WriteCommandLCM(0x38,0);
 Delay5Ms();


 WriteCommandLCM(0x38,1);        
 WriteCommandLCM(0x08,1);        
 WriteCommandLCM(0x01,1);       
 WriteCommandLCM(0x06,1);        
 WriteCommandLCM(0x0C,1);        
}



void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData) {
 Y &= 0x1;
 X &= 0xF;            
 if (Y) X |= 0x40;          
 X |= 0x80;            
 WriteCommandLCM(X, 0);         
 WriteDataLCM(DData);
}


//按指定位置显示一串字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData) {
 unsigned char ListLength;


 ListLength = 0;
 Y &= 0x1;
 X &= 0xF;            
 //while (DData[ListLength]>0x20) {    
 while (ListLength<0x0f) {
  if (X <= 0xF) {         
   DisplayOneChar(X, Y, DData[ListLength]);  
   ListLength++;
   X++;
  }
 }
}


//5ms延时
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}


//400ms延时
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)