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

日志

1602液晶移位多列显示C程序

已有 1592 次阅读2007-11-14 13:48 |系统分类:单片机| aa

#include <reg52.h>


#include <intrins.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint  unsigned int
#endif
#define DATA  P1
sbit RS=P3^7;
sbit RW=P3^6;
sbit E =P3^5;
sbit BS=P1^7;


 uchar *h="hello everyone ! \n   welcome!   " ;
 
void busy()        //???????
{
 do
 {
  DATA=0xFF;
  RS=0;
  RW=1;
  E=0;
  _nop_();
  E=1;
 }while(BS);
}
void command(uchar dat)
{
 DATA=dat;
 RS=0;
 RW=0;
 E=0;
 busy();
 E=1;
}
void write(uchar dat)
{
 DATA=dat;
 RS=1;
 RW=0;
 E=0;
 busy();
 E=1;
}


void clear(uchar dat)     //???  1 ??
{
 command(dat);
}


void back(uchar a)        //????
{
 a;
 command(0x02);
}


void mov(uchar id,uchar s)                      //id ?????????1?? 0 ??
{                                               //s???????????1? 0 ?
 command(0x04|(id<<1)|s);
}


void run(uchar d,uchar c,uchar b)               //??????
{                                               //d  ??????   1 ?  2 ?
 command(0x08|(d<<2)|(c<<1)|b);                 //c  ??????   1 ?  2 ?
}
                                                //b  ??????   1 ?  2 ?
void shift(uchar sc,uchar rl)                   //sc 1 ?????? 0 ??????
{                                               //rl 1 ??         0 ??
 command(0x10|(sc<<3)|(rl<<2));
}


void function(uchar dl,uchar n,uchar f)          //dl 1 ????     0 ????
                                                 //??dl 1????  0  ???? ????????
{                                                //n  1 ????     0 ????
 command(0x20|(dl<<4)|(n<<3)|(f<<2));            //f  1 5*10??     0 5*7 ??
}


void charaddr(uchar agg)                         //????????
{
 command(0x40|agg);
}


void dataaddr(uchar add)                         //?????????
{
 command(add|0x80);
}


void text(uchar dat)      //?????
{
 write(dat);
}


void delay(uint t)
{
 uint i;
 while(!((t--)==0x0))
   {
   for(i=0;i<0x1fff;i++);
   }
}


void print(uchar row ,char *p)   //????  row??????  *p??????
{
 uchar a=0x0,i=1;
 dataaddr(row);
  while(i--)
 {
 while((*p)!='\0')
  {
   text(*p++);
   delay(2);
   if(((*p)=='\n'))
     {
     back(2);
     dataaddr(0x50);
     }
   }
 p=&h[0];
    dataaddr(0x10);
 }
}
/*main.c**************************************/
#define uchar unsigned char
#define uint  unsigned int
void initial()
{
 clear(1);
 function(1,1,0);
 mov(1,1);
 shift(1,1);
 run(1,0,0);
 clear(0);
}
void main()
{
 uchar s;
 s=10;
 initial();
 do
 {
 clear(1);
 back(2);
 print(0x10,h);
 delay(5);
 _nop_();
 delay(0x00);
 clear(1);
 back(2);
 print(0x10,"I can ! belive me ! ");
 delay(5);
 _nop_();
 delay(0x00);
 }
 while(s--);
 clear(1);
 while(1);
}



 


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)