液晶\串行\1602串口 (4bit)通讯

#include 
#include
sbit  RS=P3^4;
sbit  RW=P3^6;
sbit  SCK=P3^5;  //EN
sbit ACC7=ACC^7;
sbit ACC6=ACC^6;
sbit ACC5=ACC^5;
sbit ACC4=ACC^4;
sbit ACC3=ACC^3;
sbit ACC2=ACC^2;
sbit ACC1=ACC^1;
sbit ACC0=ACC^0;
sbit P07=P0^7;
sbit P06=P0^6;
sbit P05=P0^5;
sbit P04=P0^4;

sbit  d0=P2^7;
sbit  d1=P2^6;

#define   uint  unsigned  int
#define   uchar   unsigned  char
#define   H 1
#define   L  0

uchar code  show_init[]={"Welcome to LWH' ELECTRIC world!"} ;
uchar code  show_init1[]={"fdsafdsfadsfdsffELECTRIC world!"} ;
uchar code  day[]={0x0f,0x09,0x09,0x0f,0x09,0x09,0x09,0x0f};
uchar code  year[]={0x0f,0x14,0x04,0x1f,0x14,0x1f,0x04,0x04};
uchar code  month[]={0x1f,0x11,0x09,0x05,0x07,0x05,0x09,0x13};


/*~~~~~~~~~~~~~~~~~~读忙标志子程序~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char busy()
{
  
/*  P0&=0X0F;
  P0|=0XF0;//P0高4位为输入   */
  RS=L;
  RW=H;
  SCK=H;
  //P0&=0X80;
  P0=ACC;
 
  if(ACC7)
  return 1;
  else
  return 0;
} 

void write_com(uchar com) //写命令
{
	uchar temp;
    
    RS=L; //0时表示写命令,1时表示写数据
   	RW=L; //0时表示写入,1时表示读出
    P0&=0X0F;
	temp=com;
    com&=0xf0;
    P0|=com; 
    
  
  /*     ACC=com;  
    P07=ACC7;
    P06=ACC6;
    P05=ACC5;
    P04=ACC4;  */
    SCK=H;
	delay(1);
    SCK=L; 
  
  P0&=0X0F;
    temp<<=4;
      P0|=temp;
  /*  P07=ACC3;
    P06=ACC2;
    P05=ACC1;
    P04=ACC0; */
    SCK=H;
	delay(1);
    SCK=L; 
		 
}

/*~~~~~~~~~~~~~~~~~~写入数据子程序~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void  write_date(uchar date) //写数据
{
	uchar temp;
     RS=H;  
    RW=L; //0时表示写入,1时表示读出
                                      
   P0&=0X0F;
    temp=date;
    date&=0xf0;
    P0|=date;    

  
  /*     ACC=date;  P07=ACC7;
    P06=ACC6;
    P05=ACC5;
    P04=ACC4;  */
    SCK=H;
	delay(1);
    SCK=L; 

   P0&=0X0F;  
    temp<<=4;
    P0|=temp; 
    /* P07=ACC3;
    P06=ACC2;
    P05=ACC1;
    P04=ACC0;   */
    SCK=H;
	delay(1);
    SCK=L;
  
		
}

void  write_zcdate(uchar *date,char addr) //自创写数据
{
	uchar i,temp1;
    temp1=0x40|(addr<<3);      ///?????????????????
  
    for(i=0;i<8;i++)
   {
     while(busy());
    
     write_com(temp1+i);   
     write_date(*date);

     date++;
    }
 
     write_com(0xc0);

     write_date(addr);
		
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~置字符发生存贮器地址~~~~~~~~~~~~~~~~~~~~~~~~~*/



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~初始化程序~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void init()		
{
//	SCK=0;
//	RW=0;
    write_com(0x28);
    SCK=H;
    delay(1);                             //写入时要多写一次设置4位传送 
                                       //,必须佩带一个使能信号
	SCK=L;  
  //  delay(1);


	write_com(0x28);  //4wei数据接口,设置16x2显示,5X7点阵,

     
	write_com(0x0f); //00001DCB; 都等于1时D表示开显示;C表示显示光标;B表示光标闪烁
	write_com(0x06); //000001NS	 N 等于1时表示当读或写一个字符后地址加1,且光标加1,
					//N等于0时则相反;S表示显示
					//S=1时当写一个字符,整屏显示左移(N=1)或右移(N=0);
					//S=0时表示当写一个字符,整屏显示不移动

    //write_com(0x02);//光标返回
                                      //此条命令要有效,应先开光标显示
    
}

void delay(uint z)
{
	uint  x;
    uchar y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);		 //Z=1时定时一毫秒
}

void main()
{
         char i;
         init();
       i=0;
        write_com(0x80);
         while(i<15)
         {
               while(busy());  
               write_date(show_init[i]);
               i++;
               delay(400);
          }
        write_com(0xc0);
        i=16;
        do
        {
              while(busy());  
              write_date(show_init[i]);
              i++;
              delay(400);
         } 
         while(i<31);   
         write_com(0x0f);//显示光标   
     
          while(1)
         {
        
                if(d1==0)
              {
                        delay(10); 
                        if(d1==0)
                       { 
                           while(!d1);
                             while(busy()); 
                             write_com(0x01);
                        }
                }
            
              if(d0==0)
              {
                        delay(10); 
                        if(d0==0)
                       { 
                           while(!d0);  
                                 while(busy()); 
                               write_zcdate(year,1);
                            
                            
                       }
             }
 
      
        }

}

你可能感兴趣的:(单片机,C语言)